The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import os |
| 17 | import sys |
| 18 | import subprocess |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 19 | import tempfile |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 20 | from signal import SIGTERM |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | from error import GitError |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 22 | from trace import REPO_TRACE, IsTrace, Trace |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
| 24 | GIT = 'git' |
| 25 | MIN_GIT_VERSION = (1, 5, 4) |
| 26 | GIT_DIR = 'GIT_DIR' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | |
| 28 | LAST_GITDIR = None |
| 29 | LAST_CWD = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 31 | _ssh_proxy_path = None |
| 32 | _ssh_sock_path = None |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 33 | _ssh_clients = [] |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 34 | |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 35 | def ssh_sock(create=True): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 36 | global _ssh_sock_path |
| 37 | if _ssh_sock_path is None: |
| 38 | if not create: |
| 39 | return None |
Shawn O. Pearce | d63bbf4 | 2009-04-21 08:05:27 -0700 | [diff] [blame] | 40 | dir = '/tmp' |
| 41 | if not os.path.exists(dir): |
| 42 | dir = tempfile.gettempdir() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 43 | _ssh_sock_path = os.path.join( |
Shawn O. Pearce | d63bbf4 | 2009-04-21 08:05:27 -0700 | [diff] [blame] | 44 | tempfile.mkdtemp('', 'ssh-', dir), |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 45 | 'master-%r@%h:%p') |
| 46 | return _ssh_sock_path |
| 47 | |
| 48 | def _ssh_proxy(): |
| 49 | global _ssh_proxy_path |
| 50 | if _ssh_proxy_path is None: |
| 51 | _ssh_proxy_path = os.path.join( |
| 52 | os.path.dirname(__file__), |
| 53 | 'git_ssh') |
| 54 | return _ssh_proxy_path |
| 55 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 56 | def _add_ssh_client(p): |
| 57 | _ssh_clients.append(p) |
| 58 | |
| 59 | def _remove_ssh_client(p): |
| 60 | try: |
| 61 | _ssh_clients.remove(p) |
| 62 | except ValueError: |
| 63 | pass |
| 64 | |
| 65 | def terminate_ssh_clients(): |
| 66 | global _ssh_clients |
| 67 | for p in _ssh_clients: |
| 68 | try: |
| 69 | os.kill(p.pid, SIGTERM) |
| 70 | p.wait() |
| 71 | except OSError: |
| 72 | pass |
| 73 | _ssh_clients = [] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | |
| 75 | class _GitCall(object): |
| 76 | def version(self): |
| 77 | p = GitCommand(None, ['--version'], capture_stdout=True) |
| 78 | if p.Wait() == 0: |
| 79 | return p.stdout |
| 80 | return None |
| 81 | |
| 82 | def __getattr__(self, name): |
| 83 | name = name.replace('_','-') |
| 84 | def fun(*cmdv): |
| 85 | command = [name] |
| 86 | command.extend(cmdv) |
| 87 | return GitCommand(None, command).Wait() == 0 |
| 88 | return fun |
| 89 | git = _GitCall() |
| 90 | |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 91 | _git_version = None |
| 92 | |
| 93 | def git_require(min_version, fail=False): |
| 94 | global _git_version |
| 95 | |
| 96 | if _git_version is None: |
| 97 | ver_str = git.version() |
| 98 | if ver_str.startswith('git version '): |
| 99 | _git_version = tuple( |
| 100 | map(lambda x: int(x), |
| 101 | ver_str[len('git version '):].strip().split('.')[0:3] |
| 102 | )) |
| 103 | else: |
| 104 | print >>sys.stderr, 'fatal: "%s" unsupported' % ver_str |
| 105 | sys.exit(1) |
| 106 | |
| 107 | if min_version <= _git_version: |
| 108 | return True |
| 109 | if fail: |
| 110 | need = '.'.join(map(lambda x: str(x), min_version)) |
| 111 | print >>sys.stderr, 'fatal: git %s or later required' % need |
| 112 | sys.exit(1) |
| 113 | return False |
| 114 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | class GitCommand(object): |
| 116 | def __init__(self, |
| 117 | project, |
| 118 | cmdv, |
| 119 | bare = False, |
| 120 | provide_stdin = False, |
| 121 | capture_stdout = False, |
| 122 | capture_stderr = False, |
| 123 | disable_editor = False, |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 124 | ssh_proxy = False, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 125 | cwd = None, |
| 126 | gitdir = None): |
| 127 | env = dict(os.environ) |
| 128 | |
| 129 | for e in [REPO_TRACE, |
| 130 | GIT_DIR, |
| 131 | 'GIT_ALTERNATE_OBJECT_DIRECTORIES', |
| 132 | 'GIT_OBJECT_DIRECTORY', |
| 133 | 'GIT_WORK_TREE', |
| 134 | 'GIT_GRAFT_FILE', |
| 135 | 'GIT_INDEX_FILE']: |
| 136 | if e in env: |
| 137 | del env[e] |
| 138 | |
| 139 | if disable_editor: |
| 140 | env['GIT_EDITOR'] = ':' |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 141 | if ssh_proxy: |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 142 | env['REPO_SSH_SOCK'] = ssh_sock() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 143 | env['GIT_SSH'] = _ssh_proxy() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 144 | |
| 145 | if project: |
| 146 | if not cwd: |
| 147 | cwd = project.worktree |
| 148 | if not gitdir: |
| 149 | gitdir = project.gitdir |
| 150 | |
| 151 | command = [GIT] |
| 152 | if bare: |
| 153 | if gitdir: |
| 154 | env[GIT_DIR] = gitdir |
| 155 | cwd = None |
| 156 | command.extend(cmdv) |
| 157 | |
| 158 | if provide_stdin: |
| 159 | stdin = subprocess.PIPE |
| 160 | else: |
| 161 | stdin = None |
| 162 | |
| 163 | if capture_stdout: |
| 164 | stdout = subprocess.PIPE |
| 165 | else: |
| 166 | stdout = None |
| 167 | |
| 168 | if capture_stderr: |
| 169 | stderr = subprocess.PIPE |
| 170 | else: |
| 171 | stderr = None |
| 172 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 173 | if IsTrace(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 174 | global LAST_CWD |
| 175 | global LAST_GITDIR |
| 176 | |
| 177 | dbg = '' |
| 178 | |
| 179 | if cwd and LAST_CWD != cwd: |
| 180 | if LAST_GITDIR or LAST_CWD: |
| 181 | dbg += '\n' |
| 182 | dbg += ': cd %s\n' % cwd |
| 183 | LAST_CWD = cwd |
| 184 | |
| 185 | if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]: |
| 186 | if LAST_GITDIR or LAST_CWD: |
| 187 | dbg += '\n' |
| 188 | dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR] |
| 189 | LAST_GITDIR = env[GIT_DIR] |
| 190 | |
| 191 | dbg += ': ' |
| 192 | dbg += ' '.join(command) |
| 193 | if stdin == subprocess.PIPE: |
| 194 | dbg += ' 0<|' |
| 195 | if stdout == subprocess.PIPE: |
| 196 | dbg += ' 1>|' |
| 197 | if stderr == subprocess.PIPE: |
| 198 | dbg += ' 2>|' |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 199 | Trace('%s', dbg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | |
| 201 | try: |
| 202 | p = subprocess.Popen(command, |
| 203 | cwd = cwd, |
| 204 | env = env, |
| 205 | stdin = stdin, |
| 206 | stdout = stdout, |
| 207 | stderr = stderr) |
| 208 | except Exception, e: |
| 209 | raise GitError('%s: %s' % (command[1], e)) |
| 210 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 211 | if ssh_proxy: |
| 212 | _add_ssh_client(p) |
| 213 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | self.process = p |
| 215 | self.stdin = p.stdin |
| 216 | |
| 217 | def Wait(self): |
| 218 | p = self.process |
| 219 | |
| 220 | if p.stdin: |
| 221 | p.stdin.close() |
| 222 | self.stdin = None |
| 223 | |
| 224 | if p.stdout: |
| 225 | self.stdout = p.stdout.read() |
| 226 | p.stdout.close() |
| 227 | else: |
| 228 | p.stdout = None |
| 229 | |
| 230 | if p.stderr: |
| 231 | self.stderr = p.stderr.read() |
| 232 | p.stderr.close() |
| 233 | else: |
| 234 | p.stderr = None |
| 235 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 236 | try: |
| 237 | rc = p.wait() |
| 238 | finally: |
| 239 | _remove_ssh_client(p) |
| 240 | return rc |