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
4 # found in the LICENSE file.
7 version.py -- Chromium version string substitution utility.
15 class Usage(Exception):
16 def __init__(self
, msg
):
20 def fetch_values_from_file(values_dict
, file_name
):
22 Fetches KEYWORD=VALUE settings from the specified file.
24 Everything to the left of the first '=' is the keyword,
25 everything to the right is the value. No stripping of
26 white space, so beware.
28 The file must exist, otherwise you get the Python exception from open().
30 for line
in open(file_name
, 'r').readlines():
31 key
, val
= line
.rstrip('\r\n').split('=', 1)
32 values_dict
[key
] = val
35 def fetch_values(file_list
):
37 Returns a dictionary of values to be used for substitution, populating
38 the dictionary with KEYWORD=VALUE settings from the files in 'file_list'.
40 Explicitly adds the following value from internal calculations:
44 CHROME_BUILD_TYPE
= os
.environ
.get('CHROME_BUILD_TYPE')
45 if CHROME_BUILD_TYPE
== '_official':
51 OFFICIAL_BUILD
= official_build
,
54 for file_name
in file_list
:
55 fetch_values_from_file(values
, file_name
)
60 def subst_template(contents
, values
):
62 Returns the template with substituted values from the specified dictionary.
64 Keywords to be substituted are surrounded by '@': @KEYWORD@.
66 No attempt is made to avoid recursive substitution. The order
67 of evaluation is random based on the order of the keywords returned
68 by the Python dictionary. So do NOT substitute a value that
69 contains any @KEYWORD@ strings expecting them to be recursively
72 for key
, val
in values
.iteritems():
74 contents
= contents
.replace('@' + key
+ '@', val
)
76 print repr(key
), repr(val
)
80 def subst_file(file_name
, values
):
82 Returns the contents of the specified file_name with substited
83 values from the specified dictionary.
85 This is like subst_template, except it operates on a file.
87 template
= open(file_name
, 'r').read()
88 return subst_template(template
, values
);
91 def write_if_changed(file_name
, contents
):
93 Writes the specified contents to the specified file_name
94 iff the contents are different than the current contents.
97 old_contents
= open(file_name
, 'r').read()
98 except EnvironmentError:
101 if contents
== old_contents
:
104 open(file_name
, 'w').write(contents
)
111 short_options
= 'e:f:i:o:t:h'
112 long_options
= ['eval=', 'file=', 'help']
115 Usage: version.py [-h] [-f FILE] ([[-i] FILE] | -t TEMPLATE) [[-o] FILE]
117 -f FILE, --file=FILE Read variables from FILE.
118 -i FILE, --input=FILE Read strings to substitute from FILE.
119 -o FILE, --output=FILE Write substituted strings to FILE.
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'.
124 -h, --help Print this help and exit.
135 opts
, args
= getopt
.getopt(argv
[1:], short_options
, long_options
)
136 except getopt
.error
, msg
:
139 if o
in ('-e', '--eval'):
141 evals
.update(dict([a
.split('=',1)]))
143 raise Usage("-e requires VAR=VAL")
144 elif o
in ('-f', '--file'):
145 variable_files
.append(a
)
146 elif o
in ('-i', '--input'):
148 elif o
in ('-o', '--output'):
150 elif o
in ('-t', '--template'):
152 elif o
in ('-h', '--help'):
155 while len(args
) and (in_file
is None or out_file
is None or
158 in_file
= args
.pop(0)
159 elif out_file
is None:
160 out_file
= args
.pop(0)
162 msg
= 'Unexpected arguments: %r' % args
165 sys
.stderr
.write(err
.msg
)
166 sys
.stderr
.write('; Use -h to get help.\n')
169 values
= fetch_values(variable_files
)
170 for key
, val
in evals
.iteritems():
171 values
[key
] = str(eval(val
, globals(), values
))
173 if template
is not None:
174 contents
= subst_template(template
, values
)
176 contents
= subst_file(in_file
, values
)
178 # Generate a default set of version information.
179 contents
= """MAJOR=%(MAJOR)s
183 LASTCHANGE=%(LASTCHANGE)s
184 OFFICIAL_BUILD=%(OFFICIAL_BUILD)s
189 write_if_changed(out_file
, contents
)
196 if __name__
== '__main__':