The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 16 | import netrc |
Shawn O. Pearce | 2a1ccb2 | 2009-04-10 16:51:53 -0700 | [diff] [blame] | 17 | from optparse import SUPPRESS_HELP |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import os |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 19 | import pickle |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import re |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 21 | import shutil |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 22 | import socket |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import subprocess |
| 24 | import sys |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 25 | import time |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 26 | import urlparse |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 27 | import xmlrpclib |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 29 | try: |
| 30 | import threading as _threading |
| 31 | except ImportError: |
| 32 | import dummy_threading as _threading |
| 33 | |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 34 | try: |
| 35 | import resource |
| 36 | def _rlimit_nofile(): |
| 37 | return resource.getrlimit(resource.RLIMIT_NOFILE) |
| 38 | except ImportError: |
| 39 | def _rlimit_nofile(): |
| 40 | return (256, 256) |
| 41 | |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 42 | try: |
| 43 | import multiprocessing |
| 44 | except ImportError: |
| 45 | multiprocessing = None |
| 46 | |
Dave Borowitz | e215267 | 2012-10-31 12:24:38 -0700 | [diff] [blame] | 47 | from git_command import GIT, git_require |
David Pursehouse | d94aaef | 2012-09-07 09:52:04 +0900 | [diff] [blame] | 48 | from git_refs import R_HEADS, HEAD |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 49 | from main import WrapperModule |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 50 | from project import Project |
| 51 | from project import RemoteSpec |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 52 | from command import Command, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | from error import RepoChangedException, GitError |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 54 | from project import SyncBuffer |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 55 | from progress import Progress |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 57 | _ONE_DAY_S = 24 * 60 * 60 |
| 58 | |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 59 | class _FetchError(Exception): |
| 60 | """Internal error thrown in _FetchHelper() when we don't want stack trace.""" |
| 61 | pass |
| 62 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 63 | class Sync(Command, MirrorSafeCommand): |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 64 | jobs = 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | common = True |
| 66 | helpSummary = "Update working tree to the latest revision" |
| 67 | helpUsage = """ |
| 68 | %prog [<project>...] |
| 69 | """ |
| 70 | helpDescription = """ |
| 71 | The '%prog' command synchronizes local project directories |
| 72 | with the remote repositories specified in the manifest. If a local |
| 73 | project does not yet exist, it will clone a new local directory from |
| 74 | the remote repository and set up tracking branches as specified in |
| 75 | the manifest. If the local project already exists, '%prog' |
| 76 | will update the remote branches and rebase any new local changes |
| 77 | on top of the new remote changes. |
| 78 | |
| 79 | '%prog' will synchronize all projects listed at the command |
| 80 | line. Projects can be specified either by name, or by a relative |
| 81 | or absolute path to the project's local directory. If no projects |
| 82 | are specified, '%prog' will synchronize all projects listed in |
| 83 | the manifest. |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 84 | |
| 85 | The -d/--detach option can be used to switch specified projects |
| 86 | back to the manifest revision. This option is especially helpful |
| 87 | if the project is currently on a topic branch, but the manifest |
| 88 | revision is temporarily needed. |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 89 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 90 | The -s/--smart-sync option can be used to sync to a known good |
| 91 | build as specified by the manifest-server element in the current |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 92 | manifest. The -t/--smart-tag option is similar and allows you to |
| 93 | specify a custom tag/label. |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 94 | |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 95 | The -u/--manifest-server-username and -p/--manifest-server-password |
| 96 | options can be used to specify a username and password to authenticate |
| 97 | with the manifest server when using the -s or -t option. |
| 98 | |
| 99 | If -u and -p are not specified when using the -s or -t option, '%prog' |
| 100 | will attempt to read authentication credentials for the manifest server |
| 101 | from the user's .netrc file. |
| 102 | |
| 103 | '%prog' will not use authentication credentials from -u/-p or .netrc |
| 104 | if the manifest server specified in the manifest file already includes |
| 105 | credentials. |
| 106 | |
Andrei Warkentin | 5df6de0 | 2010-07-02 17:58:31 -0500 | [diff] [blame] | 107 | The -f/--force-broken option can be used to proceed with syncing |
| 108 | other projects if a project sync fails. |
| 109 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 110 | The --no-clone-bundle option disables any attempt to use |
| 111 | $URL/clone.bundle to bootstrap a new Git repository from a |
| 112 | resumeable bundle file on a content delivery network. This |
| 113 | may be necessary if there are problems with the local Python |
| 114 | HTTP client or proxy configuration, but the Git binary works. |
| 115 | |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 116 | SSH Connections |
| 117 | --------------- |
| 118 | |
| 119 | If at least one project remote URL uses an SSH connection (ssh://, |
| 120 | git+ssh://, or user@host:path syntax) repo will automatically |
| 121 | enable the SSH ControlMaster option when connecting to that host. |
| 122 | This feature permits other projects in the same '%prog' session to |
| 123 | reuse the same SSH tunnel, saving connection setup overheads. |
| 124 | |
| 125 | To disable this behavior on UNIX platforms, set the GIT_SSH |
| 126 | environment variable to 'ssh'. For example: |
| 127 | |
| 128 | export GIT_SSH=ssh |
| 129 | %prog |
| 130 | |
| 131 | Compatibility |
| 132 | ~~~~~~~~~~~~~ |
| 133 | |
| 134 | This feature is automatically disabled on Windows, due to the lack |
| 135 | of UNIX domain socket support. |
| 136 | |
| 137 | This feature is not compatible with url.insteadof rewrites in the |
| 138 | user's ~/.gitconfig. '%prog' is currently not able to perform the |
| 139 | rewrite early enough to establish the ControlMaster tunnel. |
| 140 | |
| 141 | If the remote SSH daemon is Gerrit Code Review, version 2.0.10 or |
| 142 | later is required to fix a server side protocol bug. |
| 143 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 144 | """ |
| 145 | |
Nico Sallembien | 6623b21 | 2010-05-11 12:57:01 -0700 | [diff] [blame] | 146 | def _Options(self, p, show_smart=True): |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 147 | self.jobs = self.manifest.default.sync_j |
| 148 | |
Andrei Warkentin | 5df6de0 | 2010-07-02 17:58:31 -0500 | [diff] [blame] | 149 | p.add_option('-f', '--force-broken', |
| 150 | dest='force_broken', action='store_true', |
| 151 | help="continue sync even if a project fails to sync") |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 152 | p.add_option('-l','--local-only', |
| 153 | dest='local_only', action='store_true', |
| 154 | help="only update working tree, don't fetch") |
Shawn O. Pearce | 96fdcef | 2009-04-10 16:29:20 -0700 | [diff] [blame] | 155 | p.add_option('-n','--network-only', |
| 156 | dest='network_only', action='store_true', |
| 157 | help="fetch only, don't update working tree") |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 158 | p.add_option('-d','--detach', |
| 159 | dest='detach_head', action='store_true', |
| 160 | help='detach projects back to manifest revision') |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 161 | p.add_option('-c','--current-branch', |
| 162 | dest='current_branch_only', action='store_true', |
| 163 | help='fetch only current branch from server') |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 164 | p.add_option('-q','--quiet', |
| 165 | dest='quiet', action='store_true', |
| 166 | help='be more quiet') |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 167 | p.add_option('-j','--jobs', |
| 168 | dest='jobs', action='store', type='int', |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 169 | help="projects to fetch simultaneously (default %d)" % self.jobs) |
Chris Wolfe | e9dc3b3 | 2012-01-26 11:36:18 -0500 | [diff] [blame] | 170 | p.add_option('-m', '--manifest-name', |
| 171 | dest='manifest_name', |
| 172 | help='temporary manifest to use for this sync', metavar='NAME.xml') |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 173 | p.add_option('--no-clone-bundle', |
| 174 | dest='no_clone_bundle', action='store_true', |
| 175 | help='disable use of /clone.bundle on HTTP/HTTPS') |
Conley Owens | 8d070cf | 2012-11-06 13:14:31 -0800 | [diff] [blame] | 176 | p.add_option('-u', '--manifest-server-username', action='store', |
| 177 | dest='manifest_server_username', |
| 178 | help='username to authenticate with the manifest server') |
| 179 | p.add_option('-p', '--manifest-server-password', action='store', |
| 180 | dest='manifest_server_password', |
| 181 | help='password to authenticate with the manifest server') |
Nico Sallembien | 6623b21 | 2010-05-11 12:57:01 -0700 | [diff] [blame] | 182 | if show_smart: |
| 183 | p.add_option('-s', '--smart-sync', |
| 184 | dest='smart_sync', action='store_true', |
| 185 | help='smart sync using manifest from a known good build') |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 186 | p.add_option('-t', '--smart-tag', |
| 187 | dest='smart_tag', action='store', |
| 188 | help='smart sync using manifest from a known tag') |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 189 | |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 190 | g = p.add_option_group('repo Version options') |
| 191 | g.add_option('--no-repo-verify', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 192 | dest='no_repo_verify', action='store_true', |
| 193 | help='do not verify repo source code') |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 194 | g.add_option('--repo-upgraded', |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 195 | dest='repo_upgraded', action='store_true', |
Shawn O. Pearce | 2a1ccb2 | 2009-04-10 16:51:53 -0700 | [diff] [blame] | 196 | help=SUPPRESS_HELP) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 198 | def _FetchHelper(self, opt, project, lock, fetched, pm, sem, err_event): |
| 199 | """Main function of the fetch threads when jobs are > 1. |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 200 | |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 201 | Args: |
| 202 | opt: Program options returned from optparse. See _Options(). |
| 203 | project: Project object for the project to fetch. |
| 204 | lock: Lock for accessing objects that are shared amongst multiple |
| 205 | _FetchHelper() threads. |
| 206 | fetched: set object that we will add project.gitdir to when we're done |
| 207 | (with our lock held). |
| 208 | pm: Instance of a Project object. We will call pm.update() (with our |
| 209 | lock held). |
| 210 | sem: We'll release() this semaphore when we exit so that another thread |
| 211 | can be started up. |
| 212 | err_event: We'll set this event in the case of an error (after printing |
| 213 | out info about the error). |
| 214 | """ |
| 215 | # We'll set to true once we've locked the lock. |
| 216 | did_lock = False |
| 217 | |
| 218 | # Encapsulate everything in a try/except/finally so that: |
| 219 | # - We always set err_event in the case of an exception. |
| 220 | # - We always make sure we call sem.release(). |
| 221 | # - We always make sure we unlock the lock if we locked it. |
| 222 | try: |
Shawn O. Pearce | e6a0eeb | 2011-03-22 19:04:47 -0700 | [diff] [blame] | 223 | try: |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 224 | start = time.time() |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 225 | success = project.Sync_NetworkHalf( |
| 226 | quiet=opt.quiet, |
| 227 | current_branch_only=opt.current_branch_only, |
| 228 | clone_bundle=not opt.no_clone_bundle) |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 229 | self._fetch_times.Set(project, time.time() - start) |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 230 | |
Shawn O. Pearce | e6a0eeb | 2011-03-22 19:04:47 -0700 | [diff] [blame] | 231 | # Lock around all the rest of the code, since printing, updating a set |
| 232 | # and Progress.update() are not thread safe. |
| 233 | lock.acquire() |
| 234 | did_lock = True |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 235 | |
Shawn O. Pearce | e6a0eeb | 2011-03-22 19:04:47 -0700 | [diff] [blame] | 236 | if not success: |
| 237 | print >>sys.stderr, 'error: Cannot fetch %s' % project.name |
| 238 | if opt.force_broken: |
| 239 | print >>sys.stderr, 'warn: --force-broken, continuing to sync' |
| 240 | else: |
| 241 | raise _FetchError() |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 242 | |
Shawn O. Pearce | e6a0eeb | 2011-03-22 19:04:47 -0700 | [diff] [blame] | 243 | fetched.add(project.gitdir) |
| 244 | pm.update() |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 245 | except _FetchError: |
Shawn O. Pearce | e6a0eeb | 2011-03-22 19:04:47 -0700 | [diff] [blame] | 246 | err_event.set() |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 247 | except: |
| 248 | err_event.set() |
| 249 | raise |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 250 | finally: |
| 251 | if did_lock: |
| 252 | lock.release() |
| 253 | sem.release() |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 254 | |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 255 | def _Fetch(self, projects, opt): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 256 | fetched = set() |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 257 | pm = Progress('Fetching projects', len(projects)) |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 258 | |
| 259 | if self.jobs == 1: |
| 260 | for project in projects: |
| 261 | pm.update() |
Shawn O. Pearce | 5d0efdb | 2012-08-02 12:13:01 -0700 | [diff] [blame] | 262 | if project.Sync_NetworkHalf( |
| 263 | quiet=opt.quiet, |
| 264 | current_branch_only=opt.current_branch_only, |
| 265 | clone_bundle=not opt.no_clone_bundle): |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 266 | fetched.add(project.gitdir) |
| 267 | else: |
| 268 | print >>sys.stderr, 'error: Cannot fetch %s' % project.name |
Andrei Warkentin | 5df6de0 | 2010-07-02 17:58:31 -0500 | [diff] [blame] | 269 | if opt.force_broken: |
| 270 | print >>sys.stderr, 'warn: --force-broken, continuing to sync' |
| 271 | else: |
| 272 | sys.exit(1) |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 273 | else: |
| 274 | threads = set() |
| 275 | lock = _threading.Lock() |
| 276 | sem = _threading.Semaphore(self.jobs) |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 277 | err_event = _threading.Event() |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 278 | for project in projects: |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 279 | # Check for any errors before starting any new threads. |
| 280 | # ...we'll let existing threads finish, though. |
Daniel Sandler | 723c5dc | 2011-04-04 11:15:17 -0400 | [diff] [blame] | 281 | if err_event.isSet(): |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 282 | break |
| 283 | |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 284 | sem.acquire() |
| 285 | t = _threading.Thread(target = self._FetchHelper, |
Shawn O. Pearce | 16614f8 | 2010-10-29 12:05:43 -0700 | [diff] [blame] | 286 | args = (opt, |
| 287 | project, |
| 288 | lock, |
| 289 | fetched, |
| 290 | pm, |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 291 | sem, |
| 292 | err_event)) |
David 'Digit' Turner | e212665 | 2012-09-05 10:35:06 +0200 | [diff] [blame] | 293 | # Ensure that Ctrl-C will not freeze the repo process. |
| 294 | t.daemon = True |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 295 | threads.add(t) |
| 296 | t.start() |
| 297 | |
| 298 | for t in threads: |
| 299 | t.join() |
| 300 | |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 301 | # If we saw an error, exit with code 1 so that other scripts can check. |
Daniel Sandler | 723c5dc | 2011-04-04 11:15:17 -0400 | [diff] [blame] | 302 | if err_event.isSet(): |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 303 | print >>sys.stderr, '\nerror: Exited sync due to fetch errors' |
| 304 | sys.exit(1) |
| 305 | |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 306 | pm.end() |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 307 | self._fetch_times.Save() |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 308 | |
| 309 | self._GCProjects(projects) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | return fetched |
| 311 | |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 312 | def _GCProjects(self, projects): |
Dave Borowitz | e215267 | 2012-10-31 12:24:38 -0700 | [diff] [blame] | 313 | has_dash_c = git_require((1, 7, 2)) |
| 314 | if multiprocessing and has_dash_c: |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 315 | cpu_count = multiprocessing.cpu_count() |
| 316 | else: |
| 317 | cpu_count = 1 |
| 318 | jobs = min(self.jobs, cpu_count) |
| 319 | |
| 320 | if jobs < 2: |
| 321 | for project in projects: |
| 322 | project.bare_git.gc('--auto') |
| 323 | return |
| 324 | |
| 325 | config = {'pack.threads': cpu_count / jobs if cpu_count > jobs else 1} |
| 326 | |
| 327 | threads = set() |
| 328 | sem = _threading.Semaphore(jobs) |
| 329 | err_event = _threading.Event() |
| 330 | |
| 331 | def GC(project): |
| 332 | try: |
| 333 | try: |
| 334 | project.bare_git.gc('--auto', config=config) |
| 335 | except GitError: |
| 336 | err_event.set() |
| 337 | except: |
| 338 | err_event.set() |
| 339 | raise |
| 340 | finally: |
| 341 | sem.release() |
| 342 | |
| 343 | for project in projects: |
| 344 | if err_event.isSet(): |
| 345 | break |
| 346 | sem.acquire() |
| 347 | t = _threading.Thread(target=GC, args=(project,)) |
| 348 | t.daemon = True |
| 349 | threads.add(t) |
| 350 | t.start() |
| 351 | |
| 352 | for t in threads: |
| 353 | t.join() |
| 354 | |
| 355 | if err_event.isSet(): |
| 356 | print >>sys.stderr, '\nerror: Exited sync due to gc errors' |
| 357 | sys.exit(1) |
| 358 | |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 359 | def UpdateProjectList(self): |
| 360 | new_project_paths = [] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 361 | for project in self.GetProjects(None, missing_ok=True): |
Shawn O. Pearce | 3a68bb4 | 2009-06-04 16:18:09 -0700 | [diff] [blame] | 362 | if project.relpath: |
| 363 | new_project_paths.append(project.relpath) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 364 | file_name = 'project.list' |
| 365 | file_path = os.path.join(self.manifest.repodir, file_name) |
| 366 | old_project_paths = [] |
| 367 | |
| 368 | if os.path.exists(file_path): |
| 369 | fd = open(file_path, 'r') |
| 370 | try: |
| 371 | old_project_paths = fd.read().split('\n') |
| 372 | finally: |
| 373 | fd.close() |
| 374 | for path in old_project_paths: |
Shawn O. Pearce | 3a68bb4 | 2009-06-04 16:18:09 -0700 | [diff] [blame] | 375 | if not path: |
| 376 | continue |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 377 | if path not in new_project_paths: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 378 | # If the path has already been deleted, we don't need to do it |
Anthony | f3fdf82 | 2009-09-26 13:38:52 -0400 | [diff] [blame] | 379 | if os.path.exists(self.manifest.topdir + '/' + path): |
| 380 | project = Project( |
| 381 | manifest = self.manifest, |
| 382 | name = path, |
| 383 | remote = RemoteSpec('origin'), |
| 384 | gitdir = os.path.join(self.manifest.topdir, |
| 385 | path, '.git'), |
| 386 | worktree = os.path.join(self.manifest.topdir, path), |
| 387 | relpath = path, |
| 388 | revisionExpr = 'HEAD', |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 389 | revisionId = None, |
| 390 | groups = None) |
Anthony | f3fdf82 | 2009-09-26 13:38:52 -0400 | [diff] [blame] | 391 | |
| 392 | if project.IsDirty(): |
| 393 | print >>sys.stderr, 'error: Cannot remove project "%s": \ |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 394 | uncommitted changes are present' % project.relpath |
Anthony | f3fdf82 | 2009-09-26 13:38:52 -0400 | [diff] [blame] | 395 | print >>sys.stderr, ' commit changes, then run sync again' |
| 396 | return -1 |
| 397 | else: |
| 398 | print >>sys.stderr, 'Deleting obsolete path %s' % project.worktree |
| 399 | shutil.rmtree(project.worktree) |
| 400 | # Try deleting parent subdirs if they are empty |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 401 | project_dir = os.path.dirname(project.worktree) |
| 402 | while project_dir != self.manifest.topdir: |
Anthony | f3fdf82 | 2009-09-26 13:38:52 -0400 | [diff] [blame] | 403 | try: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 404 | os.rmdir(project_dir) |
Anthony | f3fdf82 | 2009-09-26 13:38:52 -0400 | [diff] [blame] | 405 | except OSError: |
| 406 | break |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 407 | project_dir = os.path.dirname(project_dir) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 408 | |
Shawn O. Pearce | 9fb29ce | 2009-06-04 20:41:02 -0700 | [diff] [blame] | 409 | new_project_paths.sort() |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 410 | fd = open(file_path, 'w') |
| 411 | try: |
| 412 | fd.write('\n'.join(new_project_paths)) |
Shawn O. Pearce | 3a68bb4 | 2009-06-04 16:18:09 -0700 | [diff] [blame] | 413 | fd.write('\n') |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 414 | finally: |
| 415 | fd.close() |
| 416 | return 0 |
| 417 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 418 | def Execute(self, opt, args): |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 419 | if opt.jobs: |
| 420 | self.jobs = opt.jobs |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 421 | if self.jobs > 1: |
| 422 | soft_limit, _ = _rlimit_nofile() |
| 423 | self.jobs = min(self.jobs, (soft_limit - 5) / 3) |
| 424 | |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 425 | if opt.network_only and opt.detach_head: |
| 426 | print >>sys.stderr, 'error: cannot combine -n and -d' |
| 427 | sys.exit(1) |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 428 | if opt.network_only and opt.local_only: |
| 429 | print >>sys.stderr, 'error: cannot combine -n and -l' |
| 430 | sys.exit(1) |
Chris Wolfe | e9dc3b3 | 2012-01-26 11:36:18 -0500 | [diff] [blame] | 431 | if opt.manifest_name and opt.smart_sync: |
| 432 | print >>sys.stderr, 'error: cannot combine -m and -s' |
| 433 | sys.exit(1) |
| 434 | if opt.manifest_name and opt.smart_tag: |
| 435 | print >>sys.stderr, 'error: cannot combine -m and -t' |
| 436 | sys.exit(1) |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 437 | if opt.manifest_server_username or opt.manifest_server_password: |
| 438 | if not (opt.smart_sync or opt.smart_tag): |
| 439 | print >>sys.stderr, 'error: -u and -p may only be combined with ' \ |
| 440 | '-s or -t' |
| 441 | sys.exit(1) |
| 442 | if None in [opt.manifest_server_username, opt.manifest_server_password]: |
| 443 | print >>sys.stderr, 'error: both -u and -p must be given' |
| 444 | sys.exit(1) |
Chris Wolfe | e9dc3b3 | 2012-01-26 11:36:18 -0500 | [diff] [blame] | 445 | |
| 446 | if opt.manifest_name: |
| 447 | self.manifest.Override(opt.manifest_name) |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 448 | |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 449 | if opt.smart_sync or opt.smart_tag: |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 450 | if not self.manifest.manifest_server: |
| 451 | print >>sys.stderr, \ |
| 452 | 'error: cannot smart sync: no manifest server defined in manifest' |
| 453 | sys.exit(1) |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 454 | |
| 455 | manifest_server = self.manifest.manifest_server |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 456 | |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 457 | if not '@' in manifest_server: |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 458 | username = None |
| 459 | password = None |
| 460 | if opt.manifest_server_username and opt.manifest_server_password: |
| 461 | username = opt.manifest_server_username |
| 462 | password = opt.manifest_server_password |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 463 | else: |
| 464 | try: |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 465 | info = netrc.netrc() |
| 466 | except IOError: |
| 467 | print >>sys.stderr, '.netrc file does not exist or could not be opened' |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 468 | else: |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 469 | try: |
| 470 | parse_result = urlparse.urlparse(manifest_server) |
| 471 | if parse_result.hostname: |
| 472 | username, _account, password = \ |
| 473 | info.authenticators(parse_result.hostname) |
| 474 | except TypeError: |
| 475 | # TypeError is raised when the given hostname is not present |
| 476 | # in the .netrc file. |
| 477 | print >>sys.stderr, 'No credentials found for %s in .netrc' % \ |
| 478 | parse_result.hostname |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 479 | except netrc.NetrcParseError as e: |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 480 | print >>sys.stderr, 'Error parsing .netrc file: %s' % e |
| 481 | |
| 482 | if (username and password): |
| 483 | manifest_server = manifest_server.replace('://', '://%s:%s@' % |
| 484 | (username, password), |
| 485 | 1) |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 486 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 487 | try: |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 488 | server = xmlrpclib.Server(manifest_server) |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 489 | if opt.smart_sync: |
| 490 | p = self.manifest.manifestProject |
| 491 | b = p.GetBranch(p.CurrentBranch) |
| 492 | branch = b.merge |
| 493 | if branch.startswith(R_HEADS): |
| 494 | branch = branch[len(R_HEADS):] |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 495 | |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 496 | env = os.environ.copy() |
| 497 | if (env.has_key('TARGET_PRODUCT') and |
| 498 | env.has_key('TARGET_BUILD_VARIANT')): |
| 499 | target = '%s-%s' % (env['TARGET_PRODUCT'], |
| 500 | env['TARGET_BUILD_VARIANT']) |
| 501 | [success, manifest_str] = server.GetApprovedManifest(branch, target) |
| 502 | else: |
| 503 | [success, manifest_str] = server.GetApprovedManifest(branch) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 504 | else: |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 505 | assert(opt.smart_tag) |
| 506 | [success, manifest_str] = server.GetManifest(opt.smart_tag) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 507 | |
| 508 | if success: |
| 509 | manifest_name = "smart_sync_override.xml" |
| 510 | manifest_path = os.path.join(self.manifest.manifestProject.worktree, |
| 511 | manifest_name) |
| 512 | try: |
| 513 | f = open(manifest_path, 'w') |
| 514 | try: |
| 515 | f.write(manifest_str) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 516 | finally: |
| 517 | f.close() |
| 518 | except IOError: |
| 519 | print >>sys.stderr, 'error: cannot write manifest to %s' % \ |
| 520 | manifest_path |
| 521 | sys.exit(1) |
Nico Sallembien | 719965a | 2010-04-20 15:28:19 -0700 | [diff] [blame] | 522 | self.manifest.Override(manifest_name) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 523 | else: |
| 524 | print >>sys.stderr, 'error: %s' % manifest_str |
| 525 | sys.exit(1) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 526 | except (socket.error, IOError, xmlrpclib.Fault) as e: |
David Pursehouse | bd489c4 | 2012-08-23 10:21:26 +0900 | [diff] [blame] | 527 | print >>sys.stderr, 'error: cannot connect to manifest server %s:\n%s' % ( |
| 528 | self.manifest.manifest_server, e) |
| 529 | sys.exit(1) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 530 | except xmlrpclib.ProtocolError as e: |
David Pursehouse | bd489c4 | 2012-08-23 10:21:26 +0900 | [diff] [blame] | 531 | print >>sys.stderr, 'error: cannot connect to manifest server %s:\n%d %s' % ( |
| 532 | self.manifest.manifest_server, e.errcode, e.errmsg) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 533 | sys.exit(1) |
| 534 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 535 | rp = self.manifest.repoProject |
| 536 | rp.PreSync() |
| 537 | |
| 538 | mp = self.manifest.manifestProject |
| 539 | mp.PreSync() |
| 540 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 541 | if opt.repo_upgraded: |
Shawn O. Pearce | 80d2ceb | 2012-10-26 12:23:05 -0700 | [diff] [blame] | 542 | _PostRepoUpgrade(self.manifest, quiet=opt.quiet) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 543 | |
Nico Sallembien | 9bb1816 | 2009-12-07 15:38:01 -0800 | [diff] [blame] | 544 | if not opt.local_only: |
Anatol Pomazau | 53d6f4d | 2011-08-25 17:21:47 -0700 | [diff] [blame] | 545 | mp.Sync_NetworkHalf(quiet=opt.quiet, |
| 546 | current_branch_only=opt.current_branch_only) |
Nico Sallembien | 9bb1816 | 2009-12-07 15:38:01 -0800 | [diff] [blame] | 547 | |
| 548 | if mp.HasChanges: |
| 549 | syncbuf = SyncBuffer(mp.config) |
| 550 | mp.Sync_LocalHalf(syncbuf) |
| 551 | if not syncbuf.Finish(): |
| 552 | sys.exit(1) |
| 553 | self.manifest._Unload() |
Shawn O. Pearce | c465796 | 2011-09-26 09:08:01 -0700 | [diff] [blame] | 554 | if opt.jobs is None: |
| 555 | self.jobs = self.manifest.default.sync_j |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 556 | all_projects = self.GetProjects(args, missing_ok=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 557 | |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 558 | self._fetch_times = _FetchTimes(self.manifest) |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 559 | if not opt.local_only: |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 560 | to_fetch = [] |
| 561 | now = time.time() |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 562 | if _ONE_DAY_S <= (now - rp.LastFetch): |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 563 | to_fetch.append(rp) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 564 | to_fetch.extend(all_projects) |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 565 | to_fetch.sort(key=self._fetch_times.Get, reverse=True) |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 566 | |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 567 | self._Fetch(to_fetch, opt) |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 568 | _PostRepoFetch(rp, opt.no_repo_verify) |
Shawn O. Pearce | b1562fa | 2009-04-10 17:04:08 -0700 | [diff] [blame] | 569 | if opt.network_only: |
| 570 | # bail out now; the rest touches the working tree |
| 571 | return |
| 572 | |
Shawn O. Pearce | cd1d7ff | 2009-06-04 16:15:53 -0700 | [diff] [blame] | 573 | if self.manifest.IsMirror: |
| 574 | # bail out now, we have no working tree |
| 575 | return |
| 576 | |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 577 | if self.UpdateProjectList(): |
| 578 | sys.exit(1) |
| 579 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 580 | syncbuf = SyncBuffer(mp.config, |
| 581 | detach_head = opt.detach_head) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 582 | pm = Progress('Syncing work tree', len(all_projects)) |
| 583 | for project in all_projects: |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 584 | pm.update() |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 585 | if project.worktree: |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 586 | project.Sync_LocalHalf(syncbuf) |
Shawn O. Pearce | 68194f4 | 2009-04-10 16:48:52 -0700 | [diff] [blame] | 587 | pm.end() |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 588 | print >>sys.stderr |
| 589 | if not syncbuf.Finish(): |
| 590 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 591 | |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 592 | # If there's a notice that's supposed to print at the end of the sync, print |
| 593 | # it now... |
| 594 | if self.manifest.notice: |
| 595 | print self.manifest.notice |
| 596 | |
Shawn O. Pearce | 80d2ceb | 2012-10-26 12:23:05 -0700 | [diff] [blame] | 597 | def _PostRepoUpgrade(manifest, quiet=False): |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 598 | wrapper = WrapperModule() |
| 599 | if wrapper.NeedSetupGnuPG(): |
Shawn O. Pearce | 80d2ceb | 2012-10-26 12:23:05 -0700 | [diff] [blame] | 600 | wrapper.SetupGnuPG(quiet) |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 601 | for project in manifest.projects.values(): |
| 602 | if project.Exists: |
| 603 | project.PostRepoUpgrade() |
| 604 | |
| 605 | def _PostRepoFetch(rp, no_repo_verify=False, verbose=False): |
| 606 | if rp.HasChanges: |
| 607 | print >>sys.stderr, 'info: A new version of repo is available' |
| 608 | print >>sys.stderr, '' |
| 609 | if no_repo_verify or _VerifyTag(rp): |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 610 | syncbuf = SyncBuffer(rp.config) |
| 611 | rp.Sync_LocalHalf(syncbuf) |
| 612 | if not syncbuf.Finish(): |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 613 | sys.exit(1) |
| 614 | print >>sys.stderr, 'info: Restarting repo with latest version' |
| 615 | raise RepoChangedException(['--repo-upgraded']) |
| 616 | else: |
| 617 | print >>sys.stderr, 'warning: Skipped upgrade to unverified version' |
| 618 | else: |
| 619 | if verbose: |
| 620 | print >>sys.stderr, 'repo version %s is current' % rp.work_git.describe(HEAD) |
| 621 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 622 | def _VerifyTag(project): |
| 623 | gpg_dir = os.path.expanduser('~/.repoconfig/gnupg') |
| 624 | if not os.path.exists(gpg_dir): |
| 625 | print >>sys.stderr,\ |
| 626 | """warning: GnuPG was not available during last "repo init" |
| 627 | warning: Cannot automatically authenticate repo.""" |
| 628 | return True |
| 629 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 630 | try: |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 631 | cur = project.bare_git.describe(project.GetRevisionId()) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 632 | except GitError: |
| 633 | cur = None |
| 634 | |
| 635 | if not cur \ |
| 636 | or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur): |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 637 | rev = project.revisionExpr |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 638 | if rev.startswith(R_HEADS): |
| 639 | rev = rev[len(R_HEADS):] |
| 640 | |
| 641 | print >>sys.stderr |
| 642 | print >>sys.stderr,\ |
| 643 | "warning: project '%s' branch '%s' is not signed" \ |
| 644 | % (project.name, rev) |
| 645 | return False |
| 646 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 647 | env = os.environ.copy() |
| 648 | env['GIT_DIR'] = project.gitdir.encode() |
| 649 | env['GNUPGHOME'] = gpg_dir.encode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 650 | |
| 651 | cmd = [GIT, 'tag', '-v', cur] |
| 652 | proc = subprocess.Popen(cmd, |
| 653 | stdout = subprocess.PIPE, |
| 654 | stderr = subprocess.PIPE, |
| 655 | env = env) |
| 656 | out = proc.stdout.read() |
| 657 | proc.stdout.close() |
| 658 | |
| 659 | err = proc.stderr.read() |
| 660 | proc.stderr.close() |
| 661 | |
| 662 | if proc.wait() != 0: |
| 663 | print >>sys.stderr |
| 664 | print >>sys.stderr, out |
| 665 | print >>sys.stderr, err |
| 666 | print >>sys.stderr |
| 667 | return False |
| 668 | return True |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 669 | |
| 670 | class _FetchTimes(object): |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 671 | _ALPHA = 0.5 |
| 672 | |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 673 | def __init__(self, manifest): |
| 674 | self._path = os.path.join(manifest.repodir, '.repopickle_fetchtimes') |
| 675 | self._times = None |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 676 | self._seen = set() |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 677 | |
| 678 | def Get(self, project): |
| 679 | self._Load() |
| 680 | return self._times.get(project.name, _ONE_DAY_S) |
| 681 | |
| 682 | def Set(self, project, t): |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 683 | self._Load() |
| 684 | name = project.name |
| 685 | old = self._times.get(name, t) |
| 686 | self._seen.add(name) |
| 687 | a = self._ALPHA |
| 688 | self._times[name] = (a*t) + ((1-a) * old) |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 689 | |
| 690 | def _Load(self): |
| 691 | if self._times is None: |
| 692 | try: |
| 693 | f = open(self._path) |
| 694 | except IOError: |
| 695 | self._times = {} |
| 696 | return self._times |
| 697 | try: |
| 698 | try: |
| 699 | self._times = pickle.load(f) |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 700 | except IOError: |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 701 | try: |
| 702 | os.remove(self._path) |
| 703 | except OSError: |
| 704 | pass |
| 705 | self._times = {} |
| 706 | finally: |
| 707 | f.close() |
| 708 | return self._times |
| 709 | |
| 710 | def Save(self): |
| 711 | if self._times is None: |
| 712 | return |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 713 | |
| 714 | to_delete = [] |
| 715 | for name in self._times: |
| 716 | if name not in self._seen: |
| 717 | to_delete.append(name) |
| 718 | for name in to_delete: |
| 719 | del self._times[name] |
| 720 | |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 721 | try: |
| 722 | f = open(self._path, 'wb') |
| 723 | try: |
| 724 | pickle.dump(self._times, f) |
| 725 | except (IOError, OSError, pickle.PickleError): |
| 726 | try: |
| 727 | os.remove(self._path) |
| 728 | except OSError: |
| 729 | pass |
| 730 | finally: |
| 731 | f.close() |