prepare_packaging.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import datetime
  10. dirpath = os.path.join(os.path.dirname(os.path.realpath(__file__)),"..")
  11. git_describe_version = subprocess.check_output(["git", "describe", "--tags", "--dirty", "--match", "v*"]).decode('utf-8').strip()
  12. # v1.2
  13. # v1.2.3
  14. # v1.2.3-rc1
  15. # v1.2.3-rc1-dirty
  16. # v1.2.3-5-g4538abcd
  17. # v1.2.3-5-g4538abcd-dirty
  18. # git_describe_version = "v1.2.3"
  19. m = re.match(r"^v([0-9]+)(\.[0-9]+)?(\.[0-9]+)?(-(.*)+)?$", git_describe_version)
  20. version_major = m.group(1) if m.group(1) is not None else "0"
  21. version_minor = m.group(2).replace(".", "") if m.group(2) is not None else "0"
  22. version_patch = m.group(3).replace(".", "") if m.group(3) is not None else "0"
  23. version_label = m.group(4) if m.group(4) is not None else ""
  24. #print("major {} minor {} patch {} label {}".format(version_major, version_minor, version_patch, version_label))
  25. # We can not use unstable for now, because dpkg-buildpackage wants to sign them
  26. # It will fail with gpg: skipped "open62541 Team <open62541-core@googlegroups.com>": No secret key
  27. #debian_distribution = "unstable"
  28. #if version_label is not "":
  29. debian_distribution = "UNRELEASED"
  30. debian_path = os.path.join(dirpath, "debian")
  31. changelog_file = os.path.join(debian_path, "changelog")
  32. # remove leading 'v'
  33. changelog_version = git_describe_version[1:] if git_describe_version[0] == 'v' else git_describe_version
  34. # replace all '-' with '~' in version
  35. changelog_version = changelog_version.replace('-', '~')
  36. # prefix the version string with the current ISO datetime to ensure correct version ordering.
  37. # See https://github.com/open62541/open62541/issues/3140
  38. changelog_version = datetime.datetime.utcnow().replace(microsecond=0).isoformat().replace('-', '').replace(':', '') + '~' + changelog_version
  39. with open(changelog_file, 'r') as original: data = original.read()
  40. with open(changelog_file, 'w') as modified:
  41. new_entry = """open62541 ({version}) {distribution}; urgency=medium
  42. * Full changelog is available here:
  43. https://github.com/open62541/open62541/blob/master/CHANGELOG
  44. -- open62541 Team <open62541-core@googlegroups.com> {time}
  45. """.format(version=changelog_version, time=formatdate(), distribution = debian_distribution)
  46. modified.write(new_entry + "\n" + data)
  47. # Create control file and replace template variables
  48. control_file_template = os.path.join(debian_path, "control-template")
  49. control_file = os.path.join(debian_path, "control")
  50. os.rename(control_file_template, control_file)
  51. with open(control_file, 'r+') as f:
  52. content = f.read()
  53. f.seek(0)
  54. f.truncate()
  55. f.write(content.replace('<soname>', "{}".format(version_major)))
  56. # rename the install template to match the soname
  57. install_file_template = os.path.join(debian_path, "libopen62541.install-template")
  58. install_file = os.path.join(debian_path, "libopen62541-{}.install".format(version_major))
  59. os.rename(install_file_template, install_file)
  60. install_file_template = os.path.join(debian_path, "libopen62541-dev.install-template")
  61. install_file = os.path.join(debian_path, "libopen62541-{}-dev.install".format(version_major))
  62. os.rename(install_file_template, install_file)
  63. install_file_template = os.path.join(debian_path, "libopen62541-tools.install-template")
  64. install_file = os.path.join(debian_path, "libopen62541-{}-tools.install".format(version_major))
  65. os.rename(install_file_template, install_file)
  66. install_file_template = os.path.join(debian_path, "open62541-doc.doc-base-template")
  67. install_file = os.path.join(debian_path, "open62541-doc.doc-base")
  68. os.rename(install_file_template, install_file)
  69. install_file_template = os.path.join(debian_path, "open62541-doc.install-template")
  70. install_file = os.path.join(debian_path, "open62541-doc.install")
  71. os.rename(install_file_template, install_file)
  72. # Create rule file and replace template variables
  73. rule_file_template = os.path.join(debian_path, "rules-template")
  74. rule_file = os.path.join(debian_path, "rules")
  75. os.rename(rule_file_template, rule_file)
  76. with open(rule_file, 'r+') as f:
  77. content = f.read()
  78. f.seek(0)
  79. f.truncate()
  80. content = content.replace('<soname>', "{}".format(version_major))
  81. f.write(content)
  82. # Update CMakeLists.txt to include full version string
  83. with open(os.path.join(dirpath,"CMakeLists.txt"), 'r+') as f:
  84. lines = f.readlines()
  85. f.seek(0)
  86. f.truncate()
  87. for idx, line in enumerate(lines):
  88. if idx == 2:
  89. f.write('set(OPEN62541_VERSION "{}")\n'.format(git_describe_version))
  90. f.write(line)