updaterevision.py

Tue, 03 Mar 2015 16:55:36 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 03 Mar 2015 16:55:36 +0200
changeset 931
85080f7a1c20
parent 926
e1ae2dbcae8e
permissions
-rw-r--r--

- renamed .cc files to .cpp

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

mercurial