updaterevision.py

changeset 40
809a75a7113b
child 67
c34057d3c94e
equal deleted inserted replaced
39:2c368cf5cc19 40:809a75a7113b
1 #!/usr/bin/env python
2 # coding: utf-8
3 #
4 # Copyright 2015 Teemu Piippo
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # 3. Neither the name of the copyright holder nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
24 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32
33 import md5, sys
34
35 class OutputFile:
36 def __init__ (self, filename):
37 self.filename = filename
38 try:
39 with open (self.filename, "r") as fp:
40 self.oldsum = fp.readline().replace ('\n', '').replace ('// ', '')
41 except IOError:
42 self.oldsum = ''
43
44 self.body = ''
45
46 def write (self, a):
47 self.body += a
48
49 def save (self):
50 checksum = md5.new (self.body).hexdigest()
51
52 if checksum == self.oldsum:
53 print '%s is up to date' % self.filename
54 return False
55
56 with open (self.filename, "w") as fp:
57 fp.write ('// %s\n' % checksum)
58 fp.write (self.body)
59 return True
60
61 def main():
62 import subprocess
63 from datetime import datetime
64
65 if len (sys.argv) != 2:
66 print 'usage: %s <output>' % sys.argv[0]
67 quit (1)
68
69 f = OutputFile (sys.argv[1])
70 data = subprocess.check_output (['hg', 'log', '-r.', '--template',
71 '{node|short} {branch} {date|hgdate}']).replace ('\n', '').split (' ')
72
73 rev = data[0]
74 branch = data[1]
75 timestamp = int (data[2])
76 date = datetime.utcfromtimestamp (timestamp)
77 datestring = date.strftime ('%y%m%d-%H%M') if date.year >= 2000 else '000000-0000'
78
79 if len(rev) > 7:
80 rev = rev[0:7]
81
82 if subprocess.check_output (['hg', 'id', '-n']).replace ('\n', '')[-1] == '+':
83 rev += '+'
84
85 f.write ('#define HG_NODE "%s"\n' % rev)
86 f.write ('#define HG_BRANCH "%s"\n' % branch)
87 f.write ('#define HG_DATE_VERSION "%s"\n' % datestring)
88 f.write ('#define HG_DATE_STRING "%s"\n' % date.strftime ('%d %b %Y'))
89 f.write ('#define HG_DATE_TIME %d\n' % int (timestamp))
90 if f.save():
91 print '%s updated to %s' % (f.filename, rev)
92
93 if __name__ == '__main__':
94 main()

mercurial