• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2020-2023 by
4# David Turner, Robert Wilhelm, and Werner Lemberg.
5#
6# This file is part of the FreeType project, and may only be used, modified,
7# and distributed under the terms of the FreeType project license,
8# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
9# indicate that you have read the license and understand and accept it
10# fully.
11
12"""Extract the libtool version from `configure.raw`.
13
14This script parses the `configure.raw` file to extract the libtool version
15number.  By default, the full dotted version number is printed, but
16`--major`, `--minor` or `--patch` can be used to only print one of these
17values instead.
18"""
19
20from __future__ import print_function
21
22import argparse
23import os
24import re
25import sys
26
27# Expected input:
28#
29#  ...
30#  version_info='23:2:17'
31#  ...
32
33RE_VERSION_INFO = re.compile(r"^version_info='(\d+):(\d+):(\d+)'")
34
35
36def parse_configure_raw(header):
37    major = None
38    minor = None
39    patch = None
40
41    for line in header.splitlines():
42        line = line.rstrip()
43        m = RE_VERSION_INFO.match(line)
44        if m:
45            assert major == None, "version_info appears more than once!"
46            major = m.group(1)
47            minor = m.group(2)
48            patch = m.group(3)
49            continue
50
51    assert (
52        major and minor and patch
53    ), "This input file is missing a version_info definition!"
54
55    return (major, minor, patch)
56
57
58def main():
59    parser = argparse.ArgumentParser(description=__doc__)
60
61    group = parser.add_mutually_exclusive_group()
62    group.add_argument(
63        "--major",
64        action="store_true",
65        help="Only print the major version number.",
66    )
67    group.add_argument(
68        "--minor",
69        action="store_true",
70        help="Only print the minor version number.",
71    )
72    group.add_argument(
73        "--patch",
74        action="store_true",
75        help="Only print the patch version number.",
76    )
77    group.add_argument(
78        "--soversion",
79        action="store_true",
80        help="Only print the libtool library suffix.",
81    )
82
83    parser.add_argument(
84        "input",
85        metavar="CONFIGURE_RAW",
86        help="The input configure.raw file to parse.",
87    )
88
89    args = parser.parse_args()
90    with open(args.input) as f:
91        raw_file = f.read()
92
93    version = parse_configure_raw(raw_file)
94
95    if args.major:
96        print(version[0])
97    elif args.minor:
98        print(version[1])
99    elif args.patch:
100        print(version[2])
101    elif args.soversion:
102        # Convert libtool version_info to the library suffix.
103        # (current,revision, age) -> (current - age, age, revision)
104        print(
105            "%d.%s.%s"
106            % (int(version[0]) - int(version[2]), version[2], version[1])
107        )
108    else:
109        print("%s.%s.%s" % version)
110
111    return 0
112
113
114if __name__ == "__main__":
115    sys.exit(main())
116