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): | 30 def fetch_values(file_list, is_official_build=None): |
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': | 40 if CHROME_BUILD_TYPE == '_official' or is_official_build: |
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.') |
116 parser.add_argument('args', nargs=argparse.REMAINDER, | 120 parser.add_argument('args', nargs=argparse.REMAINDER, |
117 help='For compatibility: INPUT and OUTPUT can be ' | 121 help='For compatibility: INPUT and OUTPUT can be ' |
118 'passed as positional arguments.') | 122 'passed as positional arguments.') |
119 options = parser.parse_args() | 123 options = parser.parse_args() |
120 | 124 |
121 evals = {} | 125 evals = {} |
122 for expression in options.eval: | 126 for expression in options.eval: |
123 try: | 127 try: |
124 evals.update(dict([expression.split('=', 1)])) | 128 evals.update(dict([expression.split('=', 1)])) |
125 except ValueError: | 129 except ValueError: |
126 parser.error('-e requires VAR=VAL') | 130 parser.error('-e requires VAR=VAL') |
127 | 131 |
128 # Compatibility with old versions that considered the first two positional | 132 # Compatibility with old versions that considered the first two positional |
129 # arguments shorthands for --input and --output. | 133 # arguments shorthands for --input and --output. |
130 while len(options.args) and (options.input is None or \ | 134 while len(options.args) and (options.input is None or \ |
131 options.output is None): | 135 options.output is None): |
132 if options.input is None: | 136 if options.input is None: |
133 options.input = options.args.pop(0) | 137 options.input = options.args.pop(0) |
134 elif options.output is None: | 138 elif options.output is None: |
135 options.output = options.args.pop(0) | 139 options.output = options.args.pop(0) |
136 if options.args: | 140 if options.args: |
137 parser.error('Unexpected arguments: %r' % options.args) | 141 parser.error('Unexpected arguments: %r' % options.args) |
138 | 142 |
139 values = fetch_values(options.file) | 143 values = fetch_values(options.file, options.official) |
140 for key, val in evals.iteritems(): | 144 for key, val in evals.iteritems(): |
141 values[key] = str(eval(val, globals(), values)) | 145 values[key] = str(eval(val, globals(), values)) |
142 | 146 |
143 if options.template is not None: | 147 if options.template is not None: |
144 contents = subst_template(options.template, values) | 148 contents = subst_template(options.template, values) |
145 elif options.input: | 149 elif options.input: |
146 contents = subst_file(options.input, values) | 150 contents = subst_file(options.input, values) |
147 else: | 151 else: |
148 # Generate a default set of version information. | 152 # Generate a default set of version information. |
149 contents = """MAJOR=%(MAJOR)s | 153 contents = """MAJOR=%(MAJOR)s |
150 MINOR=%(MINOR)s | 154 MINOR=%(MINOR)s |
151 BUILD=%(BUILD)s | 155 BUILD=%(BUILD)s |
152 PATCH=%(PATCH)s | 156 PATCH=%(PATCH)s |
153 LASTCHANGE=%(LASTCHANGE)s | 157 LASTCHANGE=%(LASTCHANGE)s |
154 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s | 158 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s |
155 """ % values | 159 """ % values |
156 | 160 |
157 if options.output is not None: | 161 if options.output is not None: |
158 write_if_changed(options.output, contents) | 162 write_if_changed(options.output, contents) |
159 else: | 163 else: |
160 print contents | 164 print contents |
161 | 165 |
162 return 0 | 166 return 0 |
163 | 167 |
164 | 168 |
165 if __name__ == '__main__': | 169 if __name__ == '__main__': |
166 sys.exit(main()) | 170 sys.exit(main()) |
OLD | NEW |