/Users/maurits/Documents/studie/afstuderen/biosphere/build/getversion.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # getversion.py - Parse version numbers from C header files.
00004 #
00005 # This file is based on getversion from the subversion tarball.
00006 # For subversion see: http://subversion.tigris.org.
00007 #
00008 #
00009 # Copyright (c) 2006 Maurits Hartman
00010 #
00011 # Permission is hereby granted, free of charge, to any person
00012 # obtaining a copy of this software and associated documentation
00013 # files (the "Software"), to deal in the Software without
00014 # restriction, including without limitation the rights to use,
00015 # copy, modify, merge, publish, distribute, sublicense, and/or sell
00016 # copies of the Software, and to permit persons to whom the
00017 # Software is furnished to do so, subject to the following
00018 # conditions:
00019 # 
00020 # The above copyright notice and this permission notice shall be
00021 # included in all copies or substantial portions of the Software.
00022 # 
00023 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00024 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00025 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00026 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00027 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00028 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00029 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00030 # OTHER DEALINGS IN THE SOFTWARE.
00031 #
00032 
00033 import re
00034 
00035 __all__ = ['Parser', 'Result']
00036 
00037 class Result:
00038   pass
00039 
00040 class Parser:
00041   def __init__(self):
00042     self.patternspatterns = {}
00043 
00044   def search(self, define_name, value_name):
00045     'Add the name of a define to the list of search pattenrs.'
00046     self.patternspatterns[define_name] = value_name
00047 
00048   def parse(self, file):
00049     'Parse the file, extracting defines into a Result object.'
00050     stream = open(file, 'rt')
00051     result = Result()
00052     regex = re.compile(r'^\s*#\s*define\s+(\w+)\s+(\d+)')
00053     for line in stream.readlines():
00054       match = regex.match(line)
00055       if match:
00056         try:
00057           name = self.patternspatterns[match.group(1)]
00058         except:
00059           continue
00060         setattr(result, name, int(match.group(2)))
00061     stream.close()
00062     return result
00063 
00064 
00065 if __name__ == '__main__':
00066   # Extract and print the version number
00067   p = Parser()
00068   p.search('BIOSPHERE_VERSION_MAJOR', 'major')
00069   p.search('BIOSPHERE_VERSION_MINOR', 'minor')
00070   p.search('BIOSPHERE_VERSION_PATCH', 'patch')
00071 
00072   import os, sys
00073   r = p.parse(sys.argv[1])
00074   if len(sys.argv) > 2:
00075     sys.stdout.write("%d.%d" % (r.major, r.minor))
00076   else:
00077     sys.stdout.write("%d.%d.%d" % (r.major, r.minor, r.patch))
00078 
00079 ### End of file.

Generated on Tue Jul 17 09:50:52 2007 for Bio-SPHERE by  doxygen 1.5.1