updaterevision.py

changeset 67
c34057d3c94e
parent 40
809a75a7113b
equal deleted inserted replaced
66:c68545f1aecb 67:c34057d3c94e
1 #!/usr/bin/env python 1 #!/usr/bin/env python3
2 # coding: utf-8 2 # coding: utf-8
3 # 3 #
4 # Copyright 2015 Teemu Piippo 4 # Copyright 2015 Teemu Piippo
5 # All rights reserved. 5 # All rights reserved.
6 # 6 #
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 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. 30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 # 31 #
32 32
33 import md5, sys 33 import hashlib, sys
34 34
35 class OutputFile: 35 class OutputFile:
36 def __init__ (self, filename): 36 def __init__ (self, filename):
37 self.filename = filename 37 self.filename = filename
38 try: 38 try:
45 45
46 def write (self, a): 46 def write (self, a):
47 self.body += a 47 self.body += a
48 48
49 def save (self): 49 def save (self):
50 checksum = md5.new (self.body).hexdigest() 50 checksum = hashlib.md5(self.body.encode()).hexdigest()
51 51
52 if checksum == self.oldsum: 52 if checksum == self.oldsum:
53 print '%s is up to date' % self.filename 53 print('%s is up to date' % self.filename)
54 return False 54 return False
55 55
56 with open (self.filename, "w") as fp: 56 with open (self.filename, "w") as fp:
57 fp.write ('// %s\n' % checksum) 57 fp.write ('// %s\n' % checksum)
58 fp.write (self.body) 58 fp.write (self.body)
61 def main(): 61 def main():
62 import subprocess 62 import subprocess
63 from datetime import datetime 63 from datetime import datetime
64 64
65 if len (sys.argv) != 2: 65 if len (sys.argv) != 2:
66 print 'usage: %s <output>' % sys.argv[0] 66 print('usage: %s <output>' % sys.argv[0])
67 quit (1) 67 quit (1)
68 68
69 f = OutputFile (sys.argv[1]) 69 f = OutputFile (sys.argv[1])
70 data = subprocess.check_output (['hg', 'log', '-r.', '--template', 70 data = subprocess.check_output (['hg', 'log', '-r.', '--template',
71 '{node|short} {branch} {date|hgdate}']).replace ('\n', '').split (' ') 71 '{node|short} {branch} {date|hgdate}']).decode().replace ('\n', '').split (' ')
72 72
73 rev = data[0] 73 rev = data[0]
74 branch = data[1] 74 branch = data[1]
75 timestamp = int (data[2]) 75 timestamp = int (data[2])
76 date = datetime.utcfromtimestamp (timestamp) 76 date = datetime.utcfromtimestamp (timestamp)
77 datestring = date.strftime ('%y%m%d-%H%M') if date.year >= 2000 else '000000-0000' 77 datestring = date.strftime ('%y%m%d-%H%M') if date.year >= 2000 else '000000-0000'
78 78
79 if len(rev) > 7: 79 if len(rev) > 7:
80 rev = rev[0:7] 80 rev = rev[0:7]
81 81
82 if subprocess.check_output (['hg', 'id', '-n']).replace ('\n', '')[-1] == '+': 82 if subprocess.check_output (['hg', 'id', '-n']).decode().replace ('\n', '')[-1] == '+':
83 rev += '+' 83 rev += '+'
84 84
85 f.write ('#define HG_NODE "%s"\n' % rev) 85 f.write ('#define HG_NODE "%s"\n' % rev)
86 f.write ('#define HG_BRANCH "%s"\n' % branch) 86 f.write ('#define HG_BRANCH "%s"\n' % branch)
87 f.write ('#define HG_DATE_VERSION "%s"\n' % datestring) 87 f.write ('#define HG_DATE_VERSION "%s"\n' % datestring)
88 f.write ('#define HG_DATE_STRING "%s"\n' % date.strftime ('%d %b %Y')) 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)) 89 f.write ('#define HG_DATE_TIME %d\n' % int (timestamp))
90 if f.save(): 90 if f.save():
91 print '%s updated to %s' % (f.filename, rev) 91 print('%s updated to %s' % (f.filename, rev))
92 92
93 if __name__ == '__main__': 93 if __name__ == '__main__':
94 main() 94 main()

mercurial