OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import StringIO | |
7 import optparse | 6 import optparse |
8 import re | 7 import re |
9 import sys | 8 import sys |
10 | 9 |
11 def MakeBranchRevisionList(): | |
12 # Parse a list of SVN changes to extract revision numbers and branch names | |
13 # | |
14 # Input is the output of "svn log -qv", each record should look like this: | |
15 # | |
16 # ------------------------------------------------------------------------ | |
17 # r151821 | someone@chromium.org | 2013-06-05 10:08:51 +0100 (Wed, 05 Jun 2013
) | |
18 # Changed paths: | |
19 # M /trunk/... | |
20 # | |
21 # or M /branches/chromium/1234/... | |
22 # | |
23 # Output is a '\n'-separated list of <branch>@<revision> pairs. The order of | |
24 # revisions is unaltered. | |
25 branches_and_revisions = [] | |
26 while True: | |
27 line = sys.stdin.readline() | |
28 if len(line) == 0: | |
29 return '' | |
30 if line.startswith('--------'): | |
31 break | |
32 while True: | |
33 revision_entry = '' | |
34 while True: | |
35 line = sys.stdin.readline() | |
36 if len(line) == 0: | |
37 revision_entry = None | |
38 break | |
39 if line.startswith('--------'): | |
40 break | |
41 revision_entry += line | |
42 if not revision_entry: | |
43 break | |
44 match = re.search('r(\d+).*Changed paths[^/]*([/].*)\n', revision_entry, re.
DOTALL) | |
45 if match: | |
46 revision = int(match.group(1)) | |
47 path = match.group(2) | |
48 branch = None | |
49 if path.startswith('/trunk'): | |
50 branch = '/trunk' | |
51 elif path.startswith('/branches/chromium'): | |
52 match = re.search('(/branches/chromium/\d+)', path) | |
53 if match: | |
54 branch = match.group(1) | |
55 if branch: | |
56 branches_and_revisions.append((branch, revision)) | |
57 out = StringIO.StringIO() | |
58 for rec in branches_and_revisions: | |
59 out.write('%s@%d\n' % (rec[0], rec[1])) | |
60 return out.getvalue() | |
61 | |
62 class VarImpl(object): | 10 class VarImpl(object): |
63 """Implement the Var function used within the DEPS file.""" | 11 """Implement the Var function used within the DEPS file.""" |
64 | 12 |
65 def __init__(self, local_scope): | 13 def __init__(self, local_scope): |
66 self._local_scope = local_scope | 14 self._local_scope = local_scope |
67 | 15 |
68 def Lookup(self, var_name): | 16 def Lookup(self, var_name): |
69 """Implements the Var syntax.""" | 17 """Implements the Var syntax.""" |
70 if var_name in self._local_scope.get('vars', {}): | 18 if var_name in self._local_scope.get('vars', {}): |
71 return self._local_scope['vars'][var_name] | 19 return self._local_scope['vars'][var_name] |
72 raise Exception('Var is not defined: %s' % var_name) | 20 raise Exception('Var is not defined: %s' % var_name) |
73 | 21 |
74 def FindProjectRevision(project_name): | 22 def FindProjectRevision(project_name): |
75 # Parse Chromium DEPS file to find out the revision of the project in use. | 23 # Parse Chromium DEPS file to find out the revision of the project in use. |
76 deps_content = sys.stdin.read() | 24 deps_content = sys.stdin.read() |
77 local_scope = {} | 25 local_scope = {} |
78 var = VarImpl(local_scope) | 26 var = VarImpl(local_scope) |
79 global_scope = { 'Var': var.Lookup, 'deps': {} } | 27 global_scope = { 'Var': var.Lookup, 'deps': {} } |
80 exec(deps_content, global_scope, local_scope) | 28 exec(deps_content, global_scope, local_scope) |
81 project_path = local_scope['deps'][project_name] | 29 project_path = local_scope['deps'][project_name] |
82 match = re.search('r[^@]*@(\d+)', project_path) | 30 match = re.search('r[^@]*@(\d+)', project_path) |
83 if match: | 31 if match: |
84 return match.group(1) | 32 return match.group(1) |
85 return project_path | 33 return project_path |
86 | 34 |
87 def main(): | 35 def main(): |
88 parser = optparse.OptionParser(usage='%prog [options]') | 36 parser = optparse.OptionParser(usage='%prog [options]') |
89 parser.add_option( | 37 parser.add_option( |
90 '', '--make_branch_revision_list', | |
91 default=False, action='store_true', | |
92 help=('MakeBranchRevisionList')) | |
93 parser.add_option( | |
94 '', '--find_project_revision', | 38 '', '--find_project_revision', |
95 default=False, | 39 default=False, |
96 help=('FindProjectRevision')) | 40 help=('FindProjectRevision')) |
97 (options, args) = parser.parse_args() | 41 (options, args) = parser.parse_args() |
98 if args: | 42 if args: |
99 parser.print_help() | 43 parser.print_help() |
100 return 1 | 44 return 1 |
101 if options.make_branch_revision_list: | 45 if options.find_project_revision: |
102 print MakeBranchRevisionList() | |
103 elif options.find_project_revision: | |
104 print FindProjectRevision(options.find_project_revision) | 46 print FindProjectRevision(options.find_project_revision) |
105 | 47 |
106 return 0 | 48 return 0 |
107 | 49 |
108 if __name__ == '__main__': | 50 if __name__ == '__main__': |
109 sys.exit(main()) | 51 sys.exit(main()) |
OLD | NEW |