PageRenderTime 34ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/version_hook.py

https://bitbucket.org/Mekk/perl-webservice_bitbucket
Python | 137 lines | 134 code | 1 blank | 2 comment | 5 complexity | 8b8d8aee4de80142c2f20b6945ba19fd MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. Updating version number on "hg tag".
  4. To use:
  5. [hooks]
  6. pre-tag=python:version_hook.py:version_update
  7. Note: pre-tag, not pretag! In the latter the changeset being tagged
  8. is already set.
  9. """
  10. VERSION = "0.1001"
  11. UPDATED_FILES = [
  12. "lib/WebService/BitBucket.pm",
  13. ]
  14. def _patch_perl_files(ui, new_version_no):
  15. """
  16. Patches all perl files to new version number, returns modified files.
  17. """
  18. import os, re
  19. version_regexps = [
  20. re.compile(r"^(?P<pfx>our +\$VERSION *= *').*?(?P<sfx>'.*)$"),
  21. re.compile(r"^(?P<pfx>Version +)\d+\.\d+(\.\d+)?(?P<sfx>\.? *)$"),
  22. ]
  23. location = os.path.dirname(__file__)
  24. updated_files = []
  25. for candidate in UPDATED_FILES:
  26. full_name = os.path.join(location, candidate)
  27. changes = 0
  28. file_lines = []
  29. ui.note("Checking file %s\n" % candidate)
  30. with open(full_name, "r") as input:
  31. for line in input.readlines():
  32. m = None
  33. for rgxp in version_regexps:
  34. m = rgxp.search(line)
  35. if m:
  36. ui.note("Patching line %s" % line)
  37. break
  38. if m:
  39. file_lines.append(
  40. m.group('pfx') + new_version_no + m.group('sfx') + "\n")
  41. changes += 1
  42. else:
  43. file_lines.append(line)
  44. if changes:
  45. ui.status("Version updater: Replacing old version number with {0:>s} in {1}\n".format(new_version_no, full_name))
  46. with open(full_name, "w") as output:
  47. output.writelines(file_lines)
  48. updated_files.append(full_name)
  49. return updated_files
  50. def version_update(repo, ui, hooktype, pats, opts, **kwargs):
  51. """
  52. Method used in mercurial version-update hook. Don't call directly.
  53. """
  54. import re
  55. import mercurial.commands
  56. # Regexps for handled version number syntaxes
  57. tag_regexps = [
  58. # something_1-2, something-1.2 and similar
  59. re.compile(r"[^-_0-9][-_](?P<major>[0-9]+)[-_\.](?P<minor>[0-9]+)$"),
  60. # 1.2, 1-2, 1_2
  61. re.compile(r"^(?P<major>[0-9]+)[-_\.](?P<minor>[0-9]+)$"),
  62. ]
  63. if opts.get('local'):
  64. ui.note("Version updater: ignoring local tag\n")
  65. return
  66. if opts.get('remove'):
  67. ui.note("Version updater: ignoring tag removal\n")
  68. return
  69. if opts.get('rev'):
  70. ui.note("Version updater: ignoring tag placed by rev\n")
  71. return
  72. if len(pats) != 1:
  73. ui.warn("Version updater: unexpected arguments, pats=%s\n" % pats)
  74. return True # means fail
  75. tag_name = pats[0]
  76. version_no = None
  77. for tag_regexp in tag_regexps:
  78. m = tag_regexp.search(tag_name)
  79. if m:
  80. version_no = "{major:>s}.{minor:>s}".format(**m.groupdict())
  81. break
  82. if not version_no:
  83. ui.warn("Version updater: Given tag does not seem to be versioned. Please make proper tags (1.2, xxxx_1-2, xaear-aera-1.2 or similar\n")
  84. return True # means fail
  85. if version_no == VERSION:
  86. ui.note("Version updater: version number {0:>s} is already correct\n".format(version_no))
  87. return False # means OK
  88. # Regexp for VERSION= line
  89. py_version_regexp = re.compile(r"^VERSION *= *")
  90. my_name = __file__
  91. if my_name.endswith(".pyc") or my_name.endswith(".pyo"):
  92. my_name = my_name[:-1]
  93. ui.status("Version updater: Replacing old version number {0:>s} with {1:>s} in {2}\n".format(
  94. VERSION, version_no, my_name))
  95. file_lines = []
  96. changes = 0
  97. with open(my_name, "r") as input:
  98. for line in input.readlines():
  99. if py_version_regexp.search(line):
  100. file_lines.append('VERSION = "%s"\n' % version_no)
  101. changes += 1
  102. else:
  103. file_lines.append(line)
  104. if not changes:
  105. ui.warn("Version updater: Line starting with VERSION= not found in {0:>s}.\nPlease correct this file and retry\n".format(my_name))
  106. return True #means fail
  107. with open(my_name, "w") as output:
  108. output.writelines(file_lines)
  109. updated_perl_files = _patch_perl_files(ui, version_no)
  110. ui.note("Commiting updated version number\n")
  111. mercurial.commands.commit(
  112. ui, repo,
  113. my_name,
  114. *updated_perl_files,
  115. message="Version number set to %s" % version_no)
  116. return False #means ok