updateversion.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #!/usr/bin/python -u
  2. '''
  3. ADOdb version update script
  4. Updates the version number, and release date in all php and html 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. # These are used by sed - they are not PCRE !
  15. _version_dev = "dev"
  16. _version_regex = "[Vv]?([0-9]\.[0-9]+)(\.([0-9]+))?(-?%s)?" % _version_dev
  17. _release_date_regex = "[0-9?]+-.*-[0-9]+"
  18. _changelog_file = "docs/changelog.md"
  19. _tag_prefix = "v"
  20. # Command-line options
  21. options = "hct"
  22. long_options = ["help", "commit", "tag"]
  23. def usage():
  24. print '''Usage: %s version
  25. Parameters:
  26. version ADOdb version, format: [v]X.YY[a-z|dev]
  27. Options:
  28. -c | --commit Automatically commit the changes
  29. -t | --tag Create a tag for the new release
  30. -h | --help Show this usage message
  31. ''' % (
  32. path.basename(__file__)
  33. )
  34. #end usage()
  35. def version_is_dev(version):
  36. ''' Returns true if version is a development release
  37. '''
  38. return version.endswith(_version_dev)
  39. def version_is_patch(version):
  40. ''' Returns true if version is a patch release (i.e. X.Y.Z with Z > 0)
  41. '''
  42. return not version.endswith('.0')
  43. def version_parse(version):
  44. ''' Breakdown the version into groups (Z and -dev are optional)
  45. 1:(X.Y), 2:(.Z), 3:(Z), 4:(-dev)
  46. '''
  47. return re.match(r'^%s$' % _version_regex, version)
  48. def version_check(version):
  49. ''' Checks that the given version is valid, exits with error if not.
  50. Returns the SemVer-normalized version without the "v" prefix
  51. - add '.0' if missing patch bit
  52. - add '-' before dev release suffix if needed
  53. '''
  54. vparse = version_parse(version)
  55. if not vparse:
  56. usage()
  57. print "ERROR: invalid version ! \n"
  58. sys.exit(1)
  59. vnorm = vparse.group(1)
  60. # Add .patch version component
  61. if vparse.group(2):
  62. vnorm += vparse.group(2)
  63. else:
  64. # None was specified, assume a .0 release
  65. vnorm += '.0'
  66. # Normalize version number
  67. if version_is_dev(version):
  68. vnorm += '-' + _version_dev
  69. return vnorm
  70. def get_release_date(version):
  71. ''' Returns the release date in DD-MMM-YYYY format
  72. For development releases, DD-MMM will be ??-???
  73. '''
  74. # Development release
  75. if version_is_dev(version):
  76. date_format = "??-???-%Y"
  77. else:
  78. date_format = "%d-%b-%Y"
  79. # Define release date
  80. return date.today().strftime(date_format)
  81. def sed_script(version):
  82. ''' Builds sed script to update version information in source files
  83. '''
  84. # Version number and release date
  85. script = r"s/{}\s+(-?)\s+{}/v{} \5 {}/".format(
  86. _version_regex,
  87. _release_date_regex,
  88. version,
  89. get_release_date(version)
  90. )
  91. return script
  92. def sed_filelist():
  93. ''' Build list of files to update
  94. '''
  95. dirlist = []
  96. for root, dirs, files in os.walk(".", topdown=True):
  97. # Filter files by extensions
  98. files = [
  99. f for f in files
  100. if re.search(r'\.(php|html?)$', f, re.IGNORECASE)
  101. ]
  102. for fname in files:
  103. dirlist.append(path.join(root, fname))
  104. return dirlist
  105. def tag_name(version):
  106. return _tag_prefix + version
  107. def tag_check(version):
  108. ''' Checks if the tag for the specified version exists in the repository
  109. by attempting to check it out
  110. Throws exception if not
  111. '''
  112. subprocess.check_call(
  113. "git checkout --quiet " + tag_name(version),
  114. stderr=subprocess.PIPE,
  115. shell=True)
  116. print "Tag '%s' already exists" % tag_name(version)
  117. def tag_delete(version):
  118. ''' Deletes the specified tag
  119. '''
  120. subprocess.check_call(
  121. "git tag --delete " + tag_name(version),
  122. stderr=subprocess.PIPE,
  123. shell=True)
  124. def tag_create(version):
  125. ''' Creates the tag for the specified version
  126. Returns True if tag created
  127. '''
  128. print "Creating release tag '%s'" % tag_name(version)
  129. result = subprocess.call(
  130. "git tag --sign --message '%s' %s" % (
  131. "ADOdb version %s released %s" % (
  132. version,
  133. get_release_date(version)
  134. ),
  135. tag_name(version)
  136. ),
  137. shell=True
  138. )
  139. return result == 0
  140. def section_exists(filename, version, print_message=True):
  141. ''' Checks given file for existing section with specified version
  142. '''
  143. script = True
  144. for i, line in enumerate(open(filename)):
  145. if re.search(r'^## ' + version, line):
  146. if print_message:
  147. print " Existing section for v%s found," % version,
  148. return True
  149. return False
  150. def version_get_previous(version):
  151. ''' Returns the previous version number
  152. Don't decrease major versions (raises exception)
  153. '''
  154. vprev = version.split('.')
  155. item = len(vprev) - 1
  156. while item > 0:
  157. val = int(vprev[item])
  158. if val > 0:
  159. vprev[item] = str(val - 1)
  160. break
  161. else:
  162. item -= 1
  163. if item == 0:
  164. raise ValueError('Refusing to decrease major version number')
  165. return '.'.join(vprev)
  166. def update_changelog(version):
  167. ''' Updates the release date in the Change Log
  168. '''
  169. print "Updating Changelog"
  170. vparse = version_parse(version)
  171. # Version number without '-dev' suffix
  172. version_release = vparse.group(1) + vparse.group(2)
  173. version_previous = version_get_previous(version_release)
  174. if not section_exists(_changelog_file, version_previous, False):
  175. raise ValueError(
  176. "ERROR: previous version %s does not exist in changelog" %
  177. version_previous
  178. )
  179. # Check if version already exists in changelog
  180. version_exists = section_exists(_changelog_file, version_release)
  181. if (not version_exists
  182. and not version_is_patch(version)
  183. and not version_is_dev(version)):
  184. version += '-' + _version_dev
  185. release_date = get_release_date(version)
  186. # Development release
  187. # Insert a new section for next release before the most recent one
  188. if version_is_dev(version):
  189. # Check changelog file for existing section
  190. if version_exists:
  191. print "nothing to do"
  192. return
  193. # No existing section found, insert new one
  194. if version_is_patch(version_release):
  195. print " Inserting new section for hotfix release v%s" % version
  196. else:
  197. print " Inserting new section for v%s" % version_release
  198. # Adjust previous version number (remove patch component)
  199. version_previous = version_parse(version_previous).group(1)
  200. script = "1,/^## {0}/s/^## {0}.*$/## {1} - {2}\\n\\n\\0/".format(
  201. version_previous,
  202. version_release,
  203. release_date
  204. )
  205. # Stable release (X.Y.0)
  206. # Replace the 1st occurence of markdown level 2 header matching version
  207. # and release date patterns
  208. elif not version_is_patch(version):
  209. print " Updating release date for v%s" % version
  210. script = r"s/^(## ){0}(\.0)? - {1}.*$/\1{2} - {3}/".format(
  211. vparse.group(1),
  212. _release_date_regex,
  213. version,
  214. release_date
  215. )
  216. # Hotfix release (X.Y.[0-9])
  217. # Insert a new section for the hotfix release before the most recent
  218. # section for version X.Y and display a warning message
  219. else:
  220. if version_exists:
  221. print 'updating release date'
  222. script = "s/^## {0}.*$/## {1} - {2}/".format(
  223. version.replace('.', '\.'),
  224. version,
  225. release_date
  226. )
  227. else:
  228. print " Inserting new section for hotfix release v%s" % version
  229. script = "1,/^## {0}/s/^## {0}.*$/## {1} - {2}\\n\\n\\0/".format(
  230. version_previous,
  231. version,
  232. release_date
  233. )
  234. print " WARNING: review '%s' to ensure added section is correct" % (
  235. _changelog_file
  236. )
  237. subprocess.call(
  238. "sed -r -i '%s' %s " % (
  239. script,
  240. _changelog_file
  241. ),
  242. shell=True
  243. )
  244. #end update_changelog
  245. def version_set(version, do_commit=True, do_tag=True):
  246. ''' Bump version number and set release date in source files
  247. '''
  248. print "Preparing version bump commit"
  249. update_changelog(version)
  250. print "Updating version and date in source files"
  251. subprocess.call(
  252. "sed -r -i '%s' %s " % (
  253. sed_script(version),
  254. " ".join(sed_filelist())
  255. ),
  256. shell=True
  257. )
  258. print "Version set to %s" % version
  259. if do_commit:
  260. # Commit changes
  261. print "Committing"
  262. commit_ok = subprocess.call(
  263. "git commit --all --message '%s'" % (
  264. "Bump version to %s" % version
  265. ),
  266. shell=True
  267. )
  268. if do_tag:
  269. tag_ok = tag_create(version)
  270. else:
  271. tag_ok = False
  272. if commit_ok == 0:
  273. print '''
  274. NOTE: you should carefully review the new commit, making sure updates
  275. to the files are correct and no additional changes are required.
  276. If everything is fine, then the commit can be pushed upstream;
  277. otherwise:
  278. - Make the required corrections
  279. - Amend the commit ('git commit --all --amend' ) or create a new one'''
  280. if tag_ok:
  281. print ''' - Drop the tag ('git tag --delete %s')
  282. - run this script again
  283. ''' % (
  284. tag_name(version)
  285. )
  286. else:
  287. print "Note: changes have been staged but not committed."
  288. #end version_set()
  289. def main():
  290. # Get command-line options
  291. try:
  292. opts, args = getopt.gnu_getopt(sys.argv[1:], options, long_options)
  293. except getopt.GetoptError, err:
  294. print str(err)
  295. usage()
  296. sys.exit(2)
  297. if len(args) < 1:
  298. usage()
  299. print "ERROR: please specify the version"
  300. sys.exit(1)
  301. do_commit = False
  302. do_tag = False
  303. for opt, val in opts:
  304. if opt in ("-h", "--help"):
  305. usage()
  306. sys.exit(0)
  307. elif opt in ("-c", "--commit"):
  308. do_commit = True
  309. elif opt in ("-t", "--tag"):
  310. do_tag = True
  311. # Mandatory parameters
  312. version = version_check(args[0])
  313. # Let's do it
  314. os.chdir(subprocess.check_output('git root', shell=True).rstrip())
  315. version_set(version, do_commit, do_tag)
  316. #end main()
  317. if __name__ == "__main__":
  318. main()