* tools/hook-scripts/commit-email.pl.in
[svn.git] / build / getversion.py
blob9e0e26a89f64a88d5488cf618761d33a276ab6b8
1 #!/usr/bin/env python
3 # getversion.py - Parse version numbers from C header files.
6 import os
7 import re
8 import sys
10 __all__ = ['Parser', 'Result']
12 class Result:
13 pass
15 class Parser:
16 def __init__(self):
17 self.patterns = {}
19 def search(self, define_name, value_name):
20 'Add the name of a define to the list of search pattenrs.'
21 self.patterns[define_name] = value_name
23 def parse(self, file):
24 'Parse the file, extracting defines into a Result object.'
25 stream = open(file, 'rt')
26 result = Result()
27 regex = re.compile(r'^\s*#\s*define\s+(\w+)\s+(\d+)')
28 for line in stream.readlines():
29 match = regex.match(line)
30 if match:
31 try:
32 name = self.patterns[match.group(1)]
33 except:
34 continue
35 setattr(result, name, int(match.group(2)))
36 stream.close()
37 return result
39 def usage_and_exit(msg):
40 if msg:
41 print >> sys.stderr, "%s\n" % msg
42 print >> sys.stderr, "usage: %s SVN_VERSION.H" % \
43 os.path.basename(sys.argv[0])
44 sys.exit(1)
46 if __name__ == '__main__':
47 if len(sys.argv) == 2:
48 include_file = sys.argv[1]
49 else:
50 usage_and_exit("Incorrect number of arguments")
52 # Extract and print the version number
53 p = Parser()
54 p.search('SVN_VER_MAJOR', 'major')
55 p.search('SVN_VER_MINOR', 'minor')
56 p.search('SVN_VER_PATCH', 'patch')
58 try:
59 r = p.parse(include_file)
60 except IOError, e:
61 usage_and_exit(str(e))
62 sys.stdout.write("%d.%d.%d" % (r.major, r.minor, r.patch))