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