File: get_version.py

package info (click to toggle)
createrepo-c 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 3,632 kB
  • sloc: ansic: 31,242; python: 4,868; xml: 2,669; sh: 363; makefile: 26; perl: 7
file content (53 lines) | stat: -rwxr-xr-x 1,538 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3

import re
import sys
import os.path
from optparse import OptionParser

VERSION_FILE_PATH = "VERSION.cmake"


def parse(root_dir):
    path = os.path.join(root_dir, VERSION_FILE_PATH)
    if not os.path.exists(path):
        print("File {path} doesn't exist".format(path=path))
        return None

    content = open(path, "r").read()
    ver = {}
    ver['major'] = re.search(r'SET\s*\(CR_MAJOR\s+"(\d+)"', content).group(1)
    ver['minor'] = re.search(r'SET\s*\(CR_MINOR\s+"(\d+)"', content).group(1)
    ver['patch'] = re.search(r'SET\s*\(CR_PATCH\s+"(\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 {path} doesn't exist".format(path=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}.{minor}.{patch}".format(**ver))

    sys.exit(0)