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 | |
| 16 | import os |
| 17 | import re |
| 18 | import subprocess |
| 19 | import sys |
| 20 | |
| 21 | from git_command import GIT |
| 22 | from command import Command |
| 23 | from error import RepoChangedException, GitError |
| 24 | from project import R_HEADS |
| 25 | |
| 26 | class Sync(Command): |
| 27 | common = True |
| 28 | helpSummary = "Update working tree to the latest revision" |
| 29 | helpUsage = """ |
| 30 | %prog [<project>...] |
| 31 | """ |
| 32 | helpDescription = """ |
| 33 | The '%prog' command synchronizes local project directories |
| 34 | with the remote repositories specified in the manifest. If a local |
| 35 | project does not yet exist, it will clone a new local directory from |
| 36 | the remote repository and set up tracking branches as specified in |
| 37 | the manifest. If the local project already exists, '%prog' |
| 38 | will update the remote branches and rebase any new local changes |
| 39 | on top of the new remote changes. |
| 40 | |
| 41 | '%prog' will synchronize all projects listed at the command |
| 42 | line. Projects can be specified either by name, or by a relative |
| 43 | or absolute path to the project's local directory. If no projects |
| 44 | are specified, '%prog' will synchronize all projects listed in |
| 45 | the manifest. |
| 46 | """ |
| 47 | |
| 48 | def _Options(self, p): |
| 49 | p.add_option('--no-repo-verify', |
| 50 | dest='no_repo_verify', action='store_true', |
| 51 | help='do not verify repo source code') |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 52 | p.add_option('--repo-upgraded', |
| 53 | dest='repo_upgraded', action='store_true', |
| 54 | help='perform additional actions after a repo upgrade') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | |
| 56 | def _Fetch(self, *projects): |
| 57 | fetched = set() |
| 58 | for project in projects: |
| 59 | if project.Sync_NetworkHalf(): |
| 60 | fetched.add(project.gitdir) |
| 61 | else: |
| 62 | print >>sys.stderr, 'error: Cannot fetch %s' % project.name |
| 63 | sys.exit(1) |
| 64 | return fetched |
| 65 | |
| 66 | def Execute(self, opt, args): |
| 67 | rp = self.manifest.repoProject |
| 68 | rp.PreSync() |
| 69 | |
| 70 | mp = self.manifest.manifestProject |
| 71 | mp.PreSync() |
| 72 | |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 73 | if opt.repo_upgraded: |
| 74 | for project in self.manifest.projects.values(): |
| 75 | if project.Exists: |
| 76 | project.PostRepoUpgrade() |
| 77 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | all = self.GetProjects(args, missing_ok=True) |
| 79 | fetched = self._Fetch(rp, mp, *all) |
| 80 | |
| 81 | if rp.HasChanges: |
| 82 | print >>sys.stderr, 'info: A new version of repo is available' |
| 83 | print >>sys.stderr, '' |
| 84 | if opt.no_repo_verify or _VerifyTag(rp): |
| 85 | if not rp.Sync_LocalHalf(): |
| 86 | sys.exit(1) |
| 87 | print >>sys.stderr, 'info: Restarting repo with latest version' |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 88 | raise RepoChangedException(['--repo-upgraded']) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | else: |
| 90 | print >>sys.stderr, 'warning: Skipped upgrade to unverified version' |
| 91 | |
| 92 | if mp.HasChanges: |
| 93 | if not mp.Sync_LocalHalf(): |
| 94 | sys.exit(1) |
| 95 | |
| 96 | self.manifest._Unload() |
| 97 | all = self.GetProjects(args, missing_ok=True) |
| 98 | missing = [] |
| 99 | for project in all: |
| 100 | if project.gitdir not in fetched: |
| 101 | missing.append(project) |
| 102 | self._Fetch(*missing) |
| 103 | |
| 104 | for project in all: |
| 105 | if not project.Sync_LocalHalf(): |
| 106 | sys.exit(1) |
| 107 | |
| 108 | |
| 109 | def _VerifyTag(project): |
| 110 | gpg_dir = os.path.expanduser('~/.repoconfig/gnupg') |
| 111 | if not os.path.exists(gpg_dir): |
| 112 | print >>sys.stderr,\ |
| 113 | """warning: GnuPG was not available during last "repo init" |
| 114 | warning: Cannot automatically authenticate repo.""" |
| 115 | return True |
| 116 | |
| 117 | remote = project.GetRemote(project.remote.name) |
| 118 | ref = remote.ToLocal(project.revision) |
| 119 | |
| 120 | try: |
| 121 | cur = project.bare_git.describe(ref) |
| 122 | except GitError: |
| 123 | cur = None |
| 124 | |
| 125 | if not cur \ |
| 126 | or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur): |
| 127 | rev = project.revision |
| 128 | if rev.startswith(R_HEADS): |
| 129 | rev = rev[len(R_HEADS):] |
| 130 | |
| 131 | print >>sys.stderr |
| 132 | print >>sys.stderr,\ |
| 133 | "warning: project '%s' branch '%s' is not signed" \ |
| 134 | % (project.name, rev) |
| 135 | return False |
| 136 | |
| 137 | env = dict(os.environ) |
| 138 | env['GIT_DIR'] = project.gitdir |
| 139 | env['GNUPGHOME'] = gpg_dir |
| 140 | |
| 141 | cmd = [GIT, 'tag', '-v', cur] |
| 142 | proc = subprocess.Popen(cmd, |
| 143 | stdout = subprocess.PIPE, |
| 144 | stderr = subprocess.PIPE, |
| 145 | env = env) |
| 146 | out = proc.stdout.read() |
| 147 | proc.stdout.close() |
| 148 | |
| 149 | err = proc.stderr.read() |
| 150 | proc.stderr.close() |
| 151 | |
| 152 | if proc.wait() != 0: |
| 153 | print >>sys.stderr |
| 154 | print >>sys.stderr, out |
| 155 | print >>sys.stderr, err |
| 156 | print >>sys.stderr |
| 157 | return False |
| 158 | return True |