asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.6 |
| 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 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 7 | import optparse |
| 8 | import os |
| 9 | import re |
| 10 | import socket |
| 11 | import sys |
| 12 | import tempfile |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 13 | from automation.clients.helper import perforce |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 14 | from utils import command_executer |
| 15 | from utils import logger |
| 16 | from utils import utils |
| 17 | |
| 18 | |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 19 | def GetCanonicalMappings(mappings): |
| 20 | canonical_mappings = [] |
| 21 | for mapping in mappings: |
| 22 | remote_path, local_path = mapping.split() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 23 | if local_path.endswith('/') and not remote_path.endswith('/'): |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 24 | local_path = os.path.join(local_path, os.path.basename(remote_path)) |
| 25 | remote_path = remote_path.lstrip('/').split('/', 1)[1] |
| 26 | canonical_mappings.append(perforce.PathMapping(remote_path, local_path)) |
| 27 | return canonical_mappings |
| 28 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 29 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 30 | def SplitMapping(mapping): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 31 | parts = mapping.split() |
| 32 | assert len(parts) <= 2, 'Mapping %s invalid' % mapping |
| 33 | remote_path = parts[0] |
| 34 | if len(parts) == 2: |
| 35 | local_path = parts[1] |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 36 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 37 | local_path = '.' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 38 | return remote_path, local_path |
| 39 | |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 40 | |
| 41 | class Repo(object): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 42 | def __init__(self): |
| 43 | self.repo_type = None |
| 44 | self.address = None |
| 45 | self.mappings = None |
| 46 | self.revision = None |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 47 | self.ignores = ['.gitignore', '.p4config', 'README.google'] |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 48 | self._root_dir = tempfile.mkdtemp() |
| 49 | self._ce = command_executer.GetCommandExecuter() |
| 50 | self._logger = logger.GetLogger() |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 51 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 52 | def PullSources(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 53 | """Pull all sources into an internal dir.""" |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 54 | pass |
| 55 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 56 | def SetupForPush(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 57 | """Setup a repository for pushing later.""" |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 58 | pass |
| 59 | |
| 60 | def PushSources(self, commit_message, dry_run=False): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 61 | """Push to the external repo with the commit message.""" |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 62 | pass |
| 63 | |
| 64 | def _RsyncExcludingRepoDirs(self, source_dir, dest_dir): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 65 | command = 'rsync -a --exclude=.git --exclude=.svn %s/ %s/' % (source_dir, |
| 66 | dest_dir) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 67 | return self._ce.RunCommand(command) |
| 68 | |
| 69 | def MapSources(self, dest_dir): |
| 70 | """Copy sources from the internal dir to root_dir.""" |
| 71 | return self._RsyncExcludingRepoDirs(self._root_dir, dest_dir) |
| 72 | |
| 73 | def GetRoot(self): |
| 74 | return self._root_dir |
| 75 | |
| 76 | def CleanupRoot(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 77 | command = 'rm -rf %s' % self._root_dir |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 78 | return self._ce.RunCommand(command) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 79 | |
| 80 | def __str__(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 81 | return '\n'.join(str(s) for s in [self.repo_type, |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 82 | self.address, |
| 83 | self.mappings]) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | class P4Repo(Repo): |
| 87 | def __init__(self, address, mappings): |
| 88 | Repo.__init__(self) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 89 | self.repo_type = 'p4' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 90 | self.address = address |
| 91 | self.mappings = mappings |
| 92 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 93 | def PullSources(self): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 94 | client_name = socket.gethostname() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 95 | client_name += tempfile.mkstemp()[1].replace('/', '-') |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 96 | mappings = self.mappings |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 97 | p4view = perforce.View('depot2', |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 98 | GetCanonicalMappings(mappings)) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 99 | p4client = perforce.CommandsFactory(self._root_dir, p4view, |
| 100 | name=client_name) |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 101 | command = p4client.SetupAndDo(p4client.Sync()) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 102 | ret = self._ce.RunCommand(command) |
| 103 | assert ret == 0, 'Could not setup client.' |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 104 | command = p4client.InCheckoutDir(p4client.SaveCurrentCLNumber()) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 105 | ret, o, _ = self._ce.RunCommand(command, return_output=True) |
| 106 | assert ret == 0, 'Could not get version from client.' |
| 107 | self.revision = re.search('^\d+$', o.strip(), re.MULTILINE).group(0) |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 108 | command = p4client.InCheckoutDir(p4client.Remove()) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 109 | ret = self._ce.RunCommand(command) |
| 110 | assert ret == 0, 'Could not delete client.' |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 111 | return 0 |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | class SvnRepo(Repo): |
| 115 | def __init__(self, address, mappings): |
| 116 | Repo.__init__(self) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 117 | self.repo_type = 'svn' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 118 | self.address = address |
| 119 | self.mappings = mappings |
| 120 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 121 | def PullSources(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 122 | with utils.WorkingDirectory(self._root_dir): |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 123 | for mapping in self.mappings: |
| 124 | remote_path, local_path = SplitMapping(mapping) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 125 | command = 'svn co %s/%s %s' % (self.address, remote_path, local_path) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 126 | ret = self._ce.RunCommand(command) |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 127 | if ret: return ret |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 128 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 129 | self.revision = '' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 130 | for mapping in self.mappings: |
| 131 | remote_path, local_path = SplitMapping(mapping) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 132 | command = 'cd %s && svnversion .' % (local_path) |
| 133 | ret, o, _ = self._ce.RunCommand(command, return_output=True) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 134 | self.revision += o.strip() |
| 135 | if ret: return ret |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 136 | return 0 |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | class GitRepo(Repo): |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 140 | def __init__(self, address, branch, mappings=None, ignores=None): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 141 | Repo.__init__(self) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 142 | self.repo_type = 'git' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 143 | self.address = address |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 144 | self.branch = branch or 'master' |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 145 | if ignores: |
| 146 | self.ignores += ignores |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 147 | self.mappings = mappings |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 148 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 149 | def _CloneSources(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 150 | with utils.WorkingDirectory(self._root_dir): |
| 151 | command = 'git clone %s .' % (self.address) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 152 | return self._ce.RunCommand(command) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 153 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 154 | def PullSources(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 155 | with utils.WorkingDirectory(self._root_dir): |
| 156 | ret = self._CloneSources() |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 157 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 158 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 159 | command = 'git checkout %s' % self.branch |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 160 | ret = self._ce.RunCommand(command) |
| 161 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 162 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 163 | command = 'git describe --always' |
| 164 | ret, o, _ = self._ce.RunCommand(command, return_output=True) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 165 | self.revision = o.strip() |
| 166 | return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 167 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 168 | def SetupForPush(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 169 | with utils.WorkingDirectory(self._root_dir): |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 170 | ret = self._CloneSources() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 171 | logger.GetLogger().LogFatalIf(ret, 'Could not clone git repo %s.' % |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 172 | self.address) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 173 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 174 | command = 'git branch -a | grep -wq %s' % self.branch |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 175 | ret = self._ce.RunCommand(command) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 176 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 177 | if ret == 0: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 178 | if self.branch != 'master': |
| 179 | command = ('git branch --track %s remotes/origin/%s' % |
| 180 | (self.branch, self.branch)) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 181 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 182 | command = 'pwd' |
| 183 | command += '&& git checkout %s' % self.branch |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 184 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 185 | command = 'git symbolic-ref HEAD refs/heads/%s' % self.branch |
| 186 | command += '&& rm -rf *' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 187 | ret = self._ce.RunCommand(command) |
| 188 | return ret |
| 189 | |
| 190 | def PushSources(self, commit_message, dry_run=False): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 191 | with utils.WorkingDirectory(self._root_dir): |
| 192 | push_args = '' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 193 | if dry_run: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 194 | push_args += ' -n ' |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 195 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 196 | command = 'pwd' |
| 197 | for ignore in self.ignores: |
| 198 | command += '&& echo \'%s\' >> .git/info/exclude' % ignore |
| 199 | command += '&& git add -Av .' |
| 200 | command += '&& git commit -v -m \'%s\'' % commit_message |
| 201 | command += '; git push -v %s origin %s:%s' % (push_args, self.branch, |
| 202 | self.branch) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 203 | return self._ce.RunCommand(command) |
| 204 | |
| 205 | def MapSources(self, root_dir): |
| 206 | if not self.mappings: |
| 207 | return |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 208 | with utils.WorkingDirectory(self._root_dir): |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 209 | for mapping in self.mappings: |
| 210 | remote_path, local_path = SplitMapping(mapping) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 211 | remote_path.rstrip('...') |
| 212 | local_path.rstrip('...') |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 213 | ret = self._RsyncExcludingRepoDirs(remote_path, |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 214 | os.path.join(root_dir, local_path)) |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 215 | if ret: return ret |
| 216 | return 0 |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 217 | |
| 218 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 219 | class RepoReader(object): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 220 | def __init__(self, filename): |
| 221 | self.filename = filename |
| 222 | self.main_dict = {} |
| 223 | self.input_repos = [] |
| 224 | self.output_repos = [] |
| 225 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 226 | def ParseFile(self): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 227 | with open(self.filename) as f: |
| 228 | self.main_dict = eval(f.read()) |
| 229 | self.CreateReposFromDict(self.main_dict) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 230 | return [self.input_repos, self.output_repos] |
| 231 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 232 | def CreateReposFromDict(self, main_dict): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 233 | for key, repo_list in main_dict.items(): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 234 | for repo_dict in repo_list: |
| 235 | repo = self.CreateRepoFromDict(repo_dict) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 236 | if key == 'input': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 237 | self.input_repos.append(repo) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 238 | elif key == 'output': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 239 | self.output_repos.append(repo) |
| 240 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 241 | logger.GetLogger().LogFatal('Unknown key: %s found' % key) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 242 | |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 243 | def CreateRepoFromDict(self, repo_dict): |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 244 | repo_type = repo_dict.get('type', None) |
| 245 | repo_address = repo_dict.get('address', None) |
| 246 | repo_mappings = repo_dict.get('mappings', None) |
| 247 | repo_ignores = repo_dict.get('ignores', None) |
| 248 | repo_branch = repo_dict.get('branch', None) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 249 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 250 | if repo_type == 'p4': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 251 | repo = P4Repo(repo_address, |
| 252 | repo_mappings) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 253 | elif repo_type == 'svn': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 254 | repo = SvnRepo(repo_address, |
| 255 | repo_mappings) |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 256 | elif repo_type == 'git': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 257 | repo = GitRepo(repo_address, |
| 258 | repo_branch, |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 259 | mappings=repo_mappings, |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 260 | ignores=repo_ignores) |
| 261 | else: |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 262 | logger.GetLogger().LogFatal('Unknown repo type: %s' % repo_type) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 263 | return repo |
| 264 | |
| 265 | |
asharif | 1b92e73 | 2013-02-15 20:51:38 +0000 | [diff] [blame] | 266 | @logger.HandleUncaughtExceptions |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 267 | def Main(argv): |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 268 | parser = optparse.OptionParser() |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 269 | parser.add_option('-i', |
| 270 | '--input_file', |
| 271 | dest='input_file', |
| 272 | help='The input file that contains repo descriptions.') |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 273 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 274 | parser.add_option('-n', |
| 275 | '--dry_run', |
| 276 | dest='dry_run', |
| 277 | action='store_true', |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 278 | default=False, |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 279 | help='Do a dry run of the push.') |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 280 | |
| 281 | options = parser.parse_args(argv)[0] |
| 282 | if not options.input_file: |
| 283 | parser.print_help() |
| 284 | return 1 |
| 285 | rr = RepoReader(options.input_file) |
| 286 | [input_repos, output_repos] = rr.ParseFile() |
| 287 | |
| 288 | for output_repo in output_repos: |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 289 | ret = output_repo.SetupForPush() |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 290 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 291 | |
| 292 | input_revisions = [] |
| 293 | for input_repo in input_repos: |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 294 | ret = input_repo.PullSources() |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 295 | if ret: return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 296 | input_revisions.append(input_repo.revision) |
| 297 | |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 298 | for input_repo in input_repos: |
| 299 | for output_repo in output_repos: |
| 300 | ret = input_repo.MapSources(output_repo.GetRoot()) |
| 301 | if ret: return ret |
| 302 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 303 | commit_message = 'Synced repos to: %s' % ','.join(input_revisions) |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 304 | for output_repo in output_repos: |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 305 | ret = output_repo.PushSources(commit_message, dry_run=options.dry_run) |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 306 | if ret: return ret |
| 307 | |
| 308 | if not options.dry_run: |
asharif | 56d78bc | 2013-02-15 21:15:08 +0000 | [diff] [blame] | 309 | for output_repo in output_repos: |
| 310 | output_repo.CleanupRoot() |
raymes | 8078084 | 2013-02-15 21:59:10 +0000 | [diff] [blame^] | 311 | for input_repo in input_repos: |
| 312 | input_repo.CleanupRoot() |
asharif | cdbacaf | 2013-02-15 20:09:23 +0000 | [diff] [blame] | 313 | |
| 314 | return ret |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 315 | |
| 316 | |
asharif | 0ea8958 | 2013-02-15 21:15:11 +0000 | [diff] [blame] | 317 | if __name__ == '__main__': |
asharif | ec9c624 | 2013-02-15 19:56:06 +0000 | [diff] [blame] | 318 | retval = Main(sys.argv) |
| 319 | sys.exit(retval) |