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