OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 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 """ | 6 """ |
7 version.py -- Chromium version string substitution utility. | 7 version.py -- Chromium version string substitution utility. |
8 """ | 8 """ |
9 | 9 |
10 import getopt | 10 import getopt |
11 import os | 11 import os |
12 import re | |
13 import subprocess | |
14 import sys | 12 import sys |
15 | 13 |
16 | 14 |
17 class Usage(Exception): | 15 class Usage(Exception): |
18 def __init__(self, msg): | 16 def __init__(self, msg): |
19 self.msg = msg | 17 self.msg = msg |
20 | 18 |
21 | 19 |
22 def fetch_values_from_file(values_dict, file_name): | 20 def fetch_values_from_file(values_dict, file_name): |
23 """ | 21 """ |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 if contents == old_contents: | 101 if contents == old_contents: |
104 return | 102 return |
105 os.unlink(file_name) | 103 os.unlink(file_name) |
106 open(file_name, 'w').write(contents) | 104 open(file_name, 'w').write(contents) |
107 | 105 |
108 | 106 |
109 def main(argv=None): | 107 def main(argv=None): |
110 if argv is None: | 108 if argv is None: |
111 argv = sys.argv | 109 argv = sys.argv |
112 | 110 |
113 short_options = 'f:i:o:t:h' | 111 short_options = 'e:f:i:o:t:h' |
114 long_options = ['file=', 'help'] | 112 long_options = ['eval=', 'file=', 'help'] |
115 | 113 |
116 helpstr = """\ | 114 helpstr = """\ |
117 Usage: version.py [-h] [-f FILE] ([[-i] FILE] | -t TEMPLATE) [[-o] FILE] | 115 Usage: version.py [-h] [-f FILE] ([[-i] FILE] | -t TEMPLATE) [[-o] FILE] |
118 | 116 |
119 -f FILE, --file=FILE Read variables from FILE. | 117 -f FILE, --file=FILE Read variables from FILE. |
120 -i FILE, --input=FILE Read strings to substitute from FILE. | 118 -i FILE, --input=FILE Read strings to substitute from FILE. |
121 -o FILE, --output=FILE Write substituted strings to FILE. | 119 -o FILE, --output=FILE Write substituted strings to FILE. |
122 -t TEMPLATE, --template=TEMPLATE Use TEMPLATE as the strings to substitute. | 120 -t TEMPLATE, --template=TEMPLATE Use TEMPLATE as the strings to substitute. |
| 121 -e VAR=VAL, --eval=VAR=VAL Evaluate VAL after reading variables. Can |
| 122 be used to synthesize variables. e.g. |
| 123 -e 'PATCH_HI=int(PATCH)/256'. |
123 -h, --help Print this help and exit. | 124 -h, --help Print this help and exit. |
124 """ | 125 """ |
125 | 126 |
| 127 evals = {} |
126 variable_files = [] | 128 variable_files = [] |
127 in_file = None | 129 in_file = None |
128 out_file = None | 130 out_file = None |
129 template = None | 131 template = None |
130 | 132 |
131 try: | 133 try: |
132 try: | 134 try: |
133 opts, args = getopt.getopt(argv[1:], short_options, long_options) | 135 opts, args = getopt.getopt(argv[1:], short_options, long_options) |
134 except getopt.error, msg: | 136 except getopt.error, msg: |
135 raise Usage(msg) | 137 raise Usage(msg) |
136 for o, a in opts: | 138 for o, a in opts: |
137 if o in ('-f', '--file'): | 139 if o in ('-e', '--eval'): |
| 140 try: |
| 141 evals.update(dict([a.split('=',1)])) |
| 142 except ValueError: |
| 143 raise Usage("-e requires VAR=VAL") |
| 144 elif o in ('-f', '--file'): |
138 variable_files.append(a) | 145 variable_files.append(a) |
139 elif o in ('-i', '--input'): | 146 elif o in ('-i', '--input'): |
140 in_file = a | 147 in_file = a |
141 elif o in ('-o', '--output'): | 148 elif o in ('-o', '--output'): |
142 out_file = a | 149 out_file = a |
143 elif o in ('-t', '--template'): | 150 elif o in ('-t', '--template'): |
144 template = a | 151 template = a |
145 elif o in ('-h', '--help'): | 152 elif o in ('-h', '--help'): |
146 print helpstr | 153 print helpstr |
147 return 0 | 154 return 0 |
148 while len(args) and (in_file is None or out_file is None or | 155 while len(args) and (in_file is None or out_file is None or |
149 template is None): | 156 template is None): |
150 if in_file is None: | 157 if in_file is None: |
151 in_file = args.pop(0) | 158 in_file = args.pop(0) |
152 elif out_file is None: | 159 elif out_file is None: |
153 out_file = args.pop(0) | 160 out_file = args.pop(0) |
154 if args: | 161 if args: |
155 msg = 'Unexpected arguments: %r' % args | 162 msg = 'Unexpected arguments: %r' % args |
156 raise Usage(msg) | 163 raise Usage(msg) |
157 except Usage, err: | 164 except Usage, err: |
158 sys.stderr.write(err.msg) | 165 sys.stderr.write(err.msg) |
159 sys.stderr.write('Use -h to get help.') | 166 sys.stderr.write('; Use -h to get help.\n') |
160 return 2 | 167 return 2 |
161 | 168 |
162 values = fetch_values(variable_files) | 169 values = fetch_values(variable_files) |
163 | 170 for key, val in evals.iteritems(): |
| 171 values[key] = str(eval(val, globals(), values)) |
164 | 172 |
165 if template is not None: | 173 if template is not None: |
166 contents = subst_template(template, values) | 174 contents = subst_template(template, values) |
167 elif in_file: | 175 elif in_file: |
168 contents = subst_file(in_file, values) | 176 contents = subst_file(in_file, values) |
169 else: | 177 else: |
170 # Generate a default set of version information. | 178 # Generate a default set of version information. |
171 contents = """MAJOR=%(MAJOR)s | 179 contents = """MAJOR=%(MAJOR)s |
172 MINOR=%(MINOR)s | 180 MINOR=%(MINOR)s |
173 BUILD=%(BUILD)s | 181 BUILD=%(BUILD)s |
174 PATCH=%(PATCH)s | 182 PATCH=%(PATCH)s |
175 LASTCHANGE=%(LASTCHANGE)s | 183 LASTCHANGE=%(LASTCHANGE)s |
176 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s | 184 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s |
177 """ % values | 185 """ % values |
178 | 186 |
179 | 187 |
180 if out_file: | 188 if out_file: |
181 write_if_changed(out_file, contents) | 189 write_if_changed(out_file, contents) |
182 else: | 190 else: |
183 print contents | 191 print contents |
184 | 192 |
185 return 0 | 193 return 0 |
186 | 194 |
187 | 195 |
188 if __name__ == '__main__': | 196 if __name__ == '__main__': |
189 sys.exit(main()) | 197 sys.exit(main()) |
OLD | NEW |