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
4 # found in the LICENSE file.
6 """Writes canary version information to a specified file."""
14 def WriteIfChanged(file_name
, content
):
16 Write |content| to |file_name| iff the content is different from the
20 old_content
= open(file_name
, 'rb').read()
21 except EnvironmentError:
24 if content
== old_content
:
27 open(file_name
, 'wb').write(content
)
34 parser
= optparse
.OptionParser(usage
="canary_version.py [options]")
35 parser
.add_option("-o", "--output", metavar
="FILE",
36 help="write patch level to FILE")
37 opts
, args
= parser
.parse_args(argv
[1:])
38 out_file
= opts
.output
40 if args
and out_file
is None:
41 out_file
= args
.pop(0)
44 sys
.stderr
.write('Unexpected arguments: %r\n\n' % args
)
48 # Number of days since January 1st, 2012.
49 day_number
= (datetime
.date
.today() - datetime
.date(2012, 1, 1)).days
50 content
= "PATCH={}\n".format(day_number
)
53 sys
.stdout
.write(content
)
55 WriteIfChanged(out_file
, content
)
57 if __name__
== '__main__':