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