OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import ast | 5 import ast |
6 import collections | 6 import collections |
7 import contextlib | 7 import contextlib |
8 import copy | 8 import copy |
9 import difflib | 9 import difflib |
10 import functools | 10 import functools |
11 import itertools | 11 import itertools |
12 import logging | 12 import logging |
13 import operator | 13 import operator |
14 import os | 14 import os |
15 import subprocess | 15 import subprocess |
16 import sys | 16 import sys |
17 import tempfile | 17 import tempfile |
18 | 18 |
19 from .third_party.google.protobuf import text_format | 19 from .third_party.google.protobuf import text_format |
20 from . import package_pb2 | 20 from . import package_pb2 |
21 | 21 from . import fetch |
22 | |
23 class UncleanFilesystemError(Exception): | |
24 pass | |
25 | |
26 | |
27 class FetchNotAllowedError(Exception): | |
28 pass | |
29 | 22 |
30 | 23 |
31 class InconsistentDependencyGraphError(Exception): | 24 class InconsistentDependencyGraphError(Exception): |
32 def __init__(self, project_id, specs): | 25 def __init__(self, project_id, specs): |
33 self.project_id = project_id | 26 self.project_id = project_id |
34 self.specs = specs | 27 self.specs = specs |
35 | 28 |
36 def __str__(self): | 29 def __str__(self): |
37 return 'Package specs for %s do not match: %s vs %s' % ( | 30 return 'Package specs for %s do not match: %s vs %s' % ( |
38 project_id, self.specs[0], self.specs[1]) | 31 project_id, self.specs[0], self.specs[1]) |
39 | 32 |
40 | 33 |
41 class CyclicDependencyError(Exception): | 34 class CyclicDependencyError(Exception): |
42 pass | 35 pass |
43 | 36 |
44 | 37 |
45 def cleanup_pyc(path): | 38 def cleanup_pyc(path): |
46 """Removes any .pyc files from |path|'s directory tree. | 39 """Removes any .pyc files from |path|'s directory tree. |
| 40 |
47 This ensures we always use the fresh code. | 41 This ensures we always use the fresh code. |
48 """ | 42 """ |
49 for root, dirs, files in os.walk(path): | 43 for root, dirs, files in os.walk(path): |
50 for f in files: | 44 for f in files: |
51 if f.endswith('.pyc'): | 45 if f.endswith('.pyc'): |
52 os.unlink(os.path.join(root, f)) | 46 os.unlink(os.path.join(root, f)) |
53 | 47 |
54 | 48 |
55 class InfraRepoConfig(object): | 49 class InfraRepoConfig(object): |
56 def to_recipes_cfg(self, repo_root): | 50 def to_recipes_cfg(self, repo_root): |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 def run_git(self, context, *args): | 210 def run_git(self, context, *args): |
217 cmd = [self._git] | 211 cmd = [self._git] |
218 if context is not None: | 212 if context is not None: |
219 cmd += ['-C', self._dep_dir(context)] | 213 cmd += ['-C', self._dep_dir(context)] |
220 cmd += list(args) | 214 cmd += list(args) |
221 | 215 |
222 logging.info('Running: %s', cmd) | 216 logging.info('Running: %s', cmd) |
223 return subprocess.check_output(cmd) | 217 return subprocess.check_output(cmd) |
224 | 218 |
225 def checkout(self, context): | 219 def checkout(self, context): |
226 dep_dir = self._dep_dir(context) | 220 checkout_dir = self._dep_dir(context) |
227 logging.info('Freshening repository %s', dep_dir) | 221 fetch.ensure_git_checkout( |
228 | 222 self.repo, self.revision, checkout_dir, context.allow_fetch) |
229 if not os.path.isdir(dep_dir): | 223 cleanup_pyc(checkout_dir) |
230 if context.allow_fetch: | |
231 self.run_git(None, 'clone', '-q', self.repo, dep_dir) | |
232 else: | |
233 raise FetchNotAllowedError( | |
234 'need to clone %s but fetch not allowed' % self.repo) | |
235 elif not os.path.isdir(os.path.join(dep_dir, '.git')): | |
236 raise UncleanFilesystemError('%s exists but is not a git repo' % dep_dir) | |
237 | |
238 try: | |
239 self.run_git(context, 'rev-parse', '-q', '--verify', | |
240 '%s^{commit}' % self.revision) | |
241 except subprocess.CalledProcessError: | |
242 if context.allow_fetch: | |
243 self.run_git(context, 'fetch') | |
244 else: | |
245 raise FetchNotAllowedError( | |
246 'need to fetch %s but fetch not allowed' % self.repo) | |
247 self.run_git(context, 'reset', '-q', '--hard', self.revision) | |
248 cleanup_pyc(dep_dir) | |
249 | 224 |
250 def repo_root(self, context): | 225 def repo_root(self, context): |
251 return os.path.join(self._dep_dir(context), self.path) | 226 return os.path.join(self._dep_dir(context), self.path) |
252 | 227 |
253 def dump(self): | 228 def dump(self): |
254 buf = package_pb2.DepSpec( | 229 buf = package_pb2.DepSpec( |
255 project_id=self.project_id, | 230 project_id=self.project_id, |
256 url=self.repo, | 231 url=self.repo, |
257 branch=self.branch, | 232 branch=self.branch, |
258 revision=self.revision) | 233 revision=self.revision) |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 >>> d = { 'x': 1, 'y': 2 } | 676 >>> d = { 'x': 1, 'y': 2 } |
702 >>> sorted(_updated(d, { 'y': 3, 'z': 4 }).items()) | 677 >>> sorted(_updated(d, { 'y': 3, 'z': 4 }).items()) |
703 [('x', 1), ('y', 3), ('z', 4)] | 678 [('x', 1), ('y', 3), ('z', 4)] |
704 >>> sorted(d.items()) | 679 >>> sorted(d.items()) |
705 [('x', 1), ('y', 2)] | 680 [('x', 1), ('y', 2)] |
706 """ | 681 """ |
707 | 682 |
708 d = copy.copy(d) | 683 d = copy.copy(d) |
709 d.update(updates) | 684 d.update(updates) |
710 return d | 685 return d |
OLD | NEW |