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 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 if contents == old_contents: | 146 if contents == old_contents: |
147 return | 147 return |
148 os.unlink(file_name) | 148 os.unlink(file_name) |
149 open(file_name, 'w').write(contents) | 149 open(file_name, 'w').write(contents) |
150 | 150 |
151 | 151 |
152 def main(argv=None): | 152 def main(argv=None): |
153 if argv is None: | 153 if argv is None: |
154 argv = sys.argv | 154 argv = sys.argv |
155 | 155 |
156 short_options = 'f:h' | 156 short_options = 'f:i:o:h' |
157 long_options = ['file=', 'help'] | 157 long_options = ['file=', 'help'] |
158 | 158 |
159 helpstr = """\ | 159 helpstr = """\ |
160 Usage: version.py [-h] [-f FILE] INFILE OUTFILE | 160 Usage: version.py [-h] [-f FILE] [[-i] FILE] [[-o] FILE] |
161 | 161 |
162 -f FILE, --file=FILE Read variables from FILE. | 162 -f FILE, --file=FILE Read variables from FILE. |
| 163 -i FILE, --input=FILE Read strings to substitute from FILE. |
| 164 -o FILE, --output=FILE Write substituted strings to FILE. |
163 -h, --help Print this help and exit. | 165 -h, --help Print this help and exit. |
164 """ | 166 """ |
165 | 167 |
166 variable_files = [] | 168 variable_files = [] |
| 169 in_file = None |
| 170 out_file = None |
167 | 171 |
168 try: | 172 try: |
169 try: | 173 try: |
170 opts, args = getopt.getopt(argv[1:], short_options, long_options) | 174 opts, args = getopt.getopt(argv[1:], short_options, long_options) |
171 except getopt.error, msg: | 175 except getopt.error, msg: |
172 raise Usage(msg) | 176 raise Usage(msg) |
173 for o, a in opts: | 177 for o, a in opts: |
174 if o in ('-f', '--file'): | 178 if o in ('-f', '--file'): |
175 variable_files.append(a) | 179 variable_files.append(a) |
| 180 elif o in ('-i', '--input'): |
| 181 in_file = a |
| 182 elif o in ('-o', '--output'): |
| 183 out_file = a |
176 elif o in ('-h', '--help'): | 184 elif o in ('-h', '--help'): |
177 print helpstr | 185 print helpstr |
178 return 0 | 186 return 0 |
179 try: | 187 while len(args) and (in_file is None or out_file is None): |
180 in_file, out_file = args | 188 if in_file is None: |
181 except ValueError: | 189 in_file = args.pop(0) |
182 msg = 'Incorrect number of arguments: %r' % args | 190 elif out_file is None: |
| 191 out_file = args.pop(0) |
| 192 if args: |
| 193 msg = 'Unexpected arguments: %r' % args |
183 raise Usage(msg) | 194 raise Usage(msg) |
184 except Usage, err: | 195 except Usage, err: |
185 sys.stderr.write(err.msg) | 196 sys.stderr.write(err.msg) |
186 sys.stderr.write('Use -h to get help.') | 197 sys.stderr.write('Use -h to get help.') |
187 return 2 | 198 return 2 |
188 | 199 |
189 values = fetch_values(variable_files) | 200 values = fetch_values(variable_files) |
190 | 201 |
191 contents = subst_contents(in_file, values) | |
192 | 202 |
193 write_if_changed(out_file, contents) | 203 if in_file: |
| 204 contents = subst_contents(in_file, values) |
| 205 else: |
| 206 # Generate a default set of version information. |
| 207 contents = """MAJOR=%(MAJOR)s |
| 208 MINOR=%(MINOR)s |
| 209 BUILD=%(BUILD)s |
| 210 PATCH=%(PATCH)s |
| 211 LASTCHANGE=%(LASTCHANGE)s |
| 212 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s |
| 213 """ % values |
| 214 |
| 215 |
| 216 if out_file: |
| 217 write_if_changed(out_file, contents) |
| 218 else: |
| 219 print contents |
194 | 220 |
195 return 0 | 221 return 0 |
196 | 222 |
197 | 223 |
198 if __name__ == '__main__': | 224 if __name__ == '__main__': |
199 sys.exit(main()) | 225 sys.exit(main()) |
OLD | NEW |