get-version.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. #
  3. # Opus get-version.py
  4. #
  5. # Extracts versions for build:
  6. # - Opus package version based on 'git describe' or $srcroot/package_version
  7. # - libtool version based on configure.ac
  8. # - macos lib version based on configure.ac
  9. #
  10. # Usage:
  11. # get-version.py [--package-version | --libtool-version | --darwin-version]
  12. import argparse
  13. import subprocess
  14. import os
  15. import sys
  16. import shutil
  17. if __name__ == '__main__':
  18. arg_parser = argparse.ArgumentParser(description='Extract Opus package version or libtool version')
  19. group = arg_parser.add_mutually_exclusive_group(required=True)
  20. group.add_argument('--libtool-version', action='store_true')
  21. group.add_argument('--package-version', action='store_true')
  22. group.add_argument('--darwin-version', action='store_true')
  23. args = arg_parser.parse_args()
  24. srcroot = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
  25. # package version
  26. if args.package_version:
  27. package_version = None
  28. # check if git checkout
  29. git_dir = os.path.join(srcroot, '.git')
  30. is_git = os.path.isdir(git_dir) or os.path.isfile(git_dir)
  31. have_git = shutil.which('git') is not None
  32. if is_git and have_git:
  33. git_cmd = subprocess.run(['git', '--git-dir=' + git_dir, 'describe', 'HEAD'], stdout=subprocess.PIPE)
  34. if git_cmd.returncode:
  35. print('ERROR: Could not extract package version via `git describe` in', srcroot, file=sys.stderr)
  36. sys.exit(-1)
  37. package_version = git_cmd.stdout.decode('ascii').strip().lstrip('v')
  38. else:
  39. with open(os.path.join(srcroot, 'package_version'), 'r') as f:
  40. for line in f:
  41. if line.startswith('PACKAGE_VERSION="'):
  42. package_version = line[17:].strip().lstrip('v').rstrip('"')
  43. if package_version:
  44. break
  45. if not package_version:
  46. print('ERROR: Could not extract package version from package_version file in', srcroot, file=sys.stderr)
  47. sys.exit(-1)
  48. print(package_version)
  49. sys.exit(0)
  50. # libtool version + darwin version
  51. elif args.libtool_version or args.darwin_version:
  52. opus_lt_cur = None
  53. opus_lt_rev = None
  54. opus_lt_age = None
  55. with open(os.path.join(srcroot, 'configure.ac'), 'r') as f:
  56. for line in f:
  57. if line.strip().startswith('OPUS_LT_CURRENT='):
  58. opus_lt_cur = line[16:].strip()
  59. elif line.strip().startswith('OPUS_LT_REVISION='):
  60. opus_lt_rev = line[17:].strip()
  61. elif line.strip().startswith('OPUS_LT_AGE='):
  62. opus_lt_age = line[12:].strip()
  63. if opus_lt_cur and opus_lt_rev and opus_lt_age:
  64. opus_lt_cur = int(opus_lt_cur)
  65. opus_lt_rev = int(opus_lt_rev)
  66. opus_lt_age = int(opus_lt_age)
  67. if args.libtool_version:
  68. print('{}.{}.{}'.format(opus_lt_cur - opus_lt_age, opus_lt_age, opus_lt_rev))
  69. elif args.darwin_version:
  70. print('{}.{}.{}'.format(opus_lt_cur + 1, 0, 0))
  71. sys.exit(0)
  72. else:
  73. print('ERROR: Could not extract libtool version from configure.ac file in', srcroot, file=sys.stderr)
  74. sys.exit(-1)
  75. else:
  76. sys.exit(-1)