fmt/support/manage.py

219 lines
6.5 KiB
Python
Raw Normal View History

2021-06-27 15:55:16 +00:00
#!/usr/bin/env python3
"""Manage site and releases.
Usage:
2019-08-24 14:24:42 +00:00
manage.py release [<branch>]
manage.py site
2019-08-24 14:24:42 +00:00
For the release command $FMT_TOKEN should contain a GitHub personal access token
obtained from https://github.com/settings/tokens.
"""
from __future__ import print_function
2017-07-01 14:30:51 +00:00
import datetime, docopt, errno, fileinput, json, os
2023-12-30 15:11:32 +00:00
import re, requests, shutil, sys
2016-06-17 14:30:42 +00:00
from contextlib import contextmanager
from subprocess import check_call
class Git:
def __init__(self, dir):
self.dir = dir
def call(self, method, args, **kwargs):
return check_call(['git', method] + list(args), **kwargs)
2016-06-22 14:58:35 +00:00
def add(self, *args):
return self.call('add', args, cwd=self.dir)
def checkout(self, *args):
return self.call('checkout', args, cwd=self.dir)
def clean(self, *args):
return self.call('clean', args, cwd=self.dir)
2016-06-22 14:58:35 +00:00
def clone(self, *args):
return self.call('clone', list(args) + [self.dir])
def commit(self, *args):
return self.call('commit', args, cwd=self.dir)
def pull(self, *args):
return self.call('pull', args, cwd=self.dir)
2016-06-22 14:58:35 +00:00
def push(self, *args):
return self.call('push', args, cwd=self.dir)
def reset(self, *args):
return self.call('reset', args, cwd=self.dir)
def update(self, *args):
2016-06-22 14:29:34 +00:00
clone = not os.path.exists(self.dir)
if clone:
self.clone(*args)
2016-06-22 14:29:34 +00:00
return clone
def clean_checkout(repo, branch):
repo.clean('-f', '-d')
repo.reset('--hard')
repo.checkout(branch)
class Runner:
2016-06-22 14:58:35 +00:00
def __init__(self, cwd):
self.cwd = cwd
def __call__(self, *args, **kwargs):
kwargs['cwd'] = kwargs.get('cwd', self.cwd)
check_call(args, **kwargs)
def create_build_env():
"""Create a build environment."""
class Env:
pass
env = Env()
env.fmt_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
env.build_dir = 'build'
env.fmt_repo = Git(os.path.join(env.build_dir, 'fmt'))
return env
fmt_repo_url = 'git@github.com:fmtlib/fmt'
def update_site(env):
env.fmt_repo.update(fmt_repo_url)
2024-06-29 15:54:03 +00:00
doc_repo = Git(os.path.join(env.build_dir, 'fmt.dev'))
doc_repo.update('git@github.com:fmtlib/fmt.dev')
2024-06-29 15:05:52 +00:00
version = '11.0.0'
clean_checkout(env.fmt_repo, version)
target_doc_dir = os.path.join(env.fmt_repo.dir, 'doc')
# Build the docs.
html_dir = os.path.join(env.build_dir, 'html')
if os.path.exists(html_dir):
shutil.rmtree(html_dir)
include_dir = env.fmt_repo.dir
import build
build.build_docs(version, doc_dir=target_doc_dir,
include_dir=include_dir, work_dir=env.build_dir)
shutil.rmtree(os.path.join(html_dir, '.doctrees'))
# Copy docs to the website.
version_doc_dir = os.path.join(doc_repo.dir, version)
try:
shutil.rmtree(version_doc_dir)
except OSError as e:
if e.errno != errno.ENOENT:
raise
shutil.move(html_dir, version_doc_dir)
def release(args):
env = create_build_env()
2016-06-22 14:58:35 +00:00
fmt_repo = env.fmt_repo
branch = args.get('<branch>')
if branch is None:
branch = 'master'
2016-06-22 14:58:35 +00:00
if not fmt_repo.update('-b', branch, fmt_repo_url):
clean_checkout(fmt_repo, branch)
2023-12-30 22:29:31 +00:00
# Update the date in the changelog and extract the version and the first
# section content.
2023-09-04 16:38:06 +00:00
changelog = 'ChangeLog.md'
2016-06-22 14:58:35 +00:00
changelog_path = os.path.join(fmt_repo.dir, changelog)
2023-12-30 22:29:31 +00:00
is_first_section = True
first_section = []
for i, line in enumerate(fileinput.input(changelog_path, inplace=True)):
if i == 0:
version = re.match(r'# (.*) - TBD', line).group(1)
2024-01-01 19:06:44 +00:00
line = '# {} - {}\n'.format(
version, datetime.date.today().isoformat())
2023-12-30 22:29:31 +00:00
elif not is_first_section:
pass
2023-12-30 17:57:31 +00:00
elif line.startswith('#'):
2023-12-30 22:29:31 +00:00
is_first_section = False
else:
first_section.append(line)
2023-12-30 14:53:20 +00:00
sys.stdout.write(line)
2023-12-30 22:29:31 +00:00
if first_section[0] == '\n':
first_section.pop(0)
ns_version = None
2024-07-05 15:25:10 +00:00
base_h_path = os.path.join(fmt_repo.dir, 'include', 'fmt', 'base.h')
for line in fileinput.input(base_h_path):
m = re.match(r'\s*inline namespace v(.*) .*', line)
if m:
ns_version = m.group(1)
break
major_version = version.split('.')[0]
if not ns_version or ns_version != major_version:
raise Exception(f'Version mismatch {ns_version} != {major_version}')
2024-06-30 14:55:57 +00:00
# Workaround GitHub-flavored Markdown treating newlines as <br>.
2024-06-29 16:36:35 +00:00
changes = ''
2024-06-29 16:23:02 +00:00
code_block = False
stripped = False
for line in first_section:
if re.match(r'^\s*```', line):
code_block = not code_block
changes += line
stripped = False
continue
if code_block:
changes += line
continue
2024-06-29 16:31:32 +00:00
if line == '\n' or re.match(r'^\s*\|.*', line):
2024-06-29 16:23:02 +00:00
if stripped:
2024-06-29 16:36:35 +00:00
changes += '\n'
2024-06-29 16:23:02 +00:00
stripped = False
2024-06-29 16:36:35 +00:00
changes += line
2024-06-29 16:23:02 +00:00
continue
if stripped:
line = ' ' + line.lstrip()
changes += line.rstrip()
stripped = True
2023-12-30 14:53:20 +00:00
2016-06-22 14:58:35 +00:00
fmt_repo.checkout('-B', 'release')
2024-06-29 15:59:54 +00:00
fmt_repo.add(changelog)
2016-06-22 14:58:35 +00:00
fmt_repo.commit('-m', 'Update version')
# Build the docs and package.
2016-06-22 14:58:35 +00:00
run = Runner(fmt_repo.dir)
run('cmake', '.')
run('make', 'doc', 'package_source')
# Create a release on GitHub.
2016-06-22 14:58:35 +00:00
fmt_repo.push('origin', 'release')
2022-01-02 17:29:42 +00:00
auth_headers = {'Authorization': 'token ' + os.getenv('FMT_TOKEN')}
r = requests.post('https://api.github.com/repos/fmtlib/fmt/releases',
2022-01-02 17:29:42 +00:00
headers=auth_headers,
data=json.dumps({'tag_name': version,
'target_commitish': 'release',
'body': changes, 'draft': True}))
if r.status_code != 201:
raise Exception('Failed to create a release ' + str(r))
2017-12-20 16:30:58 +00:00
id = r.json()['id']
uploads_url = 'https://uploads.github.com/repos/fmtlib/fmt/releases'
package = 'fmt-{}.zip'.format(version)
2018-06-05 14:58:02 +00:00
r = requests.post(
'{}/{}/assets?name={}'.format(uploads_url, id, package),
2022-01-02 17:29:42 +00:00
headers={'Content-Type': 'application/zip'} | auth_headers,
data=open('build/fmt/' + package, 'rb'))
2018-06-05 14:58:02 +00:00
if r.status_code != 201:
raise Exception('Failed to upload an asset ' + str(r))
2024-01-01 19:26:19 +00:00
update_site(env)
if __name__ == '__main__':
args = docopt.docopt(__doc__)
if args.get('release'):
release(args)
elif args.get('site'):
update_site(create_build_env())