31 # |
31 # |
32 |
32 |
33 import argparse |
33 import argparse |
34 import outputfile |
34 import outputfile |
35 |
35 |
36 def main(): |
36 if __name__ == '__main__': |
37 import subprocess |
37 import subprocess |
38 from datetime import datetime |
38 from datetime import datetime |
39 parser = argparse.ArgumentParser(description='Writes a header file with Hg commit information') |
39 parser = argparse.ArgumentParser(description='Writes a header file with Hg commit information') |
40 parser.add_argument('--cwd', default = '.') |
40 parser.add_argument('--cwd', default = '.') |
41 parser.add_argument('output') |
41 parser.add_argument('output') |
42 args = parser.parse_args() |
42 args = parser.parse_args() |
43 f = outputfile.OutputFile(args.output) |
|
44 data = subprocess.check_output(['hg', 'log', '--cwd', args.cwd, '-r.', '--template', |
43 data = subprocess.check_output(['hg', 'log', '--cwd', args.cwd, '-r.', '--template', |
45 '{node|short} {branch} {date|hgdate}']).decode().replace('\n', '').split(' ') |
44 '{node|short} {branch} {date|hgdate} {tags}']).decode().replace('\n', '').split() |
46 |
|
47 rev = data[0] |
45 rev = data[0] |
48 branch = data[1] |
46 branch = data[1] |
49 timestamp = int(data[2]) |
47 timestamp = int(data[2]) |
|
48 all_tags = set(data[4:]) |
|
49 try: |
|
50 version_tag = [tag for tag in all_tags if tag.startswith('v')][0] |
|
51 except IndexError: |
|
52 version_tag = None |
50 date = datetime.utcfromtimestamp(timestamp) |
53 date = datetime.utcfromtimestamp(timestamp) |
51 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' |
52 |
|
53 if len(rev) > 7: |
55 if len(rev) > 7: |
54 rev = rev[0:7] |
56 rev = rev[:7] |
55 |
57 modified = subprocess.check_output(['hg', 'id', '--cwd', args.cwd, '-n']).decode().strip().endswith('+') |
56 if subprocess.check_output(['hg', 'id', '--cwd', args.cwd, '-n']).decode().replace('\n', '').endswith('+'): |
58 if modified: |
57 rev += '+' |
59 rev += '+' |
58 |
60 # if the source tree is modified, it's not the released version |
59 f.write('#define HG_NODE "%s"\n' % rev) |
61 version_tag = None |
60 f.write('#define HG_BRANCH "%s"\n' % branch) |
62 if version_tag is not None: |
61 f.write('#define HG_DATE_VERSION "%s"\n' % datestring) |
63 # tags should be the same in released versions whether or not it's tip |
62 f.write('#define HG_DATE_STRING "%s"\n' % date.strftime('%d %b %Y')) |
64 # (though released versions shouldn't be tip because hgtags is modified |
63 f.write('#define HG_DATE_TIME %d\n' % int(timestamp)) |
65 # to create the tag 🤔, but let's do this anyway) |
64 if f.save(): |
66 all_tags -= {'tip'} |
65 print('%s updated to %s' %(f.filename, rev)) |
67 with outputfile.OutputFile(args.output, verbose = True) as f: |
66 |
68 f.write(f'#define HG_NODE "{rev}"\n') |
67 if __name__ == '__main__': |
69 if branch != 'default': |
68 main() |
70 f.write(f'#define HG_BRANCH "{branch}"\n') |
|
71 f.write(f'#define HG_DATE_VERSION "{datestring}"\n') |
|
72 f.write(f'#define HG_DATE_TIME {int(timestamp)}\n') |
|
73 if all_tags: |
|
74 f.write(f'#define HG_ALL_TAGS "{" ".join(sorted(all_tags))}"\n') |
|
75 if version_tag: |
|
76 f.write(f'#define HG_VERSION_TAG "{version_tag[1:]}"\n') |
|
77 if 'tip' in all_tags: |
|
78 f.write('#define HG_TIP\n') |
|
79 print(f'Current Hg revision: {rev}') |