2021-06-27 18:55:16 +03:00
|
|
|
#!/usr/bin/env python3
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
"""Manage site and releases.
|
|
|
|
|
|
|
|
Usage:
|
2019-08-24 17:24:42 +03:00
|
|
|
manage.py release [<branch>]
|
2016-06-16 17:53:07 +03:00
|
|
|
manage.py site
|
2019-08-24 17:24:42 +03:00
|
|
|
|
|
|
|
For the release command $FMT_TOKEN should contain a GitHub personal access token
|
|
|
|
obtained from https://github.com/settings/tokens.
|
2016-06-16 17:53:07 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
2017-07-01 17:30:51 +03:00
|
|
|
import datetime, docopt, errno, fileinput, json, os
|
2016-06-16 17:53:07 +03:00
|
|
|
import re, requests, shutil, sys, tempfile
|
2016-06-17 17:30:42 +03:00
|
|
|
from contextlib import contextmanager
|
2016-06-16 17:53:07 +03:00
|
|
|
from distutils.version import LooseVersion
|
|
|
|
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 17:58:35 +03:00
|
|
|
def add(self, *args):
|
|
|
|
return self.call('add', args, cwd=self.dir)
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
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 17:58:35 +03: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)
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
def pull(self, *args):
|
|
|
|
return self.call('pull', args, cwd=self.dir)
|
|
|
|
|
2016-06-22 17:58:35 +03: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)
|
|
|
|
|
2016-06-16 17:53:07 +03:00
|
|
|
def update(self, *args):
|
2016-06-22 17:29:34 +03:00
|
|
|
clone = not os.path.exists(self.dir)
|
|
|
|
if clone:
|
2016-06-16 17:53:07 +03:00
|
|
|
self.clone(*args)
|
2016-06-22 17:29:34 +03:00
|
|
|
return clone
|
|
|
|
|
|
|
|
|
|
|
|
def clean_checkout(repo, branch):
|
|
|
|
repo.clean('-f', '-d')
|
|
|
|
repo.reset('--hard')
|
|
|
|
repo.checkout(branch)
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Runner:
|
2016-06-22 17:58:35 +03:00
|
|
|
def __init__(self, cwd):
|
|
|
|
self.cwd = cwd
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
# Import the documentation build module.
|
|
|
|
env.fmt_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.insert(0, os.path.join(env.fmt_dir, 'doc'))
|
|
|
|
import build
|
|
|
|
|
|
|
|
env.build_dir = 'build'
|
2017-07-01 17:30:51 +03:00
|
|
|
env.versions = build.versions
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
# Virtualenv and repos are cached to speed up builds.
|
|
|
|
build.create_build_env(os.path.join(env.build_dir, 'virtualenv'))
|
|
|
|
|
|
|
|
env.fmt_repo = Git(os.path.join(env.build_dir, 'fmt'))
|
|
|
|
return env
|
|
|
|
|
|
|
|
|
2016-06-17 17:30:42 +03:00
|
|
|
@contextmanager
|
|
|
|
def rewrite(filename):
|
|
|
|
class Buffer:
|
|
|
|
pass
|
|
|
|
buffer = Buffer()
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
buffer.data = ''
|
|
|
|
yield buffer
|
|
|
|
return
|
|
|
|
with open(filename) as f:
|
|
|
|
buffer.data = f.read()
|
|
|
|
yield buffer
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write(buffer.data)
|
|
|
|
|
|
|
|
|
2016-06-16 17:53:07 +03:00
|
|
|
fmt_repo_url = 'git@github.com:fmtlib/fmt'
|
|
|
|
|
|
|
|
|
|
|
|
def update_site(env):
|
|
|
|
env.fmt_repo.update(fmt_repo_url)
|
|
|
|
|
|
|
|
doc_repo = Git(os.path.join(env.build_dir, 'fmtlib.github.io'))
|
|
|
|
doc_repo.update('git@github.com:fmtlib/fmtlib.github.io')
|
|
|
|
|
2017-07-01 17:30:51 +03:00
|
|
|
for version in env.versions:
|
2016-06-22 17:29:34 +03:00
|
|
|
clean_checkout(env.fmt_repo, version)
|
2016-06-16 17:53:07 +03:00
|
|
|
target_doc_dir = os.path.join(env.fmt_repo.dir, 'doc')
|
|
|
|
# Remove the old theme.
|
|
|
|
for entry in os.listdir(target_doc_dir):
|
|
|
|
path = os.path.join(target_doc_dir, entry)
|
|
|
|
if os.path.isdir(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
# Copy the new theme.
|
|
|
|
for entry in ['_static', '_templates', 'basic-bootstrap', 'bootstrap',
|
|
|
|
'conf.py', 'fmt.less']:
|
|
|
|
src = os.path.join(env.fmt_dir, 'doc', entry)
|
|
|
|
dst = os.path.join(target_doc_dir, entry)
|
|
|
|
copy = shutil.copytree if os.path.isdir(src) else shutil.copyfile
|
|
|
|
copy(src, dst)
|
|
|
|
# Rename index to contents.
|
|
|
|
contents = os.path.join(target_doc_dir, 'contents.rst')
|
|
|
|
if not os.path.exists(contents):
|
|
|
|
os.rename(os.path.join(target_doc_dir, 'index.rst'), contents)
|
|
|
|
# Fix issues in reference.rst/api.rst.
|
2019-12-14 17:50:53 +03:00
|
|
|
for filename in ['reference.rst', 'api.rst', 'index.rst']:
|
2016-06-16 17:53:07 +03:00
|
|
|
pattern = re.compile('doxygenfunction.. (bin|oct|hexu|hex)$', re.M)
|
2016-06-17 17:30:42 +03:00
|
|
|
with rewrite(os.path.join(target_doc_dir, filename)) as b:
|
|
|
|
b.data = b.data.replace('std::ostream &', 'std::ostream&')
|
|
|
|
b.data = re.sub(pattern, r'doxygenfunction:: \1(int)', b.data)
|
|
|
|
b.data = b.data.replace('std::FILE*', 'std::FILE *')
|
|
|
|
b.data = b.data.replace('unsigned int', 'unsigned')
|
2020-04-06 17:32:15 +03:00
|
|
|
#b.data = b.data.replace('operator""_', 'operator"" _')
|
2020-07-06 19:50:24 +03:00
|
|
|
b.data = b.data.replace(
|
|
|
|
'format_to_n(OutputIt, size_t, string_view, Args&&',
|
|
|
|
'format_to_n(OutputIt, size_t, const S&, const Args&')
|
|
|
|
b.data = b.data.replace(
|
|
|
|
'format_to_n(OutputIt, std::size_t, string_view, Args&&',
|
|
|
|
'format_to_n(OutputIt, std::size_t, const S&, const Args&')
|
|
|
|
if version == ('3.0.2'):
|
|
|
|
b.data = b.data.replace(
|
|
|
|
'fprintf(std::ostream&', 'fprintf(std::ostream &')
|
|
|
|
if version == ('5.3.0'):
|
|
|
|
b.data = b.data.replace(
|
|
|
|
'format_to(OutputIt, const S&, const Args&...)',
|
|
|
|
'format_to(OutputIt, const S &, const Args &...)')
|
|
|
|
if version.startswith('5.') or version.startswith('6.'):
|
2020-07-06 17:15:39 +03:00
|
|
|
b.data = b.data.replace(', size_t', ', std::size_t')
|
2020-07-06 19:50:24 +03:00
|
|
|
if version.startswith('7.'):
|
2020-07-06 17:15:39 +03:00
|
|
|
b.data = b.data.replace(', std::size_t', ', size_t')
|
|
|
|
b.data = b.data.replace('join(It, It', 'join(It, Sentinel')
|
2021-06-27 18:55:16 +03:00
|
|
|
if version.startswith('7.1.'):
|
|
|
|
b.data = b.data.replace(', std::size_t', ', size_t')
|
|
|
|
b.data = b.data.replace('join(It, It', 'join(It, Sentinel')
|
|
|
|
b.data = b.data.replace(
|
|
|
|
'fmt::format_to(OutputIt, const S&, Args&&...)',
|
|
|
|
'fmt::format_to(OutputIt, const S&, Args&&...) -> ' +
|
|
|
|
'typename std::enable_if<enable, OutputIt>::type')
|
2019-12-14 17:50:53 +03:00
|
|
|
b.data = b.data.replace('aa long', 'a long')
|
2020-07-03 01:09:34 +03:00
|
|
|
b.data = b.data.replace('serveral', 'several')
|
2020-05-15 05:11:40 +03:00
|
|
|
if version.startswith('6.2.'):
|
2020-04-06 17:32:15 +03:00
|
|
|
b.data = b.data.replace(
|
|
|
|
'vformat(const S&, basic_format_args<' +
|
|
|
|
'buffer_context<Char>>)',
|
|
|
|
'vformat(const S&, basic_format_args<' +
|
|
|
|
'buffer_context<type_identity_t<Char>>>)')
|
2016-06-17 17:30:42 +03:00
|
|
|
# Fix a broken link in index.rst.
|
|
|
|
index = os.path.join(target_doc_dir, 'index.rst')
|
|
|
|
with rewrite(index) as b:
|
2016-06-22 16:29:18 +03:00
|
|
|
b.data = b.data.replace(
|
|
|
|
'doc/latest/index.html#format-string-syntax', 'syntax.html')
|
2016-06-16 17:53:07 +03:00
|
|
|
# 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
|
2018-05-26 17:02:49 +03:00
|
|
|
if LooseVersion(version) >= LooseVersion('5.0.0'):
|
|
|
|
include_dir = os.path.join(include_dir, 'include', 'fmt')
|
|
|
|
elif LooseVersion(version) >= LooseVersion('3.0.0'):
|
2016-06-16 17:53:07 +03:00
|
|
|
include_dir = os.path.join(include_dir, 'fmt')
|
|
|
|
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'))
|
|
|
|
# Create symlinks for older versions.
|
|
|
|
for link, target in {'index': 'contents', 'api': 'reference'}.items():
|
|
|
|
link = os.path.join(html_dir, link) + '.html'
|
|
|
|
target += '.html'
|
|
|
|
if os.path.exists(os.path.join(html_dir, target)) and \
|
|
|
|
not os.path.exists(link):
|
|
|
|
os.symlink(target, link)
|
|
|
|
# Copy docs to the website.
|
|
|
|
version_doc_dir = os.path.join(doc_repo.dir, version)
|
2017-07-01 17:30:51 +03:00
|
|
|
try:
|
|
|
|
shutil.rmtree(version_doc_dir)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
2016-06-16 17:53:07 +03:00
|
|
|
shutil.move(html_dir, version_doc_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def release(args):
|
|
|
|
env = create_build_env()
|
2016-06-22 17:58:35 +03:00
|
|
|
fmt_repo = env.fmt_repo
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
branch = args.get('<branch>')
|
|
|
|
if branch is None:
|
|
|
|
branch = 'master'
|
2016-06-22 17:58:35 +03:00
|
|
|
if not fmt_repo.update('-b', branch, fmt_repo_url):
|
|
|
|
clean_checkout(fmt_repo, branch)
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
# Convert changelog from RST to GitHub-flavored Markdown and get the
|
|
|
|
# version.
|
|
|
|
changelog = 'ChangeLog.rst'
|
2016-06-22 17:58:35 +03:00
|
|
|
changelog_path = os.path.join(fmt_repo.dir, changelog)
|
2016-06-16 17:53:07 +03:00
|
|
|
import rst2md
|
|
|
|
changes, version = rst2md.convert(changelog_path)
|
|
|
|
cmakelists = 'CMakeLists.txt'
|
2016-06-22 17:58:35 +03:00
|
|
|
for line in fileinput.input(os.path.join(fmt_repo.dir, cmakelists),
|
2016-06-16 17:53:07 +03:00
|
|
|
inplace=True):
|
|
|
|
prefix = 'set(FMT_VERSION '
|
|
|
|
if line.startswith(prefix):
|
|
|
|
line = prefix + version + ')\n'
|
|
|
|
sys.stdout.write(line)
|
|
|
|
|
|
|
|
# Update the version in the changelog.
|
|
|
|
title_len = 0
|
|
|
|
for line in fileinput.input(changelog_path, inplace=True):
|
2022-01-02 19:22:25 +03:00
|
|
|
if line.startswith(version + ' - TBD'):
|
2016-06-16 17:53:07 +03:00
|
|
|
line = version + ' - ' + datetime.date.today().isoformat()
|
|
|
|
title_len = len(line)
|
|
|
|
line += '\n'
|
|
|
|
elif title_len:
|
|
|
|
line = '-' * title_len + '\n'
|
|
|
|
title_len = 0
|
|
|
|
sys.stdout.write(line)
|
2017-07-01 16:17:37 +03:00
|
|
|
|
2017-07-01 17:30:51 +03:00
|
|
|
# Add the version to the build script.
|
|
|
|
script = os.path.join('doc', 'build.py')
|
2017-07-01 16:17:37 +03:00
|
|
|
script_path = os.path.join(fmt_repo.dir, script)
|
|
|
|
for line in fileinput.input(script_path, inplace=True):
|
2017-07-01 17:30:51 +03:00
|
|
|
m = re.match(r'( *versions = )\[(.+)\]', line)
|
2017-07-01 16:17:37 +03:00
|
|
|
if m:
|
2017-07-01 17:30:51 +03:00
|
|
|
line = '{}[{}, \'{}\']\n'.format(m.group(1), m.group(2), version)
|
2017-07-01 16:17:37 +03:00
|
|
|
sys.stdout.write(line)
|
|
|
|
|
2016-06-22 17:58:35 +03:00
|
|
|
fmt_repo.checkout('-B', 'release')
|
2017-07-01 16:17:37 +03:00
|
|
|
fmt_repo.add(changelog, cmakelists, script)
|
2016-06-22 17:58:35 +03:00
|
|
|
fmt_repo.commit('-m', 'Update version')
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
# Build the docs and package.
|
2016-06-22 17:58:35 +03:00
|
|
|
run = Runner(fmt_repo.dir)
|
2016-06-16 17:53:07 +03:00
|
|
|
run('cmake', '.')
|
|
|
|
run('make', 'doc', 'package_source')
|
|
|
|
update_site(env)
|
|
|
|
|
|
|
|
# Create a release on GitHub.
|
2016-06-22 17:58:35 +03:00
|
|
|
fmt_repo.push('origin', 'release')
|
2022-01-02 20:29:42 +03:00
|
|
|
auth_headers = {'Authorization': 'token ' + os.getenv('FMT_TOKEN')}
|
2016-06-16 17:53:07 +03:00
|
|
|
r = requests.post('https://api.github.com/repos/fmtlib/fmt/releases',
|
2022-01-02 20:29:42 +03:00
|
|
|
headers=auth_headers,
|
2016-06-16 17:53:07 +03:00
|
|
|
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 19:30:58 +03:00
|
|
|
id = r.json()['id']
|
|
|
|
uploads_url = 'https://uploads.github.com/repos/fmtlib/fmt/releases'
|
|
|
|
package = 'fmt-{}.zip'.format(version)
|
2018-06-05 17:58:02 +03:00
|
|
|
r = requests.post(
|
|
|
|
'{}/{}/assets?name={}'.format(uploads_url, id, package),
|
2022-01-02 20:29:42 +03:00
|
|
|
headers={'Content-Type': 'application/zip'} | auth_headers,
|
|
|
|
data=open('build/fmt/' + package, 'rb'))
|
2018-06-05 17:58:02 +03:00
|
|
|
if r.status_code != 201:
|
|
|
|
raise Exception('Failed to upload an asset ' + str(r))
|
2016-06-16 17:53:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = docopt.docopt(__doc__)
|
|
|
|
if args.get('release'):
|
|
|
|
release(args)
|
|
|
|
elif args.get('site'):
|
|
|
|
update_site(create_build_env())
|