3 # getversion.py - Parse version numbers from C header files.
10 __all__
= ['Parser', 'Result']
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')
27 regex
= re
.compile(r
'^\s*#\s*define\s+(\w+)\s+(\d+)')
28 for line
in stream
.readlines():
29 match
= regex
.match(line
)
32 name
= self
.patterns
[match
.group(1)]
35 setattr(result
, name
, int(match
.group(2)))
39 def usage_and_exit(msg
):
41 print >> sys
.stderr
, "%s\n" % msg
42 print >> sys
.stderr
, "usage: %s SVN_VERSION.H" % \
43 os
.path
.basename(sys
.argv
[0])
46 if __name__
== '__main__':
47 if len(sys
.argv
) == 2:
48 include_file
= sys
.argv
[1]
50 usage_and_exit("Incorrect number of arguments")
52 # Extract and print the version number
54 p
.search('SVN_VER_MAJOR', 'major')
55 p
.search('SVN_VER_MINOR', 'minor')
56 p
.search('SVN_VER_PATCH', 'patch')
59 r
= p
.parse(include_file
)
61 usage_and_exit(str(e
))
62 sys
.stdout
.write("%d.%d.%d" % (r
.major
, r
.minor
, r
.patch
))