OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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. | |
5 | |
6 """Writes canary version information to a specified file.""" | |
7 | |
8 import datetime | |
9 import sys | |
10 import optparse | |
11 import os | |
12 | |
13 | |
14 def WriteIfChanged(file_name, content): | |
15 """ | |
16 Write |content| to |file_name| iff the content is different from the | |
17 current content. | |
18 """ | |
19 try: | |
20 old_content = open(file_name, 'rb').read() | |
21 except EnvironmentError: | |
22 pass | |
23 else: | |
24 if content == old_content: | |
25 return | |
26 os.unlink(file_name) | |
27 open(file_name, 'wb').write(content) | |
28 | |
29 | |
30 def main(argv=None): | |
31 if argv is None: | |
32 argv = sys.argv | |
33 | |
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 | |
39 | |
40 if args and out_file is None: | |
41 out_file = args.pop(0) | |
42 | |
43 if args: | |
44 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | |
45 parser.print_help() | |
46 sys.exit(2) | |
47 | |
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) | |
51 | |
52 if not out_file: | |
53 sys.stdout.write(content) | |
54 else: | |
55 WriteIfChanged(out_file, content) | |
56 | |
57 if __name__ == '__main__': | |
58 sys.exit(main()) | |
OLD | NEW |