updaterevision/updaterevision.py

Mon, 10 Apr 2023 10:42:45 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 10 Apr 2023 10:42:45 +0300
changeset 201
1bfa1cdffb02
parent 124
c5ff5a4704dd
permissions
-rw-r--r--

Now builds again

201
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
1 #!/usr/bin/env python
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
2 # coding: utf-8
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
3 #
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
4 # Copyright 2015 Teemu Piippo
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
5 # All rights reserved.
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
6 #
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
7 # Redistribution and use in source and binary forms, with or without
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
8 # modification, are permitted provided that the following conditions
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
9 # are met:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
10 #
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
11 # 1. Redistributions of source code must retain the above copyright
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
12 # notice, this list of conditions and the following disclaimer.
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
13 # 2. Redistributions in binary form must reproduce the above copyright
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
14 # notice, this list of conditions and the following disclaimer in the
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
15 # documentation and/or other materials provided with the distribution.
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
16 # 3. Neither the name of the copyright holder nor the names of its
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
17 # contributors may be used to endorse or promote products derived from
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
18 # this software without specific prior written permission.
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
19 #
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
24 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
31 #
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
32
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
33 import argparse
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
34 import outputfile
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
35
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
36 if __name__ == '__main__':
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
37 import subprocess
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
38 from datetime import datetime
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
39 parser = argparse.ArgumentParser(description='Writes a header file with Hg commit information')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
40 parser.add_argument('--cwd', default = '.')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
41 parser.add_argument('output')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
42 args = parser.parse_args()
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
43 data = subprocess.check_output(['hg', 'log', '--cwd', args.cwd, '-r.', '--template',
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
44 '{node|short} {branch} {date|hgdate} {tags}']).decode().replace('\n', '').split()
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
45 rev = data[0]
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
46 branch = data[1]
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
47 timestamp = int(data[2])
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
48 all_tags = set(data[4:])
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
49 try:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
50 version_tag = [tag for tag in all_tags if tag.startswith('v')][0]
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
51 except IndexError:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
52 version_tag = None
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
53 date = datetime.utcfromtimestamp(timestamp)
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
54 datestring = date.strftime('%y%m%d-%H%M') if date.year >= 2000 else '000000-0000'
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
55 if len(rev) > 7:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
56 rev = rev[:7]
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
57 modified = subprocess.check_output(['hg', 'id', '--cwd', args.cwd, '-n']).decode().strip().endswith('+')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
58 if modified:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
59 rev += '+'
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
60 # if the source tree is modified, it's not the released version
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
61 version_tag = None
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
62 if version_tag is not None:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
63 # tags should be the same in released versions whether or not it's tip
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
64 # (though released versions shouldn't be tip because hgtags is modified
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
65 # to create the tag 🤔, but let's do this anyway)
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
66 all_tags -= {'tip'}
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
67 with outputfile.OutputFile(args.output, verbose = True) as f:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
68 f.write(f'#define HG_NODE "{rev}"\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
69 if branch != 'default':
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
70 f.write(f'#define HG_BRANCH "{branch}"\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
71 f.write(f'#define HG_DATE_VERSION "{datestring}"\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
72 f.write(f'#define HG_DATE_TIME {int(timestamp)}\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
73 datestring = date.strftime('%d %b %Y')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
74 f.write(f'#define HG_DATE_STRING "{datestring}"\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
75 if all_tags:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
76 f.write(f'#define HG_ALL_TAGS "{" ".join(sorted(all_tags))}"\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
77 if version_tag:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
78 f.write(f'#define HG_VERSION_TAG "{version_tag[1:]}"\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
79 if 'tip' in all_tags:
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
80 f.write('#define HG_TIP\n')
1bfa1cdffb02 Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 124
diff changeset
81 print(f'Current Hg revision: {rev}')

mercurial