updaterevision/updaterevision.py

changeset 201
1bfa1cdffb02
parent 124
c5ff5a4704dd
equal deleted inserted replaced
200:3fb775db4829 201:1bfa1cdffb02
1 #!/usr/bin/env python
2 # coding: utf-8
1 # 3 #
2 # Copyright 2014 Teemu Piippo 4 # Copyright 2015 Teemu Piippo
3 # All rights reserved. 5 # All rights reserved.
4 # 6 #
5 # Redistribution and use in source and binary forms, with or without 7 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions 8 # modification, are permitted provided that the following conditions
7 # are met: 9 # are met:
21 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
22 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
27 # 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
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # 31 #
30 32
31 import sys 33 import argparse
32 import subprocess 34 import outputfile
33 from datetime import datetime
34 35
35 if len (sys.argv) != 2: 36 if __name__ == '__main__':
36 print 'usage: %s <output>' % sys.argv[0] 37 import subprocess
37 quit (1) 38 from datetime import datetime
38 39 parser = argparse.ArgumentParser(description='Writes a header file with Hg commit information')
39 oldcompare = '' 40 parser.add_argument('--cwd', default = '.')
40 41 parser.add_argument('output')
41 try: 42 args = parser.parse_args()
42 with open (sys.argv[1]) as fp: 43 data = subprocess.check_output(['hg', 'log', '--cwd', args.cwd, '-r.', '--template',
43 oldcompare = fp.readline().replace ('\n', '').replace ('// ', '') 44 '{node|short} {branch} {date|hgdate} {tags}']).decode().replace('\n', '').split()
44 except IOError: 45 rev = data[0]
45 pass 46 branch = data[1]
46 47 timestamp = int(data[2])
47 delim='@'*10 48 all_tags = set(data[4:])
48 rev, branch, timestampstr, tags = subprocess.check_output (['hg', 'log', '-r.', '--template', 49 try:
49 delim.join (['{node|short}', '{branch}', '{date|hgdate}', '{tags}'])]).replace ('\n', '').split (delim) 50 version_tag = [tag for tag in all_tags if tag.startswith('v')][0]
50 51 except IndexError:
51 timestamp = int (timestampstr.split(' ')[0]) 52 version_tag = None
52 date = datetime.utcfromtimestamp (timestamp) 53 date = datetime.utcfromtimestamp(timestamp)
53 datestring = date.strftime ('%y%m%d-%H%M') if date.year >= 2000 else '000000-0000' 54 datestring = date.strftime('%y%m%d-%H%M') if date.year >= 2000 else '000000-0000'
54 55 if len(rev) > 7:
55 if tags and tags != 'tip': 56 rev = rev[:7]
56 tag = tags.split(' ')[0] 57 modified = subprocess.check_output(['hg', 'id', '--cwd', args.cwd, '-n']).decode().strip().endswith('+')
57 else: 58 if modified:
58 tag = None 59 rev += '+'
59 60 # if the source tree is modified, it's not the released version
60 if len(rev) > 7: 61 version_tag = None
61 rev = rev[0:7] 62 if version_tag is not None:
62 63 # tags should be the same in released versions whether or not it's tip
63 if subprocess.check_output (['hg', 'id', '-n']).replace ('\n', '')[-1] == '+': 64 # (though released versions shouldn't be tip because hgtags is modified
64 rev += '+' 65 # to create the tag 🤔, but let's do this anyway)
65 66 all_tags -= {'tip'}
66 compare = tag or rev 67 with outputfile.OutputFile(args.output, verbose = True) as f:
67 68 f.write(f'#define HG_NODE "{rev}"\n')
68 if compare == oldcompare: 69 if branch != 'default':
69 print "%s is up to date at %s" % (sys.argv[1], compare) 70 f.write(f'#define HG_BRANCH "{branch}"\n')
70 quit (0) 71 f.write(f'#define HG_DATE_VERSION "{datestring}"\n')
71 72 f.write(f'#define HG_DATE_TIME {int(timestamp)}\n')
72 with open (sys.argv[1], 'w') as fp: 73 datestring = date.strftime('%d %b %Y')
73 fp.write ('// %s\n' % compare) 74 f.write(f'#define HG_DATE_STRING "{datestring}"\n')
74 fp.write ('#define HG_NODE "%s"\n' % rev) 75 if all_tags:
75 fp.write ('#define HG_BRANCH "%s"\n' % branch) 76 f.write(f'#define HG_ALL_TAGS "{" ".join(sorted(all_tags))}"\n')
76 fp.write ('#define HG_DATE_VERSION "%s"\n' % datestring) 77 if version_tag:
77 fp.write ('#define HG_DATE_STRING "%s"\n' % date.strftime ('%d %b %Y')) 78 f.write(f'#define HG_VERSION_TAG "{version_tag[1:]}"\n')
78 fp.write ('#define HG_DATE_TIME %d\n' % int (timestamp)) 79 if 'tip' in all_tags:
79 80 f.write('#define HG_TIP\n')
80 if tag: 81 print(f'Current Hg revision: {rev}')
81 fp.write ('#define HG_TAG "%s"\n' % tags.split(' ')[0])
82
83 print '%s updated to %s' % (sys.argv[1], compare)

mercurial