prepare_release_update_documentation.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
  2. # or more contributor license agreements. Licensed under the Elastic License
  3. # 2.0 and the Server Side Public License, v 1; you may not use this file except
  4. # in compliance with, at your election, the Elastic License 2.0 or the Server
  5. # Side Public License, v 1.
  6. # Prepare a release: Update the documentation and commit
  7. #
  8. # USAGE:
  9. #
  10. # python3 ./dev-tools/prepare_release_update_documentation.py
  11. #
  12. # Note: Ensure the script is run from the root directory
  13. # This script needs to be run and then pushed,
  14. # before proceeding with prepare_release_create-release-version.py
  15. # on your build VM
  16. #
  17. import fnmatch
  18. import subprocess
  19. import tempfile
  20. import re
  21. import os
  22. import shutil
  23. def run(command):
  24. if os.system('%s' % (command)):
  25. raise RuntimeError(' FAILED: %s' % (command))
  26. def ensure_checkout_is_clean():
  27. # Make sure no local mods:
  28. s = subprocess.check_output('git diff --shortstat', shell=True)
  29. if len(s) > 0:
  30. raise RuntimeError('git diff --shortstat is non-empty: got:\n%s' % s)
  31. # Make sure no untracked files:
  32. s = subprocess.check_output('git status', shell=True).decode('utf-8', errors='replace')
  33. if 'Untracked files:' in s:
  34. raise RuntimeError('git status shows untracked files: got:\n%s' % s)
  35. # Make sure we have all changes from origin:
  36. if 'is behind' in s:
  37. raise RuntimeError('git status shows not all changes pulled from origin; try running "git pull origin" in this branch: got:\n%s' % (s))
  38. # Make sure we no local unpushed changes (this is supposed to be a clean area):
  39. if 'is ahead' in s:
  40. raise RuntimeError('git status shows local commits; try running "git fetch origin", "git checkout ", "git reset --hard origin/" in this branch: got:\n%s' % (s))
  41. # Reads the given file and applies the
  42. # callback to it. If the callback changed
  43. # a line the given file is replaced with
  44. # the modified input.
  45. def process_file(file_path, line_callback):
  46. fh, abs_path = tempfile.mkstemp()
  47. modified = False
  48. with open(abs_path,'w', encoding='utf-8') as new_file:
  49. with open(file_path, encoding='utf-8') as old_file:
  50. for line in old_file:
  51. new_line = line_callback(line)
  52. modified = modified or (new_line != line)
  53. new_file.write(new_line)
  54. os.close(fh)
  55. if modified:
  56. #Remove original file
  57. os.remove(file_path)
  58. #Move new file
  59. shutil.move(abs_path, file_path)
  60. return True
  61. else:
  62. # nothing to do - just remove the tmp file
  63. os.remove(abs_path)
  64. return False
  65. # Checks the pom.xml for the release version.
  66. # This method fails if the pom file has no SNAPSHOT version set ie.
  67. # if the version is already on a release version we fail.
  68. # Returns the next version string ie. 0.90.7
  69. def find_release_version():
  70. with open('pom.xml', encoding='utf-8') as file:
  71. for line in file:
  72. match = re.search(r'<version>(.+)-SNAPSHOT</version>', line)
  73. if match:
  74. return match.group(1)
  75. raise RuntimeError('Could not find release version in branch')
  76. # Stages the given files for the next git commit
  77. def add_pending_files(*files):
  78. for file in files:
  79. if file:
  80. # print("Adding file: %s" % (file))
  81. run('git add %s' % (file))
  82. # Updates documentation feature flags
  83. def commit_feature_flags(release):
  84. run('git commit -m "Update Documentation Feature Flags [%s]"' % release)
  85. # Walks the given directory path (defaults to 'docs')
  86. # and replaces all 'coming[$version]' tags with
  87. # 'added[$version]'. This method only accesses asciidoc files.
  88. def update_reference_docs(release_version, path='docs'):
  89. pattern = 'coming[%s' % (release_version)
  90. replacement = 'added[%s' % (release_version)
  91. pending_files = []
  92. def callback(line):
  93. return line.replace(pattern, replacement)
  94. for root, _, file_names in os.walk(path):
  95. for file_name in fnmatch.filter(file_names, '*.asciidoc'):
  96. full_path = os.path.join(root, file_name)
  97. if process_file(full_path, callback):
  98. pending_files.append(os.path.join(root, file_name))
  99. return pending_files
  100. if __name__ == "__main__":
  101. release_version = find_release_version()
  102. print('*** Preparing release version documentation: [%s]' % release_version)
  103. ensure_checkout_is_clean()
  104. pending_files = update_reference_docs(release_version)
  105. if pending_files:
  106. add_pending_files(*pending_files) # expects var args use * to expand
  107. commit_feature_flags(release_version)
  108. else:
  109. print('WARNING: no documentation references updates for release %s' % (release_version))
  110. print('*** Done.')