OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 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 argparse | 10 import argparse |
11 import os | 11 import os |
12 import sys | 12 import sys |
13 | 13 |
14 | 14 |
15 def fetch_values_from_file(values_dict, file_name): | 15 def fetch_values_from_file(values_dict, file_name): |
16 """ | 16 """ |
17 Fetches KEYWORD=VALUE settings from the specified file. | 17 Fetches KEYWORD=VALUE settings from the specified file. |
18 | 18 |
19 Everything to the left of the first '=' is the keyword, | 19 Everything to the left of the first '=' is the keyword, |
20 everything to the right is the value. No stripping of | 20 everything to the right is the value. No stripping of |
21 white space, so beware. | 21 white space, so beware. |
22 | 22 |
23 The file must exist, otherwise you get the Python exception from open(). | 23 The file must exist, otherwise you get the Python exception from open(). |
24 """ | 24 """ |
25 for line in open(file_name, 'r').readlines(): | 25 for line in open(file_name, 'r').readlines(): |
26 key, val = line.rstrip('\r\n').split('=', 1) | 26 key, val = line.rstrip('\r\n').split('=', 1) |
27 values_dict[key] = val | 27 values_dict[key] = val |
28 | 28 |
29 | 29 |
30 def fetch_values(file_list, is_official_build=None): | 30 def fetch_values(file_list): |
31 """ | 31 """ |
32 Returns a dictionary of values to be used for substitution, populating | 32 Returns a dictionary of values to be used for substitution, populating |
33 the dictionary with KEYWORD=VALUE settings from the files in 'file_list'. | 33 the dictionary with KEYWORD=VALUE settings from the files in 'file_list'. |
34 | 34 |
35 Explicitly adds the following value from internal calculations: | 35 Explicitly adds the following value from internal calculations: |
36 | 36 |
37 OFFICIAL_BUILD | 37 OFFICIAL_BUILD |
38 """ | 38 """ |
39 CHROME_BUILD_TYPE = os.environ.get('CHROME_BUILD_TYPE') | 39 CHROME_BUILD_TYPE = os.environ.get('CHROME_BUILD_TYPE') |
40 if CHROME_BUILD_TYPE == '_official' or is_official_build: | 40 if CHROME_BUILD_TYPE == '_official': |
41 official_build = '1' | 41 official_build = '1' |
42 else: | 42 else: |
43 official_build = '0' | 43 official_build = '0' |
44 | 44 |
45 values = dict( | 45 values = dict( |
46 OFFICIAL_BUILD = official_build, | 46 OFFICIAL_BUILD = official_build, |
47 ) | 47 ) |
48 | 48 |
49 for file_name in file_list: | 49 for file_name in file_list: |
50 fetch_values_from_file(values, file_name) | 50 fetch_values_from_file(values, file_name) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 parser.add_argument('-i', '--input', default=None, | 106 parser.add_argument('-i', '--input', default=None, |
107 help='Read strings to substitute from FILE.') | 107 help='Read strings to substitute from FILE.') |
108 parser.add_argument('-o', '--output', default=None, | 108 parser.add_argument('-o', '--output', default=None, |
109 help='Write substituted strings to FILE.') | 109 help='Write substituted strings to FILE.') |
110 parser.add_argument('-t', '--template', default=None, | 110 parser.add_argument('-t', '--template', default=None, |
111 help='Use TEMPLATE as the strings to substitute.') | 111 help='Use TEMPLATE as the strings to substitute.') |
112 parser.add_argument('-e', '--eval', action='append', default=[], | 112 parser.add_argument('-e', '--eval', action='append', default=[], |
113 help='Evaluate VAL after reading variables. Can be used ' | 113 help='Evaluate VAL after reading variables. Can be used ' |
114 'to synthesize variables. e.g. -e \'PATCH_HI=int(' | 114 'to synthesize variables. e.g. -e \'PATCH_HI=int(' |
115 'PATCH)/256.') | 115 'PATCH)/256.') |
116 parser.add_argument('--official', action='store_true', | |
117 help='Whether the current build should be an official ' | |
118 'build, used in addition to the environment ' | |
119 'variable.') | |
120 parser.add_argument('args', nargs=argparse.REMAINDER, | 116 parser.add_argument('args', nargs=argparse.REMAINDER, |
121 help='For compatibility: INPUT and OUTPUT can be ' | 117 help='For compatibility: INPUT and OUTPUT can be ' |
122 'passed as positional arguments.') | 118 'passed as positional arguments.') |
123 options = parser.parse_args() | 119 options = parser.parse_args() |
124 | 120 |
125 evals = {} | 121 evals = {} |
126 for expression in options.eval: | 122 for expression in options.eval: |
127 try: | 123 try: |
128 evals.update(dict([expression.split('=', 1)])) | 124 evals.update(dict([expression.split('=', 1)])) |
129 except ValueError: | 125 except ValueError: |
130 parser.error('-e requires VAR=VAL') | 126 parser.error('-e requires VAR=VAL') |
131 | 127 |
132 # Compatibility with old versions that considered the first two positional | 128 # Compatibility with old versions that considered the first two positional |
133 # arguments shorthands for --input and --output. | 129 # arguments shorthands for --input and --output. |
134 while len(options.args) and (options.input is None or \ | 130 while len(options.args) and (options.input is None or \ |
135 options.output is None): | 131 options.output is None): |
136 if options.input is None: | 132 if options.input is None: |
137 options.input = options.args.pop(0) | 133 options.input = options.args.pop(0) |
138 elif options.output is None: | 134 elif options.output is None: |
139 options.output = options.args.pop(0) | 135 options.output = options.args.pop(0) |
140 if options.args: | 136 if options.args: |
141 parser.error('Unexpected arguments: %r' % options.args) | 137 parser.error('Unexpected arguments: %r' % options.args) |
142 | 138 |
143 values = fetch_values(options.file, options.official) | 139 values = fetch_values(options.file) |
144 for key, val in evals.iteritems(): | 140 for key, val in evals.iteritems(): |
145 values[key] = str(eval(val, globals(), values)) | 141 values[key] = str(eval(val, globals(), values)) |
146 | 142 |
147 if options.template is not None: | 143 if options.template is not None: |
148 contents = subst_template(options.template, values) | 144 contents = subst_template(options.template, values) |
149 elif options.input: | 145 elif options.input: |
150 contents = subst_file(options.input, values) | 146 contents = subst_file(options.input, values) |
151 else: | 147 else: |
152 # Generate a default set of version information. | 148 # Generate a default set of version information. |
153 contents = """MAJOR=%(MAJOR)s | 149 contents = """MAJOR=%(MAJOR)s |
154 MINOR=%(MINOR)s | 150 MINOR=%(MINOR)s |
155 BUILD=%(BUILD)s | 151 BUILD=%(BUILD)s |
156 PATCH=%(PATCH)s | 152 PATCH=%(PATCH)s |
157 LASTCHANGE=%(LASTCHANGE)s | 153 LASTCHANGE=%(LASTCHANGE)s |
158 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s | 154 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s |
159 """ % values | 155 """ % values |
160 | 156 |
161 if options.output is not None: | 157 if options.output is not None: |
162 write_if_changed(options.output, contents) | 158 write_if_changed(options.output, contents) |
163 else: | 159 else: |
164 print contents | 160 print contents |
165 | 161 |
166 return 0 | 162 return 0 |
167 | 163 |
168 | 164 |
169 if __name__ == '__main__': | 165 if __name__ == '__main__': |
170 sys.exit(main()) | 166 sys.exit(main()) |
OLD | NEW |