prepare_packaging.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. # We can not use unstable for now, because dpkg-buildpackage wants to sign them
  25. # It will fail with gpg: skipped "open62541 Team <open62541-core@googlegroups.com>": No secret key
  26. #debian_distribution = "unstable"
  27. #if version_label is not "":
  28. debian_distribution = "UNRELEASED"
  29. debian_path = os.path.join(dirpath, "debian")
  30. changelog_file = os.path.join(debian_path, "changelog")
  31. # remove leading 'v'
  32. changelog_version = git_describe_version[1:] if git_describe_version[0] == 'v' else git_describe_version
  33. # replace all '-' with '~' in version
  34. changelog_version = changelog_version.replace('-', '~')
  35. with open(changelog_file, 'r') as original: data = original.read()
  36. with open(changelog_file, 'w') as modified:
  37. new_entry = """open62541 ({version}) {distribution}; urgency=medium
  38. * Full changelog is available here:
  39. https://github.com/open62541/open62541/blob/master/CHANGELOG
  40. -- open62541 Team <open62541-core@googlegroups.com> {time}
  41. """.format(version=changelog_version, time=formatdate(), distribution = debian_distribution)
  42. modified.write(new_entry + "\n" + data)
  43. # Create control file and replace template variables
  44. control_file_template = os.path.join(debian_path, "control-template")
  45. control_file = os.path.join(debian_path, "control")
  46. os.rename(control_file_template, control_file)
  47. with open(control_file, 'r+') as f:
  48. content = f.read()
  49. f.seek(0)
  50. f.truncate()
  51. f.write(content.replace('<soname>', "{}".format(version_major)))
  52. # rename the install template to match the soname
  53. install_file_template = os.path.join(debian_path, "libopen62541.install-template")
  54. install_file = os.path.join(debian_path, "libopen62541-{}.install".format(version_major))
  55. os.rename(install_file_template, install_file)
  56. install_file_template = os.path.join(debian_path, "libopen62541-dev.install-template")
  57. install_file = os.path.join(debian_path, "libopen62541-{}-dev.install".format(version_major))
  58. os.rename(install_file_template, install_file)
  59. # Create rule file and replace template variables
  60. rule_file_template = os.path.join(debian_path, "rules-template")
  61. rule_file = os.path.join(debian_path, "rules")
  62. os.rename(rule_file_template, rule_file)
  63. with open(rule_file, 'r+') as f:
  64. content = f.read()
  65. f.seek(0)
  66. f.truncate()
  67. content = content.replace('<soname>', "{}".format(version_major))
  68. f.write(content)
  69. # Update CMakeLists.txt to include full version string
  70. with open(os.path.join(dirpath,"CMakeLists.txt"), 'r+') as f:
  71. lines = f.readlines()
  72. f.seek(0)
  73. f.truncate()
  74. for idx, line in enumerate(lines):
  75. if idx == 2:
  76. f.write('set(OPEN62541_VERSION "{}")\n'.format(git_describe_version))
  77. f.write(line)