prepare_packaging.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. # replace all '-' with '~' in version
  32. changelog_version = changelog_version.replace('-', '~')
  33. with open(changelog_file, 'r') as original: data = original.read()
  34. with open(changelog_file, 'w') as modified:
  35. new_entry = """open62541 ({version}) {distribution}; urgency=medium
  36. * Full changelog is available here:
  37. https://github.com/open62541/open62541/blob/master/CHANGELOG
  38. -- open62541 Team <open62541-core@googlegroups.com> {time}
  39. """.format(version=changelog_version, time=formatdate(), distribution = debian_distribution)
  40. modified.write(new_entry + "\n" + data)
  41. # Create control file and replace template variables
  42. control_file_template = os.path.join(debian_path, "control-template")
  43. control_file = os.path.join(debian_path, "control")
  44. os.rename(control_file_template, control_file)
  45. with open(control_file, 'r+') as f:
  46. content = f.read()
  47. f.seek(0)
  48. f.truncate()
  49. f.write(content.replace('<soname>', "{}".format(version_major)))
  50. # rename the install template to match the soname
  51. install_file_template = os.path.join(debian_path, "libopen62541.install-template")
  52. install_file = os.path.join(debian_path, "libopen62541-{}.install".format(version_major))
  53. os.rename(install_file_template, install_file)
  54. install_file_template = os.path.join(debian_path, "libopen62541-dev.install-template")
  55. install_file = os.path.join(debian_path, "libopen62541-{}-dev.install".format(version_major))
  56. os.rename(install_file_template, install_file)
  57. # Create rule file and replace template variables
  58. rule_file_template = os.path.join(debian_path, "rules-template")
  59. rule_file = os.path.join(debian_path, "rules")
  60. os.rename(rule_file_template, rule_file)
  61. with open(rule_file, 'r+') as f:
  62. content = f.read()
  63. f.seek(0)
  64. f.truncate()
  65. content = content.replace('<soname>', "{}".format(version_major))
  66. f.write(content)
  67. # Update CMakeLists.txt to include full version string
  68. with open(os.path.join(dirpath,"CMakeLists.txt"), 'r+') as f:
  69. lines = f.readlines()
  70. f.seek(0)
  71. f.truncate()
  72. for idx, line in enumerate(lines):
  73. if idx == 2:
  74. f.write('set(OPEN62541_VERSION "{}")\n'.format(git_describe_version))
  75. f.write(line)