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