yunlian | 994ae3a | 2013-02-19 21:36:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 5 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 6 | |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 7 | import datetime |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 8 | import optparse |
| 9 | import os |
| 10 | import re |
| 11 | import socket |
| 12 | import sys |
| 13 | import tempfile |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 14 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 15 | from automation.clients.helper import perforce |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 16 | from utils import command_executer |
| 17 | from utils import logger |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 18 | from utils import misc |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 19 | |
| 20 | |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 21 | def GetCanonicalMappings(mappings): |
| 22 | canonical_mappings = [] |
| 23 | for mapping in mappings: |
| 24 | remote_path, local_path = mapping.split() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 25 | if local_path.endswith('/') and not remote_path.endswith('/'): |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 26 | local_path = os.path.join(local_path, os.path.basename(remote_path)) |
| 27 | remote_path = remote_path.lstrip('/').split('/', 1)[1] |
| 28 | canonical_mappings.append(perforce.PathMapping(remote_path, local_path)) |
| 29 | return canonical_mappings |
| 30 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 31 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 32 | def SplitMapping(mapping): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 33 | parts = mapping.split() |
| 34 | assert len(parts) <= 2, 'Mapping %s invalid' % mapping |
| 35 | remote_path = parts[0] |
| 36 | if len(parts) == 2: |
| 37 | local_path = parts[1] |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 38 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 39 | local_path = '.' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 40 | return remote_path, local_path |
| 41 | |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 42 | |
| 43 | class Repo(object): |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 44 | def __init__(self, no_create_tmp_dir=False): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 45 | self.repo_type = None |
| 46 | self.address = None |
| 47 | self.mappings = None |
| 48 | self.revision = None |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 49 | self.ignores = ['.gitignore', '.p4config', 'README.google'] |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 50 | if no_create_tmp_dir: |
| 51 | self._root_dir = None |
| 52 | else: |
| 53 | self._root_dir = tempfile.mkdtemp() |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 54 | self._ce = command_executer.GetCommandExecuter() |
| 55 | self._logger = logger.GetLogger() |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 56 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 57 | def PullSources(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 58 | """Pull all sources into an internal dir.""" |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 59 | pass |
| 60 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 61 | def SetupForPush(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 62 | """Setup a repository for pushing later.""" |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 63 | pass |
| 64 | |
asharif | 983c7c4 | 2013-02-16 02:13:01 +0000 | [diff] [blame] | 65 | def PushSources(self, commit_message=None, dry_run=False, message_file=None): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 66 | """Push to the external repo with the commit message.""" |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 67 | pass |
| 68 | |
| 69 | def _RsyncExcludingRepoDirs(self, source_dir, dest_dir): |
asharif | f6ffe50 | 2013-02-15 22:23:12 +0000 | [diff] [blame] | 70 | for f in os.listdir(source_dir): |
| 71 | if f in [".git", ".svn", ".p4config"]: |
| 72 | continue |
| 73 | dest_file = os.path.join(dest_dir, f) |
| 74 | source_file = os.path.join(source_dir, f) |
| 75 | if os.path.exists(dest_file): |
| 76 | command = "rm -rf %s" % dest_file |
| 77 | self._ce.RunCommand(command) |
| 78 | command = "rsync -a %s %s" % (source_file, dest_dir) |
| 79 | self._ce.RunCommand(command) |
| 80 | return 0 |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 81 | |
| 82 | def MapSources(self, dest_dir): |
| 83 | """Copy sources from the internal dir to root_dir.""" |
| 84 | return self._RsyncExcludingRepoDirs(self._root_dir, dest_dir) |
| 85 | |
| 86 | def GetRoot(self): |
| 87 | return self._root_dir |
| 88 | |
| 89 | def CleanupRoot(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 90 | command = 'rm -rf %s' % self._root_dir |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 91 | return self._ce.RunCommand(command) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 92 | |
| 93 | def __str__(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 94 | return '\n'.join(str(s) for s in [self.repo_type, |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 95 | self.address, |
| 96 | self.mappings]) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 97 | |
| 98 | |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 99 | # Note - this type of repo is used only for "readonly", in other words, this |
| 100 | # only serves as a incoming repo. |
| 101 | class FileRepo(Repo): |
| 102 | def __init__(self, address, ignores=None): |
| 103 | Repo.__init__(self, no_create_tmp_dir=True) |
| 104 | self.repo_type = 'file' |
| 105 | self.address = address |
| 106 | self.mappings = None |
| 107 | self.branch = None |
| 108 | self.revision = '{0} (as of "{1}")'.format(address, datetime.datetime.now()) |
| 109 | self.gerrit = None |
| 110 | self._root_dir = self.address |
| 111 | |
| 112 | def CleanupRoot(self): |
| 113 | """Override to prevent deletion.""" |
| 114 | pass |
| 115 | |
| 116 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 117 | class P4Repo(Repo): |
asharif | 6578cf8 | 2013-02-16 02:41:46 +0000 | [diff] [blame] | 118 | def __init__(self, address, mappings, revision=None): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 119 | Repo.__init__(self) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 120 | self.repo_type = 'p4' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 121 | self.address = address |
| 122 | self.mappings = mappings |
asharif | 6578cf8 | 2013-02-16 02:41:46 +0000 | [diff] [blame] | 123 | self.revision = revision |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 124 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 125 | def PullSources(self): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 126 | client_name = socket.gethostname() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 127 | client_name += tempfile.mkstemp()[1].replace('/', '-') |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 128 | mappings = self.mappings |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 129 | p4view = perforce.View('depot2', |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 130 | GetCanonicalMappings(mappings)) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 131 | p4client = perforce.CommandsFactory(self._root_dir, p4view, |
| 132 | name=client_name) |
asharif | 6578cf8 | 2013-02-16 02:41:46 +0000 | [diff] [blame] | 133 | command = p4client.SetupAndDo(p4client.Sync(self.revision)) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 134 | ret = self._ce.RunCommand(command) |
| 135 | assert ret == 0, 'Could not setup client.' |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 136 | command = p4client.InCheckoutDir(p4client.SaveCurrentCLNumber()) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 137 | ret, o, _ = self._ce.RunCommand(command, return_output=True) |
| 138 | assert ret == 0, 'Could not get version from client.' |
| 139 | self.revision = re.search('^\d+$', o.strip(), re.MULTILINE).group(0) |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 140 | command = p4client.InCheckoutDir(p4client.Remove()) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 141 | ret = self._ce.RunCommand(command) |
| 142 | assert ret == 0, 'Could not delete client.' |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 143 | return 0 |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 144 | |
| 145 | |
| 146 | class SvnRepo(Repo): |
| 147 | def __init__(self, address, mappings): |
| 148 | Repo.__init__(self) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 149 | self.repo_type = 'svn' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 150 | self.address = address |
| 151 | self.mappings = mappings |
| 152 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 153 | def PullSources(self): |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 154 | with misc.WorkingDirectory(self._root_dir): |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 155 | for mapping in self.mappings: |
| 156 | remote_path, local_path = SplitMapping(mapping) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 157 | command = 'svn co %s/%s %s' % (self.address, remote_path, local_path) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 158 | ret = self._ce.RunCommand(command) |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 159 | if ret: return ret |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 160 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 161 | self.revision = '' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 162 | for mapping in self.mappings: |
| 163 | remote_path, local_path = SplitMapping(mapping) |
asharif | 39f7cb2 | 2013-02-19 19:35:16 +0000 | [diff] [blame] | 164 | command = 'cd %s && svnversion -c .' % (local_path) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 165 | ret, o, _ = self._ce.RunCommand(command, return_output=True) |
asharif | 39f7cb2 | 2013-02-19 19:35:16 +0000 | [diff] [blame] | 166 | self.revision += o.strip().split(":")[-1] |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 167 | if ret: return ret |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 168 | return 0 |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | class GitRepo(Repo): |
asharif | 2aeeaca | 2013-02-16 02:11:04 +0000 | [diff] [blame] | 172 | def __init__(self, address, branch, mappings=None, ignores=None, gerrit=None): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 173 | Repo.__init__(self) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 174 | self.repo_type = 'git' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 175 | self.address = address |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 176 | self.branch = branch or 'master' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 177 | if ignores: |
| 178 | self.ignores += ignores |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 179 | self.mappings = mappings |
asharif | 2aeeaca | 2013-02-16 02:11:04 +0000 | [diff] [blame] | 180 | self.gerrit = gerrit |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 181 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 182 | def _CloneSources(self): |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 183 | with misc.WorkingDirectory(self._root_dir): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 184 | command = 'git clone %s .' % (self.address) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 185 | return self._ce.RunCommand(command) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 186 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 187 | def PullSources(self): |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 188 | with misc.WorkingDirectory(self._root_dir): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 189 | ret = self._CloneSources() |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 190 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 191 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 192 | command = 'git checkout %s' % self.branch |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 193 | ret = self._ce.RunCommand(command) |
| 194 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 195 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 196 | command = 'git describe --always' |
| 197 | ret, o, _ = self._ce.RunCommand(command, return_output=True) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 198 | self.revision = o.strip() |
| 199 | return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 200 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 201 | def SetupForPush(self): |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 202 | with misc.WorkingDirectory(self._root_dir): |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 203 | ret = self._CloneSources() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 204 | logger.GetLogger().LogFatalIf(ret, 'Could not clone git repo %s.' % |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 205 | self.address) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 206 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 207 | command = 'git branch -a | grep -wq %s' % self.branch |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 208 | ret = self._ce.RunCommand(command) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 209 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 210 | if ret == 0: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 211 | if self.branch != 'master': |
| 212 | command = ('git branch --track %s remotes/origin/%s' % |
| 213 | (self.branch, self.branch)) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 214 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 215 | command = 'pwd' |
| 216 | command += '&& git checkout %s' % self.branch |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 217 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 218 | command = 'git symbolic-ref HEAD refs/heads/%s' % self.branch |
| 219 | command += '&& rm -rf *' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 220 | ret = self._ce.RunCommand(command) |
| 221 | return ret |
| 222 | |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 223 | def CommitLocally(self, commit_message=None, message_file=None): |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 224 | with misc.WorkingDirectory(self._root_dir): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 225 | command = 'pwd' |
| 226 | for ignore in self.ignores: |
| 227 | command += '&& echo \'%s\' >> .git/info/exclude' % ignore |
| 228 | command += '&& git add -Av .' |
asharif | 983c7c4 | 2013-02-16 02:13:01 +0000 | [diff] [blame] | 229 | if message_file: |
| 230 | message_arg = '-F %s' % message_file |
| 231 | elif commit_message: |
| 232 | message_arg = '-m \'%s\'' % commit_message |
| 233 | else: |
| 234 | raise Exception("No commit message given!") |
| 235 | command += '&& git commit -v %s' % message_arg |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 236 | return self._ce.RunCommand(command) |
| 237 | |
| 238 | def PushSources(self, commit_message=None, dry_run=False, message_file=None): |
| 239 | ret = self.CommitLocally(commit_message, message_file) |
| 240 | if ret: return ret |
| 241 | push_args = '' |
| 242 | if dry_run: |
| 243 | push_args += ' -n ' |
| 244 | with misc.WorkingDirectory(self._root_dir): |
asharif | 2aeeaca | 2013-02-16 02:11:04 +0000 | [diff] [blame] | 245 | if self.gerrit: |
| 246 | label = 'somelabel' |
| 247 | command = 'git remote add %s %s' % (label, self.address) |
| 248 | command += ('&& git push %s %s HEAD:refs/for/master' % |
| 249 | (push_args,label)) |
| 250 | else: |
| 251 | command = 'git push -v %s origin %s:%s' % (push_args, self.branch, |
| 252 | self.branch) |
| 253 | ret = self._ce.RunCommand(command) |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 254 | return ret |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 255 | |
| 256 | def MapSources(self, root_dir): |
| 257 | if not self.mappings: |
asharif | f6ffe50 | 2013-02-15 22:23:12 +0000 | [diff] [blame] | 258 | self._RsyncExcludingRepoDirs(self._root_dir, root_dir) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 259 | return |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 260 | with misc.WorkingDirectory(self._root_dir): |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 261 | for mapping in self.mappings: |
| 262 | remote_path, local_path = SplitMapping(mapping) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 263 | remote_path.rstrip('...') |
| 264 | local_path.rstrip('...') |
asharif | f6ffe50 | 2013-02-15 22:23:12 +0000 | [diff] [blame] | 265 | full_local_path = os.path.join(root_dir, local_path) |
| 266 | ret = self._RsyncExcludingRepoDirs(remote_path, full_local_path) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 267 | if ret: return ret |
| 268 | return 0 |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 269 | |
| 270 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 271 | class RepoReader(object): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 272 | def __init__(self, filename): |
| 273 | self.filename = filename |
| 274 | self.main_dict = {} |
| 275 | self.input_repos = [] |
| 276 | self.output_repos = [] |
| 277 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 278 | def ParseFile(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 279 | with open(self.filename) as f: |
| 280 | self.main_dict = eval(f.read()) |
| 281 | self.CreateReposFromDict(self.main_dict) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 282 | return [self.input_repos, self.output_repos] |
| 283 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 284 | def CreateReposFromDict(self, main_dict): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 285 | for key, repo_list in main_dict.items(): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 286 | for repo_dict in repo_list: |
| 287 | repo = self.CreateRepoFromDict(repo_dict) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 288 | if key == 'input': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 289 | self.input_repos.append(repo) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 290 | elif key == 'output': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 291 | self.output_repos.append(repo) |
| 292 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 293 | logger.GetLogger().LogFatal('Unknown key: %s found' % key) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 294 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 295 | def CreateRepoFromDict(self, repo_dict): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 296 | repo_type = repo_dict.get('type', None) |
| 297 | repo_address = repo_dict.get('address', None) |
| 298 | repo_mappings = repo_dict.get('mappings', None) |
| 299 | repo_ignores = repo_dict.get('ignores', None) |
| 300 | repo_branch = repo_dict.get('branch', None) |
asharif | 2aeeaca | 2013-02-16 02:11:04 +0000 | [diff] [blame] | 301 | gerrit = repo_dict.get('gerrit', None) |
asharif | 6578cf8 | 2013-02-16 02:41:46 +0000 | [diff] [blame] | 302 | revision = repo_dict.get('revision', None) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 303 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 304 | if repo_type == 'p4': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 305 | repo = P4Repo(repo_address, |
asharif | 6578cf8 | 2013-02-16 02:41:46 +0000 | [diff] [blame] | 306 | repo_mappings, |
| 307 | revision=revision) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 308 | elif repo_type == 'svn': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 309 | repo = SvnRepo(repo_address, |
| 310 | repo_mappings) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 311 | elif repo_type == 'git': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 312 | repo = GitRepo(repo_address, |
| 313 | repo_branch, |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 314 | mappings=repo_mappings, |
asharif | 2aeeaca | 2013-02-16 02:11:04 +0000 | [diff] [blame] | 315 | ignores=repo_ignores, |
| 316 | gerrit=gerrit) |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 317 | elif repo_type == 'file': |
| 318 | repo = FileRepo(repo_address) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 319 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 320 | logger.GetLogger().LogFatal('Unknown repo type: %s' % repo_type) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 321 | return repo |
| 322 | |
| 323 | |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 324 | @logger.HandleUncaughtExceptions |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 325 | def Main(argv): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 326 | parser = optparse.OptionParser() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 327 | parser.add_option('-i', |
| 328 | '--input_file', |
| 329 | dest='input_file', |
| 330 | help='The input file that contains repo descriptions.') |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 331 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 332 | parser.add_option('-n', |
| 333 | '--dry_run', |
| 334 | dest='dry_run', |
| 335 | action='store_true', |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 336 | default=False, |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 337 | help='Do a dry run of the push.') |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 338 | |
asharif | 983c7c4 | 2013-02-16 02:13:01 +0000 | [diff] [blame] | 339 | parser.add_option('-F', |
| 340 | '--message_file', |
| 341 | dest='message_file', |
| 342 | default=None, |
| 343 | help='Use contents of the log file as the commit message.') |
| 344 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 345 | options = parser.parse_args(argv)[0] |
| 346 | if not options.input_file: |
| 347 | parser.print_help() |
| 348 | return 1 |
| 349 | rr = RepoReader(options.input_file) |
| 350 | [input_repos, output_repos] = rr.ParseFile() |
| 351 | |
Han Shen | eb77c44 | 2013-03-18 13:53:52 -0700 | [diff] [blame] | 352 | # Make sure FileRepo is not used as output destination. |
| 353 | for output_repo in output_repos: |
| 354 | if output_repo.repo_type == 'file': |
| 355 | logger.GetLogger().LogFatal( |
| 356 | 'FileRepo is only supported as an input repo.') |
| 357 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 358 | for output_repo in output_repos: |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 359 | ret = output_repo.SetupForPush() |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 360 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 361 | |
| 362 | input_revisions = [] |
| 363 | for input_repo in input_repos: |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 364 | ret = input_repo.PullSources() |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 365 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 366 | input_revisions.append(input_repo.revision) |
| 367 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 368 | for input_repo in input_repos: |
| 369 | for output_repo in output_repos: |
| 370 | ret = input_repo.MapSources(output_repo.GetRoot()) |
| 371 | if ret: return ret |
| 372 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 373 | commit_message = 'Synced repos to: %s' % ','.join(input_revisions) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 374 | for output_repo in output_repos: |
asharif | 983c7c4 | 2013-02-16 02:13:01 +0000 | [diff] [blame] | 375 | ret = output_repo.PushSources(commit_message=commit_message, |
| 376 | dry_run=options.dry_run, |
| 377 | message_file=options.message_file) |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 378 | if ret: return ret |
| 379 | |
| 380 | if not options.dry_run: |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 381 | for output_repo in output_repos: |
| 382 | output_repo.CleanupRoot() |
raymes | 8078084 | 2013-02-15 21:59:10 +0000 | [diff] [blame] | 383 | for input_repo in input_repos: |
| 384 | input_repo.CleanupRoot() |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 385 | |
| 386 | return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 387 | |
| 388 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 389 | if __name__ == '__main__': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 390 | retval = Main(sys.argv) |
| 391 | sys.exit(retval) |