OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import logging | |
6 import os | |
7 import subprocess | |
8 import sys | |
9 | |
10 | |
11 class FetchError(Exception): | |
12 pass | |
13 | |
14 | |
15 class UncleanFilesystemError(FetchError): | |
16 pass | |
17 | |
18 | |
19 class FetchNotAllowedError(FetchError): | |
20 pass | |
21 | |
22 | |
23 def _run_git(checkout_dir, *args): | |
24 if sys.platform.startswith(('win', 'cygwin')): | |
25 cmd = ['git.bat'] | |
iannucci1
2016/05/20 18:04:22
I think we need a full path here, right? Otherwise
Paweł Hajdan Jr.
2016/05/20 21:59:57
This is existing code as you noticed, and point #2
| |
26 else: | |
27 cmd = ['git'] | |
28 | |
29 if checkout_dir is not None: | |
30 cmd += ['-C', checkout_dir] | |
31 cmd += list(args) | |
32 | |
33 logging.info('Running: %s', cmd) | |
34 return subprocess.check_output(cmd) | |
35 | |
36 | |
37 def _cleanup_pyc(path): | |
38 """Removes any .pyc files from |path|'s directory tree. | |
39 This ensures we always use the fresh code. | |
40 """ | |
41 for root, dirs, files in os.walk(path): | |
42 for f in files: | |
43 if f.endswith('.pyc'): | |
44 os.unlink(os.path.join(root, f)) | |
45 | |
46 | |
47 def fetch_from_git(repo, revision, checkout_dir, allow_fetch): | |
48 """Fetches given |repo| at |revision| to |checkout_dir| using git. | |
49 Network operations are performed only if |allow_fetch| is True. | |
50 """ | |
51 logging.info('Freshening repository %s in %s', repo, checkout_dir) | |
52 | |
53 if not os.path.isdir(checkout_dir): | |
54 if allow_fetch: | |
55 _run_git(None, 'clone', '-q', repo, checkout_dir) | |
56 else: | |
57 raise FetchNotAllowedError( | |
58 'need to clone %s but fetch not allowed' % repo) | |
59 elif not os.path.isdir(os.path.join(checkout_dir, '.git')): | |
60 raise UncleanFilesystemError( | |
61 '%s exists but is not a git repo' % checkout_dir) | |
62 | |
63 try: | |
64 _run_git(checkout_dir, 'rev-parse', '-q', '--verify', | |
65 '%s^{commit}' % revision) | |
66 except subprocess.CalledProcessError: | |
67 if allow_fetch: | |
68 _run_git(checkout_dir, 'fetch') | |
69 else: | |
70 raise FetchNotAllowedError( | |
71 'need to fetch %s but fetch not allowed' % repo) | |
72 _run_git(checkout_dir, 'reset', '-q', '--hard', revision) | |
73 _cleanup_pyc(checkout_dir) | |
OLD | NEW |