OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 # Functions in this file relies on depot_tools been checked-out as a sibling | |
6 # of infra.git. | |
7 | |
8 import logging | |
9 import os | |
10 import re | |
11 import subprocess | |
12 | |
13 | |
14 BASE_DIR = os.path.dirname( | |
15 os.path.dirname( | |
16 os.path.dirname( | |
17 os.path.dirname(os.path.realpath(__file__))))) | |
18 | |
19 | |
20 def parse_revinfo(revinfo): | |
21 """Parse the output of "gclient revinfo -a" | |
22 | |
23 Args: | |
24 revinfo (str): string containing gclient stdout. | |
25 | |
26 Returns: | |
27 revinfo_d (dict): <directory>: (URL, revision) | |
28 """ | |
29 revision_expr = re.compile('(.*)@([^@]*)') | |
30 | |
31 revinfo_d = {} | |
32 for line in revinfo.splitlines(): | |
33 if ':' not in line: | |
34 continue | |
35 | |
36 # TODO: this fails when the file name contains a colon. | |
37 path, line = line.split(':', 1) | |
38 if '@' in line: | |
39 url, revision = revision_expr.match(line).groups() | |
40 revision = revision.strip() | |
41 else: | |
42 # Split at the last @ | |
43 url, revision = line.strip(), None | |
44 | |
45 path = path.strip() | |
46 url = url.strip() | |
47 revinfo_d[path] = {'source_url': url, 'revision': revision} | |
48 return revinfo_d | |
49 | |
50 | |
51 def get_revinfo(cwd=None): # pragma: no cover | |
52 """Call gclient to get the list of all revisions actually checked out on disk. | |
53 | |
54 gclient is expected to be under depot_tools/ sibling to infra/. | |
55 If gclient can't be found or fail to run returns {}. | |
56 | |
57 Args: | |
58 cwd (str): working directory where to run gclient. If None, use the | |
59 current working directory. | |
60 Returns: | |
61 revinfo (dict): keys are local paths, values are dicts with keys: | |
62 'source_url' or 'revision'. The latter can be a git SHA1 or an svn | |
63 revision. | |
64 """ | |
65 | |
66 cmd = [os.path.join(BASE_DIR, 'depot_tools', 'gclient'), 'revinfo', '-a'] | |
67 logging.debug('Running: %s', ' '.join(cmd)) | |
68 revinfo = '' | |
69 try: | |
70 revinfo = subprocess.check_output(cmd, cwd=cwd) | |
71 except (subprocess.CalledProcessError, OSError): | |
72 logging.exception('Command failed to run: %s', ' '.join(cmd)) | |
73 return parse_revinfo(revinfo) | |
OLD | NEW |