OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Utilities for formatting and writing DEPS files.""" | |
7 | |
6 | 8 |
7 class VarImpl(object): | 9 class VarImpl(object): |
8 """Implement the Var function used within the DEPS file.""" | 10 """Implement the Var function used within the DEPS file.""" |
11 | |
9 def __init__(self, local_scope): | 12 def __init__(self, local_scope): |
10 self._local_scope = local_scope | 13 self._local_scope = local_scope |
11 | 14 |
12 def Lookup(self, var_name): | 15 def Lookup(self, var_name): |
13 """Implements the Var syntax.""" | 16 """Implements the Var syntax.""" |
14 if var_name in self._local_scope.get('vars', {}): | 17 if var_name in self._local_scope.get('vars', {}): |
15 return self._local_scope['vars'][var_name] | 18 return self._local_scope['vars'][var_name] |
16 raise Exception('Var is not defined: %s' % var_name) | 19 raise Exception('Var is not defined: %s' % var_name) |
17 | 20 |
18 | 21 |
19 def GetDepsContent(deps_path): | 22 def GetDepsContent(deps_path): |
20 """Read a DEPS file and return all the sections.""" | 23 """Read a DEPS file and return all the sections.""" |
21 deps_file = open(deps_path, 'rU') | 24 deps_file = open(deps_path, 'rU') |
22 content = deps_file.read() | 25 content = deps_file.read() |
23 local_scope = {} | 26 local_scope = {} |
24 var = VarImpl(local_scope) | 27 var = VarImpl(local_scope) |
25 global_scope = { | 28 global_scope = { |
26 'Var': var.Lookup, | 29 'Var': var.Lookup, |
27 'deps': {}, | 30 'deps': {}, |
28 'deps_os': {}, | 31 'deps_os': {}, |
29 'include_rules': [], | 32 'include_rules': [], |
30 'skip_child_includes': [], | 33 'skip_child_includes': [], |
31 'hooks': [], | 34 'hooks': [], |
32 } | 35 } |
33 exec(content, global_scope, local_scope) | 36 exec(content, global_scope, local_scope) |
34 local_scope.setdefault('deps', {}) | 37 local_scope.setdefault('deps', {}) |
35 local_scope.setdefault('deps_os', {}) | 38 local_scope.setdefault('deps_os', {}) |
36 local_scope.setdefault('include_rules', []) | 39 local_scope.setdefault('include_rules', []) |
37 local_scope.setdefault('skip_child_includes', []) | 40 local_scope.setdefault('skip_child_includes', []) |
38 local_scope.setdefault('hooks', []) | 41 local_scope.setdefault('hooks', []) |
42 local_scope.setdefault('vars', {}) | |
39 | 43 |
40 return (local_scope['deps'], local_scope['deps_os'], | 44 return (local_scope['deps'], local_scope['deps_os'], |
41 local_scope['include_rules'], local_scope['skip_child_includes'], | 45 local_scope['include_rules'], local_scope['skip_child_includes'], |
42 local_scope['hooks']) | 46 local_scope['hooks'], local_scope['vars']) |
43 | 47 |
44 | 48 |
45 def PrettyDeps(deps, indent=0): | 49 def PrettyDeps(deps, indent=0): |
46 """Stringify a deps dictionary in a pretty way.""" | 50 """Stringify a deps dictionary in a pretty way.""" |
47 pretty = ' ' * indent | 51 pretty = ' ' * indent |
48 pretty += '{\n' | 52 pretty += '{\n' |
49 | 53 |
50 indent += 4 | 54 indent += 4 |
51 | 55 |
52 for item in sorted(deps): | 56 for item in sorted(deps): |
(...skipping 16 matching lines...) Expand all Loading... | |
69 """Stringify an object in a pretty way.""" | 73 """Stringify an object in a pretty way.""" |
70 pretty = str(obj).replace('{', '{\n ') | 74 pretty = str(obj).replace('{', '{\n ') |
71 pretty = pretty.replace('}', '\n}') | 75 pretty = pretty.replace('}', '\n}') |
72 pretty = pretty.replace('[', '[\n ') | 76 pretty = pretty.replace('[', '[\n ') |
73 pretty = pretty.replace(']', '\n]') | 77 pretty = pretty.replace(']', '\n]') |
74 pretty = pretty.replace('\':', '\':\n ') | 78 pretty = pretty.replace('\':', '\':\n ') |
75 pretty = pretty.replace(', ', ',\n ') | 79 pretty = pretty.replace(', ', ',\n ') |
76 return pretty | 80 return pretty |
77 | 81 |
78 | 82 |
79 def Varify(deps): | 83 def Varify(deps, deps_overrides): |
80 """Replace all instances of our git server with a git_url var.""" | 84 """Replace all instances of our git server with a git_url var.""" |
81 deps = deps.replace('\'http://git.chromium.org/external/WebKit_trimmed.git', | 85 deps = deps.replace('\'http://git.chromium.org/external/WebKit_trimmed.git', |
82 'Var(\'webkit_url\')') | 86 'Var(\'webkit_url\')') |
83 deps = deps.replace('\'http://git.chromium.org', 'Var(\'git_url\') + \'') | 87 deps = deps.replace('\'http://git.chromium.org', 'Var(\'git_url\') + \'') |
84 deps = deps.replace('VAR_WEBKIT_REV\'', ' + Var(\'webkit_rev\')') | 88 for value in deps_overrides.values(): |
89 deps = deps.replace("VAR_HASH_TEMP_%s'" % value, "' + Var('%s')" % value) | |
nsylvain
2012/02/15 17:43:39
Can you add a comment here to say that you are "tr
DaleCurtis
2012/02/16 02:38:37
Done.
| |
90 deps = deps.replace("VAR_WEBKIT_REV'", " + Var('webkit_rev')") | |
85 return deps | 91 return deps |
86 | 92 |
87 | 93 |
88 def WriteDeps(deps_file_name, vars, deps, deps_os, include_rules, | 94 def WriteDeps(deps_file_name, deps_vars, deps, deps_os, include_rules, |
89 skip_child_includes, hooks): | 95 skip_child_includes, hooks, deps_overrides): |
90 """Given all the sections in a DEPS file, write it to disk.""" | 96 """Given all the sections in a DEPS file, write it to disk.""" |
91 new_deps = ('# DO NOT EDIT EXCEPT FOR LOCAL TESTING.\n' | 97 new_deps = ('# DO NOT EDIT EXCEPT FOR LOCAL TESTING.\n' |
92 '# THIS IS A GENERATED FILE.\n', | 98 '# THIS IS A GENERATED FILE.\n', |
93 '# ALL MANUAL CHANGES WILL BE OVERWRITTEN.\n', | 99 '# ALL MANUAL CHANGES WILL BE OVERWRITTEN.\n', |
94 '# SEE http://code.google.com/p/chromium/wiki/UsingNewGit\n', | 100 '# SEE http://code.google.com/p/chromium/wiki/UsingNewGit\n', |
95 '# FOR HOW TO ROLL DEPS\n' | 101 '# FOR HOW TO ROLL DEPS\n' |
96 'vars = %s\n\n' % PrettyObj(vars), | 102 'vars = %s\n\n' % PrettyObj(deps_vars), |
97 'deps = %s\n\n' % Varify(PrettyDeps(deps)), | 103 'deps = %s\n\n' % Varify(PrettyDeps(deps), deps_overrides), |
98 'deps_os = %s\n\n' % Varify(PrettyDeps(deps_os)), | 104 'deps_os = %s\n\n' % Varify(PrettyDeps(deps_os), deps_overrides), |
99 'include_rules = %s\n\n' % PrettyObj(include_rules), | 105 'include_rules = %s\n\n' % PrettyObj(include_rules), |
100 'skip_child_includes = %s\n\n' % PrettyObj(skip_child_includes), | 106 'skip_child_includes = %s\n\n' % PrettyObj(skip_child_includes), |
101 'hooks = %s\n' % PrettyObj(hooks)) | 107 'hooks = %s\n' % PrettyObj(hooks)) |
102 new_deps = ''.join(new_deps) | 108 new_deps = ''.join(new_deps) |
103 deps_file = open(deps_file_name, 'w') | 109 deps_file = open(deps_file_name, 'w') |
104 deps_file.write(new_deps) | 110 deps_file.write(new_deps) |
OLD | NEW |