Add get_version.py script into utils
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 24 May 2012 09:10:53 +0000 (11:10 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 24 May 2012 09:10:53 +0000 (11:10 +0200)
utils/get_version.py [new file with mode: 0755]

diff --git a/utils/get_version.py b/utils/get_version.py
new file mode 100755 (executable)
index 0000000..512c706
--- /dev/null
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+
+import re
+import sys
+import os.path
+from optparse import OptionParser
+
+VERSION_H_PATH = "src/version.h"
+
+
+def parse(root_dir):
+    path = os.path.join(root_dir, VERSION_H_PATH)
+    if not os.path.exists(path):
+        print "File %s doesn't exists" % path
+        return None
+
+    content = open(path, "r").read()
+    ver = {}
+    ver['major'] = re.search('#define MAJOR_VERSION +(\d)', content).group(1)
+    ver['minor'] = re.search('#define MINOR_VERSION +(\d)', content).group(1)
+    ver['patch'] = re.search('#define PATCH_VERSION +(\d)', content).group(1)
+    return ver
+
+
+if __name__ == "__main__":
+    parser = OptionParser("usage: %prog <project_root_dir> [--major|--minor|--patch]")
+    parser.add_option("--major", action="store_true", help="Return major version")
+    parser.add_option("--minor", action="store_true", help="Return minor version")
+    parser.add_option("--patch", action="store_true", help="Return patch version")
+    options, args = parser.parse_args()
+
+    if len(args) != 1:
+        parser.error("Must specify a project root directory")
+
+    path = args[0]
+
+    if not os.path.isdir(path):
+        parser.error("Directory %s doesn't exists" % path)
+
+    ver = parse(path)
+    if ver is None:
+        sys.exit(1)
+
+    if options.major:
+        print ver['major']
+    elif options.minor:
+        print ver['minor']
+    elif options.patch:
+        print ver['patch']
+    else:
+        print "%(major)s.%(minor)s.%(patch)s" % ver
+
+    sys.exit(0)