Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make various improvements to site/build_docs.py #589

Merged
merged 6 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 58 additions & 45 deletions site/build_docs.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#!/usr/bin/env python3

# Copyright (C) 2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

import os
from pathlib import Path
import shutil
import subprocess
import tarfile
import tempfile

from packaging import version
import git
import toml

MINIMUM_VERSION='0.1.11' # the initial version for the documentation site
# the initial version for the documentation site
MINIMUM_VERSION = version.Version('0.1.11')

def prepare_tags(repo):
tags = {}
for tag in repo.tags:
tag_version = version.parse(tag.name)
if tag_version >= version.Version(MINIMUM_VERSION) and not tag_version.is_prerelease:
if tag_version >= MINIMUM_VERSION and not tag_version.is_prerelease:
release_version = (tag_version.major, tag_version.minor)
if not release_version in tags or tag_version > version.parse(tags[release_version].name):
if release_version not in tags or tag_version > version.parse(tags[release_version].name):
tags[release_version] = tag

return tags.values()
Expand All @@ -34,14 +39,21 @@ def write_version_item(file_object, version, url):
for v in versions:
write_version_item(f, v, '{}/{}'.format(url_prefix, v))

def git_checkout(tagname, cwd):
docs_dir = os.path.join(cwd, 'site', 'content', 'en', 'docs')
shutil.rmtree(docs_dir)
repo.git.checkout(tagname, '--', 'site/content/en/docs')
if tagname != "v0.1.11":
images_dir = os.path.join(cwd, 'site', 'content', 'en', 'images')
shutil.rmtree(images_dir)
repo.git.checkout(tagname, '--', 'site/content/en/images')
def git_checkout(tagname, repo, temp_dir):
subdirs = ['site/content/en/docs']
if version.Version(tagname) != MINIMUM_VERSION:
subdirs.append('site/content/en/images')

for subdir in subdirs:
shutil.rmtree(temp_dir / subdir)

with tempfile.TemporaryFile() as archive:
# `git checkout` doesn't work for this, as it modifies the index.
# `git restore` would work, but it's only available since Git 2.23.
repo.git.archive(tagname, '--', subdir, output_stream=archive)
archive.seek(0)
with tarfile.open(fileobj=archive) as tar:
tar.extractall(temp_dir)

def change_version_menu_toml(filename, version):
data = toml.load(filename)
Expand All @@ -51,38 +63,39 @@ def change_version_menu_toml(filename, version):
toml.dump(data, f)

def generate_docs(repo, output_dir, tags):
def run_hugo(content_loc, destination_dir):
subprocess.run([ # nosec
'hugo',
'--destination',
destination_dir,
'--config',
'config.toml,versioning.toml',
],
cwd=content_loc,
)

cwd = repo.working_tree_dir
content_loc = os.path.join(cwd, 'site')
if not os.path.exists(output_dir):
os.makedirs(output_dir)

generate_versioning_config(os.path.join(cwd, 'site', 'versioning.toml'), (t.name for t in tags))
change_version_menu_toml(os.path.join(cwd, 'site', 'versioning.toml'), 'develop')
run_hugo(content_loc, output_dir)

generate_versioning_config(os.path.join(cwd, 'site', 'versioning.toml'), (t.name for t in tags), '/..')
for tag in tags:
git_checkout(tag.name, cwd)
destination_dir = os.path.join(output_dir, tag.name)
change_version_menu_toml(os.path.join(cwd, 'site', 'versioning.toml'), tag.name)
os.makedirs(destination_dir)
run_hugo(content_loc, destination_dir)
repo_root = Path(repo.working_tree_dir)

with tempfile.TemporaryDirectory() as temp_dir:
content_loc = Path(temp_dir, 'site')
shutil.copytree(repo_root / 'site', content_loc, symlinks=True)

def run_hugo(destination_dir):
subprocess.run([ # nosec
'hugo',
'--destination',
str(destination_dir),
'--config',
'config.toml,versioning.toml',
],
cwd=content_loc,
check=True,
)

versioning_toml_path = content_loc / 'versioning.toml'

generate_versioning_config(versioning_toml_path, (t.name for t in tags))
change_version_menu_toml(versioning_toml_path, 'develop')
run_hugo(output_dir)

generate_versioning_config(versioning_toml_path, (t.name for t in tags),
'/..')
for tag in tags:
git_checkout(tag.name, repo, Path(temp_dir))
change_version_menu_toml(versioning_toml_path, tag.name)
run_hugo(output_dir / tag.name)

if __name__ == "__main__":
repo_root = os.getcwd()
repo = git.Repo(repo_root)
output_dir = os.path.join(repo_root, 'public')

tags = prepare_tags(repo)
generate_docs(repo, output_dir, tags)
repo_root = Path(__file__).resolve().parents[1]
with git.Repo(repo_root) as repo:
tags = prepare_tags(repo)
generate_docs(repo, repo_root / 'public', tags)
2 changes: 1 addition & 1 deletion site/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enableRobotsTXT = true
theme = ["docsy"]

# Will give values to .Lastmod etc.
enableGitInfo = true
enableGitInfo = false

# Language settings
contentDir = "content/en"
Expand Down