release.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python3
  2. """Make a release.
  3. Usage:
  4. release.py [<branch>]
  5. For the release command $FMT_TOKEN should contain a GitHub personal access token
  6. obtained from https://github.com/settings/tokens.
  7. """
  8. from __future__ import print_function
  9. import datetime, docopt, errno, fileinput, json, os
  10. import re, shutil, sys
  11. from subprocess import check_call
  12. import urllib.request
  13. class Git:
  14. def __init__(self, dir):
  15. self.dir = dir
  16. def call(self, method, args, **kwargs):
  17. return check_call(['git', method] + list(args), **kwargs)
  18. def add(self, *args):
  19. return self.call('add', args, cwd=self.dir)
  20. def checkout(self, *args):
  21. return self.call('checkout', args, cwd=self.dir)
  22. def clean(self, *args):
  23. return self.call('clean', args, cwd=self.dir)
  24. def clone(self, *args):
  25. return self.call('clone', list(args) + [self.dir])
  26. def commit(self, *args):
  27. return self.call('commit', args, cwd=self.dir)
  28. def pull(self, *args):
  29. return self.call('pull', args, cwd=self.dir)
  30. def push(self, *args):
  31. return self.call('push', args, cwd=self.dir)
  32. def reset(self, *args):
  33. return self.call('reset', args, cwd=self.dir)
  34. def update(self, *args):
  35. clone = not os.path.exists(self.dir)
  36. if clone:
  37. self.clone(*args)
  38. return clone
  39. def clean_checkout(repo, branch):
  40. repo.clean('-f', '-d')
  41. repo.reset('--hard')
  42. repo.checkout(branch)
  43. class Runner:
  44. def __init__(self, cwd):
  45. self.cwd = cwd
  46. def __call__(self, *args, **kwargs):
  47. kwargs['cwd'] = kwargs.get('cwd', self.cwd)
  48. check_call(args, **kwargs)
  49. def create_build_env():
  50. """Create a build environment."""
  51. class Env:
  52. pass
  53. env = Env()
  54. env.fmt_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  55. env.build_dir = 'build'
  56. env.fmt_repo = Git(os.path.join(env.build_dir, 'fmt'))
  57. return env
  58. if __name__ == '__main__':
  59. args = docopt.docopt(__doc__)
  60. env = create_build_env()
  61. fmt_repo = env.fmt_repo
  62. branch = args.get('<branch>')
  63. if branch is None:
  64. branch = 'master'
  65. if not fmt_repo.update('-b', branch, '[email protected]:fmtlib/fmt'):
  66. clean_checkout(fmt_repo, branch)
  67. # Update the date in the changelog and extract the version and the first
  68. # section content.
  69. changelog = 'ChangeLog.md'
  70. changelog_path = os.path.join(fmt_repo.dir, changelog)
  71. is_first_section = True
  72. first_section = []
  73. for i, line in enumerate(fileinput.input(changelog_path, inplace=True)):
  74. if i == 0:
  75. version = re.match(r'# (.*) - TBD', line).group(1)
  76. line = '# {} - {}\n'.format(
  77. version, datetime.date.today().isoformat())
  78. elif not is_first_section:
  79. pass
  80. elif line.startswith('#'):
  81. is_first_section = False
  82. else:
  83. first_section.append(line)
  84. sys.stdout.write(line)
  85. if first_section[0] == '\n':
  86. first_section.pop(0)
  87. ns_version = None
  88. base_h_path = os.path.join(fmt_repo.dir, 'include', 'fmt', 'base.h')
  89. for line in fileinput.input(base_h_path):
  90. m = re.match(r'\s*inline namespace v(.*) .*', line)
  91. if m:
  92. ns_version = m.group(1)
  93. break
  94. major_version = version.split('.')[0]
  95. if not ns_version or ns_version != major_version:
  96. raise Exception(f'Version mismatch {ns_version} != {major_version}')
  97. # Workaround GitHub-flavored Markdown treating newlines as <br>.
  98. changes = ''
  99. code_block = False
  100. stripped = False
  101. for line in first_section:
  102. if re.match(r'^\s*```', line):
  103. code_block = not code_block
  104. changes += line
  105. stripped = False
  106. continue
  107. if code_block:
  108. changes += line
  109. continue
  110. if line == '\n' or re.match(r'^\s*\|.*', line):
  111. if stripped:
  112. changes += '\n'
  113. stripped = False
  114. changes += line
  115. continue
  116. if stripped:
  117. line = ' ' + line.lstrip()
  118. changes += line.rstrip()
  119. stripped = True
  120. fmt_repo.checkout('-B', 'release')
  121. fmt_repo.add(changelog)
  122. fmt_repo.commit('-m', 'Update version')
  123. # Build the docs and package.
  124. run = Runner(fmt_repo.dir)
  125. run('cmake', '.')
  126. run('make', 'doc', 'package_source')
  127. # Create a release on GitHub.
  128. fmt_repo.push('origin', 'release')
  129. auth_headers = {'Authorization': 'token ' + os.getenv('FMT_TOKEN')}
  130. req = urllib.request.Request(
  131. 'https://api.github.com/repos/fmtlib/fmt/releases',
  132. data=json.dumps({'tag_name': version,
  133. 'target_commitish': 'release',
  134. 'body': changes, 'draft': True}).encode('utf-8'),
  135. headers=auth_headers, method='POST')
  136. with urllib.request.urlopen(req) as response:
  137. if response.status != 201:
  138. raise Exception(f'Failed to create a release ' +
  139. '{response.status} {response.reason}')
  140. response_data = json.loads(response.read().decode('utf-8'))
  141. id = response_data['id']
  142. # Upload the package.
  143. uploads_url = 'https://uploads.github.com/repos/fmtlib/fmt/releases'
  144. package = 'fmt-{}.zip'.format(version)
  145. req = urllib.request.Request(
  146. f'{uploads_url}/{id}/assets?name={package}',
  147. headers={'Content-Type': 'application/zip'} | auth_headers,
  148. data=open('build/fmt/' + package, 'rb').read(), method='POST')
  149. with urllib.request.urlopen(req) as response:
  150. if response.status != 201:
  151. raise Exception(f'Failed to upload an asset '
  152. '{response.status} {response.reason}')
  153. short_version = '.'.join(version.split('.')[:-1])
  154. check_call(['./mkdocs', 'deploy', short_version])