prepare_packaging.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. import subprocess
  6. import os
  7. import re
  8. from email.utils import formatdate
  9. dirpath = os.path.join(os.path.dirname(os.path.realpath(__file__)),"..")
  10. git_describe_version = subprocess.check_output(["git", "describe", "--tags", "--dirty", "--match", "v*"]).decode('utf-8').strip()
  11. # v1.2
  12. # v1.2.3
  13. # v1.2.3-rc1
  14. # v1.2.3-rc1-dirty
  15. # v1.2.3-5-g4538abcd
  16. # v1.2.3-5-g4538abcd-dirty
  17. # git_describe_version = "v1.2.3"
  18. m = re.match(r"^v([0-9]+)(\.[0-9]+)?(\.[0-9]+)?(-(.*)+)?$", git_describe_version)
  19. version_major = m.group(1) if m.group(1) is not None else "0"
  20. version_minor = m.group(2).replace(".", "") if m.group(2) is not None else "0"
  21. version_patch = m.group(3).replace(".", "") if m.group(3) is not None else "0"
  22. version_label = m.group(4) if m.group(4) is not None else ""
  23. #print("major {} minor {} patch {} label {}".format(version_major, version_minor, version_patch, version_label))
  24. debian_distribution = "unstable"
  25. if version_label is not "":
  26. debian_distribution = "UNRELEASED"
  27. debian_path = os.path.join(dirpath, "debian")
  28. changelog_file = os.path.join(debian_path, "changelog")
  29. # remove leading 'v'
  30. changelog_version = git_describe_version[1:] if git_describe_version[0] == 'v' else git_describe_version
  31. with open(changelog_file, 'r') as original: data = original.read()
  32. with open(changelog_file, 'w') as modified:
  33. new_entry = """open62541 ({version}) {distribution}; urgency=medium
  34. * Full changelog is available here: https://github.com/open62541/open62541/blob/master/CHANGELOG
  35. -- open62541 Team <open62541-core@googlegroups.com> {time}
  36. """.format(version=changelog_version, time=formatdate(), distribution = debian_distribution)
  37. modified.write(new_entry + "\n" + data)
  38. # Create control file and replace template variables
  39. control_file_template = os.path.join(debian_path, "control-template")
  40. control_file = os.path.join(debian_path, "control")
  41. os.rename(control_file_template, control_file)
  42. with open(control_file, 'r+') as f:
  43. content = f.read()
  44. f.seek(0)
  45. f.truncate()
  46. f.write(content.replace('<soname>', "{}".format(version_major)))
  47. # rename the install template to match the soname
  48. install_file_template = os.path.join(debian_path, "libopen62541.install-template")
  49. install_file = os.path.join(debian_path, "libopen62541-{}.install".format(version_major))
  50. os.rename(install_file_template, install_file)
  51. install_file_template = os.path.join(debian_path, "libopen62541-dev.install-template")
  52. install_file = os.path.join(debian_path, "libopen62541-{}-dev.install".format(version_major))
  53. os.rename(install_file_template, install_file)
  54. # Create rule file and replace template variables
  55. rule_file_template = os.path.join(debian_path, "rules-template")
  56. rule_file = os.path.join(debian_path, "rules")
  57. os.rename(rule_file_template, rule_file)
  58. with open(rule_file, 'r+') as f:
  59. content = f.read()
  60. f.seek(0)
  61. f.truncate()
  62. content = content.replace('<soname>', "{}".format(version_major))
  63. f.write(content)
  64. # Update CMakeLists.txt to include full version string
  65. with open(os.path.join(dirpath,"CMakeLists.txt"), 'r+') as f:
  66. lines = f.readlines()
  67. f.seek(0)
  68. f.truncate()
  69. for idx, line in enumerate(lines):
  70. if idx == 2:
  71. f.write('set(OPEN62541_VERSION "{}")\n'.format(git_describe_version))
  72. f.write(line)