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