updaterevision.py

Sun, 30 Aug 2015 15:17:28 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 30 Aug 2015 15:17:28 +0300
changeset 967
eb586d3e1a6a
parent 949
a9ba8ffd9534
permissions
-rw-r--r--

elif -> else if
Removed a bunch of long unused macros
Replaced the countof implementation with the more idiomatic one

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

mercurial