OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
nodir
2016/05/20 23:03:49
is this file about git? it seems so. Maybe rename
Paweł Hajdan Jr.
2016/05/24 16:58:06
This won't be necessarily exclusive to git. For ex
nodir
2016/05/24 17:46:31
Acknowledged.
| |
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 sys | |
8 | |
9 from .third_party import subprocess42 | |
10 | |
11 | |
12 class FetchError(Exception): | |
13 pass | |
14 | |
15 | |
16 class UncleanFilesystemError(FetchError): | |
17 pass | |
18 | |
19 | |
20 class FetchNotAllowedError(FetchError): | |
21 pass | |
22 | |
23 | |
24 def _run_git(checkout_dir, *args): | |
25 if sys.platform.startswith(('win', 'cygwin')): | |
26 cmd = ['git.bat'] | |
27 else: | |
28 cmd = ['git'] | |
29 | |
30 if checkout_dir is not None: | |
31 cmd += ['-C', checkout_dir] | |
32 cmd += list(args) | |
33 | |
34 logging.info('Running: %s', cmd) | |
35 return subprocess42.check_output(cmd) | |
36 | |
37 | |
38 def _cleanup_pyc(path): | |
nodir
2016/05/20 23:03:49
why cleanup_pyc is in a file related to fetching?
Paweł Hajdan Jr.
2016/05/24 16:58:06
Good point. Moved back.
| |
39 """Removes any .pyc files from |path|'s directory tree. | |
40 This ensures we always use the fresh code. | |
nodir
2016/05/20 23:03:49
there should be a blank line between first line of
Paweł Hajdan Jr.
2016/05/24 16:58:06
Done.
| |
41 """ | |
42 for root, dirs, files in os.walk(path): | |
43 for f in files: | |
44 if f.endswith('.pyc'): | |
45 os.unlink(os.path.join(root, f)) | |
46 | |
47 | |
48 def fetch_from_git(repo, revision, checkout_dir, allow_fetch): | |
nodir
2016/05/20 23:03:49
it is strange to see "allow_fetch" parameter in a
Paweł Hajdan Jr.
2016/05/24 16:58:06
Yeah, I was also thinking it's somewhat ugly but m
| |
49 """Fetches given |repo| at |revision| to |checkout_dir| using git. | |
50 Network operations are performed only if |allow_fetch| is True. | |
nodir
2016/05/20 23:03:49
here too
Paweł Hajdan Jr.
2016/05/24 16:58:06
Done.
| |
51 """ | |
52 logging.info('Freshening repository %s in %s', repo, checkout_dir) | |
53 | |
54 if not os.path.isdir(checkout_dir): | |
55 if allow_fetch: | |
nodir
2016/05/20 23:03:49
inverse condition and unindent the _run_git call
Paweł Hajdan Jr.
2016/05/24 16:58:06
Done.
| |
56 _run_git(None, 'clone', '-q', repo, checkout_dir) | |
57 else: | |
58 raise FetchNotAllowedError( | |
59 'need to clone %s but fetch not allowed' % repo) | |
60 elif not os.path.isdir(os.path.join(checkout_dir, '.git')): | |
61 raise UncleanFilesystemError( | |
62 '%s exists but is not a git repo' % checkout_dir) | |
63 | |
nodir
2016/05/20 23:03:49
what if checkout dir exists but points to a differ
Paweł Hajdan Jr.
2016/05/24 16:58:06
Good point. Out of scope for this CL (this is just
nodir
2016/05/24 17:46:31
It is likely to succeed because default value of r
Paweł Hajdan Jr.
2016/05/25 00:15:41
Yeah, fair enough. I added code to verify it in th
| |
64 try: | |
65 _run_git(checkout_dir, 'rev-parse', '-q', '--verify', | |
66 '%s^{commit}' % revision) | |
67 except subprocess42.CalledProcessError: | |
68 if allow_fetch: | |
nodir
2016/05/20 23:03:49
same here
Paweł Hajdan Jr.
2016/05/24 16:58:06
Done.
| |
69 _run_git(checkout_dir, 'fetch') | |
70 else: | |
71 raise FetchNotAllowedError( | |
72 'need to fetch %s but fetch not allowed' % repo) | |
73 _run_git(checkout_dir, 'reset', '-q', '--hard', revision) | |
74 _cleanup_pyc(checkout_dir) | |
nodir
2016/05/20 23:03:49
pyc files have nothing to do with git. This functi
Paweł Hajdan Jr.
2016/05/24 16:58:06
Yup, done.
| |
OLD | NEW |