Implement website update script

This commit is contained in:
Victor Zverovich 2016-06-01 07:45:54 -07:00
parent 5e70843a73
commit 0d9870dd9e

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, re, shutil, sys, tempfile import os, re, shutil, sys
from subprocess import check_call from subprocess import check_call
class Git: class Git:
@ -22,65 +22,68 @@ class Git:
def reset(self, *args): def reset(self, *args):
return self.call('reset', args, cwd=self.dir) return self.call('reset', args, cwd=self.dir)
# Create build environment. def pull(self, *args):
return self.call('pull', args, cwd=self.dir)
def update(self, *args):
if not os.path.exists(self.dir):
self.clone(*args)
# Import the documentation build module.
fmt_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) fmt_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(fmt_dir, 'doc')) sys.path.insert(0, os.path.join(fmt_dir, 'doc'))
import build
work_dir = tempfile.mkdtemp('fmt') work_dir = 'build'
try:
import build
# The virtualenv is stored in ~/.cache/fmt/virtualenv and reused to speed
# up builds.
build.create_build_env(os.path.join(
os.path.expanduser('~'), '.cache', 'fmt', 'virtualenv'))
fmt_repo = Git(os.path.join(work_dir, 'fmt')) # Virtualenv and repos are cached to speed up builds.
fmt_repo.clone('git@github.com:fmtlib/fmt') build.create_build_env(os.path.join(work_dir, 'virtualenv'))
doc_repo = Git('fmtlib.github.io')
doc_repo.clone('git@github.com:fmtlib/fmtlib.github.io')
for version in ['1.0.0']:#, '1.1.0', '2.0.0', '3.0.0']: fmt_repo = Git(os.path.join(work_dir, 'fmt'))
fmt_repo.clean('-f', '-d') fmt_repo.update('git@github.com:fmtlib/fmt')
fmt_repo.reset('--hard')
fmt_repo.checkout(version) doc_repo = Git(os.path.join(work_dir, 'fmtlib.github.io'))
target_doc_dir = os.path.join(fmt_repo.dir, 'doc') doc_repo.update('git@github.com:fmtlib/fmtlib.github.io')
# Remove the old theme.
for entry in os.listdir(target_doc_dir): for version in ['1.0.0']:#, '1.1.0', '2.0.0', '3.0.0']:
path = os.path.join(target_doc_dir, entry) fmt_repo.clean('-f', '-d')
if os.path.isdir(path): fmt_repo.reset('--hard')
shutil.rmtree(path) fmt_repo.checkout(version)
# Copy the new theme. target_doc_dir = os.path.join(fmt_repo.dir, 'doc')
for entry in ['_static', '_templates', 'basic-bootstrap', 'bootstrap', # Remove the old theme.
'conf.py', 'fmt.less']: for entry in os.listdir(target_doc_dir):
src = os.path.join(fmt_dir, 'doc', entry) path = os.path.join(target_doc_dir, entry)
dst = os.path.join(target_doc_dir, entry) if os.path.isdir(path):
copy = shutil.copytree if os.path.isdir(src) else shutil.copyfile shutil.rmtree(path)
copy(src, dst) # Copy the new theme.
# Rename index to contents. for entry in ['_static', '_templates', 'basic-bootstrap', 'bootstrap',
contents = os.path.join(target_doc_dir, 'contents.rst') 'conf.py', 'fmt.less']:
if not os.path.exists(contents): src = os.path.join(fmt_dir, 'doc', entry)
os.rename(os.path.join(target_doc_dir, 'index.rst'), contents) dst = os.path.join(target_doc_dir, entry)
# Fix issues in reference.rst. copy = shutil.copytree if os.path.isdir(src) else shutil.copyfile
reference = os.path.join(target_doc_dir, 'reference.rst') copy(src, dst)
if os.path.exists(reference): # TODO: update links in navbar
with open(reference) as f: # Rename index to contents.
data = f.read() contents = os.path.join(target_doc_dir, 'contents.rst')
data = data.replace('std::ostream &', 'std::ostream&') if not os.path.exists(contents):
data = re.sub('doxygenfunction.. (bin|oct|hexu|hex)', os.rename(os.path.join(target_doc_dir, 'index.rst'), contents)
r'doxygenfunction:: \1(int)', data) # Fix issues in reference.rst.
with open(reference, 'w') as f: reference = os.path.join(target_doc_dir, 'reference.rst')
f.write(data) if os.path.exists(reference):
# Build the docs. with open(reference) as f:
build.build_docs(version, doc_dir=target_doc_dir, include_dir=fmt_repo.dir, data = f.read()
work_dir=work_dir) data = data.replace('std::ostream &', 'std::ostream&')
# Create symlinks for older versions. data = re.sub(r'doxygenfunction.. (bin|oct|hexu|hex)',
for link, target in {'index': 'contents', 'api': 'reference'}.items(): r'doxygenfunction:: \1(int)', data)
os.symlink(target + '.html', os.path.join('html', link) + '.html') with open(reference, 'w') as f:
# Copy docs to the website. f.write(data)
version_doc_dir = os.path.join(doc_repo.dir, version) # Build the docs.
shutil.rmtree(version_doc_dir) html_dir = build.build_docs(version, doc_dir=target_doc_dir,
shutil.move('html', version_doc_dir) include_dir=fmt_repo.dir, work_dir=work_dir)
# TODO: update links in navbar # Create symlinks for older versions.
# TODO: remove doc repo for link, target in {'index': 'contents', 'api': 'reference'}.items():
except: os.symlink(target + '.html', os.path.join(html_dir, link) + '.html')
shutil.rmtree(work_dir) # Copy docs to the website.
version_doc_dir = os.path.join(doc_repo.dir, version)
shutil.rmtree(version_doc_dir)
shutil.move(html_dir, version_doc_dir)