updateversion.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/python -u
  2. '''
  3. ADOdb version update script
  4. Updates the version number, and release date in all php, txt and htm files
  5. '''
  6. from datetime import date
  7. import getopt
  8. import os
  9. from os import path
  10. import re
  11. import subprocess
  12. import sys
  13. # ADOdb version validation regex
  14. _version_dev = "dev"
  15. _version_regex = "[Vv]?[0-9]\.[0-9]+(%s|[a-z]|\.[0-9])?" % _version_dev
  16. _release_date_regex = "[0-9?]+-.*-[0-9]+"
  17. _changelog_file = "docs/changelog.md"
  18. _tag_prefix = "v"
  19. # Command-line options
  20. options = "hct"
  21. long_options = ["help", "commit", "tag"]
  22. def usage():
  23. print '''Usage: %s version
  24. Parameters:
  25. version ADOdb version, format: [v]X.YY[a-z|dev]
  26. Options:
  27. -c | --commit Automatically commit the changes
  28. -t | --tag Create a tag for the new release
  29. -h | --help Show this usage message
  30. ''' % (
  31. path.basename(__file__)
  32. )
  33. #end usage()
  34. def version_check(version):
  35. ''' Checks that the given version is valid, exits with error if not.
  36. Returns the version without the "v" prefix
  37. '''
  38. if not re.search("^%s$" % _version_regex, version):
  39. usage()
  40. print "ERROR: invalid version ! \n"
  41. sys.exit(1)
  42. return version.lstrip("Vv")
  43. def release_date(version):
  44. ''' Returns the release date in DD-MMM-YYYY format
  45. For development releases, DD-MMM will be ??-???
  46. '''
  47. # Development release
  48. if version.endswith(_version_dev):
  49. date_format = "??-???-%Y"
  50. else:
  51. date_format = "%d-%b-%Y"
  52. # Define release date
  53. return date.today().strftime(date_format)
  54. def sed_script(version):
  55. ''' Builds sed script to update version information in source files
  56. '''
  57. # Version number and release date
  58. script = "s/%s\s+%s/v%s %s/\n" % (
  59. _version_regex,
  60. _release_date_regex,
  61. version,
  62. release_date(version)
  63. )
  64. return script
  65. def sed_filelist():
  66. ''' Build list of files to update
  67. '''
  68. def sed_filter(name):
  69. return name.lower().endswith((".php", ".htm", ".txt"))
  70. dirlist = []
  71. for root, dirs, files in os.walk(".", topdown=True):
  72. for name in filter(sed_filter, files):
  73. dirlist.append(path.join(root, name))
  74. return dirlist
  75. def tag_name(version):
  76. return _tag_prefix + version
  77. def tag_check(version):
  78. ''' Checks if the tag for the specified version exists in the repository
  79. by attempting to check it out
  80. Throws exception if not
  81. '''
  82. subprocess.check_call(
  83. "git checkout --quiet " + tag_name(version),
  84. stderr=subprocess.PIPE,
  85. shell=True)
  86. print "Tag '%s' already exists" % tag_name(version)
  87. def tag_delete(version):
  88. ''' Deletes the specified tag
  89. '''
  90. subprocess.check_call(
  91. "git tag --delete " + tag_name(version),
  92. stderr=subprocess.PIPE,
  93. shell=True)
  94. def tag_create(version):
  95. ''' Creates the tag for the specified version
  96. Returns True if tag created
  97. '''
  98. print "Creating release tag '%s'" % tag_name(version)
  99. result = subprocess.call(
  100. "git tag --sign --message '%s' %s" % (
  101. "ADOdb version %s released %s" % (
  102. version,
  103. release_date(version)
  104. ),
  105. tag_name(version)
  106. ),
  107. shell=True
  108. )
  109. return result == 0
  110. def update_changelog(version):
  111. ''' Updates the release date in the Change Log
  112. '''
  113. print "Updating Changelog"
  114. # Development release
  115. # Insert a new section for next release before the most recent one
  116. if version.endswith(_version_dev):
  117. version_release = version[:-len(_version_dev)]
  118. version_previous = version_release.split(".")
  119. version_previous[1] = str(int(version_previous[1]) - 1)
  120. version_previous = ".".join(version_previous)
  121. print " Inserting new section for v%s" % version_release
  122. script = "1,/^##/s/^##.*$/## %s - %s\\n\\n\\0/" % (
  123. version_release,
  124. release_date(version)
  125. )
  126. # Stable release (X.Y.0 or X.Y)
  127. # Replace the occurence of markdown level 2 header matching version
  128. # and release date patterns
  129. elif version.endswith(".0") or re.match('[Vv]?[0-9]\.[0-9]+$', version):
  130. print " Updating release date for v%s" % version
  131. script = "1,/^##/s/^(## )%s - %s.*$/\\1%s - %s/" % (
  132. _version_regex,
  133. _release_date_regex,
  134. version,
  135. release_date(version)
  136. )
  137. # Hotfix release (X.Y.[0-9] or X.Y[a-z])
  138. # Insert a new section for the hotfix release before the most recent
  139. # section for version X.Y and display a warning message
  140. else:
  141. version_parent = re.match('[Vv]?([0-9]\.[0-9]+)', version).group(1)
  142. print " Inserting new section for hotfix release v%s" % version
  143. print " WARNING: review '%s' to ensure added section is correct" % (
  144. _changelog_file
  145. )
  146. script = "1,/^## {0}/s/^## {0}.*$/## {1} - {2}\\n\\n\\0/".format(
  147. version_parent,
  148. version,
  149. release_date(version)
  150. )
  151. subprocess.call(
  152. "sed -r -i '%s' %s " % (
  153. script,
  154. _changelog_file
  155. ),
  156. shell=True
  157. )
  158. #end update_changelog
  159. def version_set(version, do_commit=True, do_tag=True):
  160. ''' Bump version number and set release date in source files
  161. '''
  162. print "Preparing version bump commit"
  163. update_changelog(version)
  164. print "Updating version and date in source files"
  165. subprocess.call(
  166. "sed -r -i '%s' %s " % (
  167. sed_script(version),
  168. " ".join(sed_filelist())
  169. ),
  170. shell=True
  171. )
  172. print "Version set to %s" % version
  173. if do_commit:
  174. # Commit changes
  175. print "Committing"
  176. commit_ok = subprocess.call(
  177. "git commit --all --message '%s'" % (
  178. "Bump version to %s" % version
  179. ),
  180. shell=True
  181. )
  182. if do_tag:
  183. tag_ok = tag_create(version)
  184. else:
  185. tag_ok = False
  186. if commit_ok == 0:
  187. print '''
  188. NOTE: you should carefully review the new commit, making sure updates
  189. to the files are correct and no additional changes are required.
  190. If everything is fine, then the commit can be pushed upstream;
  191. otherwise:
  192. - Make the required corrections
  193. - Amend the commit ('git commit --all --amend' ) or create a new one'''
  194. if tag_ok:
  195. print ''' - Drop the tag ('git tag --delete %s')
  196. - run this script again
  197. ''' % (
  198. tag_name(version)
  199. )
  200. else:
  201. print "Note: changes have been staged but not committed."
  202. #end version_set()
  203. def main():
  204. # Get command-line options
  205. try:
  206. opts, args = getopt.gnu_getopt(sys.argv[1:], options, long_options)
  207. except getopt.GetoptError, err:
  208. print str(err)
  209. usage()
  210. sys.exit(2)
  211. if len(args) < 1:
  212. usage()
  213. print "ERROR: please specify the version"
  214. sys.exit(1)
  215. do_commit = False
  216. do_tag = False
  217. for opt, val in opts:
  218. if opt in ("-h", "--help"):
  219. usage()
  220. sys.exit(0)
  221. elif opt in ("-c", "--commit"):
  222. do_commit = True
  223. elif opt in ("-t", "--tag"):
  224. do_tag = True
  225. # Mandatory parameters
  226. version = version_check(args[0])
  227. # Let's do it
  228. version_set(version, do_commit, do_tag)
  229. #end main()
  230. if __name__ == "__main__":
  231. main()