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

Write more redirect stubs #2955

Merged
merged 2 commits into from
Oct 1, 2020
Merged
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
68 changes: 43 additions & 25 deletions maintainer/update_json_stubs_sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import os
import shutil
import xml.etree.ElementTree as ET
import errno
import glob
import textwrap

try:
from urllib.request import Request, urlopen
Expand Down Expand Up @@ -40,6 +43,21 @@ def get_web_file(filename, callback, default):
return callback(data)


def write_redirect(file, version='', outfile=None):
if outfile is None:
outfile = file
url = os.path.join(URL, version, file)
REDIRECT = textwrap.dedent(f"""
<!DOCTYPE html>
<meta charset="utf-8">
<title>Redirecting to {url}</title>
<meta http-equiv="refresh" content="0; URL={url}">
<link rel="canonical" href="{url}">
""")
with open(outfile, 'w') as f:
f.write(REDIRECT)


# ========= WRITE JSON =========
# Update $root/versions.json with links to the right version
versions = get_web_file('versions.json', json.loads, [])
Expand Down Expand Up @@ -68,44 +86,44 @@ def get_web_file(filename, callback, default):
# latest/index.html -> latest release
# dev/index.html -> dev docs

REDIRECT = """
<!DOCTYPE html>
<meta charset="utf-8">
<title>Redirecting to {url}</title>
<meta http-equiv="refresh" content="0; URL={url}">
<link rel="canonical" href="{url}">
"""

for ver in versions[::-1]:
if ver['latest']:
latest_url = ver['url']
latest_version = ver['version']
break
else:
try:
latest_url = versions[-1]['url']
latest_version = versions[-1]['version']
except IndexError:
latest_url = None
latest_version = None

for ver in versions[::-1]:
if 'dev' in ver['version']:
dev_url = ver['url']
dev_version = ver['version']
break
else:
try:
dev_url = versions[-1]['url']
dev_version = versions[-1]['version']
except IndexError:
dev_url = None

if latest_url:
with open('index.html', 'w') as f:
f.write(REDIRECT.format(url=latest_url))

with open('latest/index.html', 'w') as f:
f.write(REDIRECT.format(url=latest_url))

if dev_url:
with open('dev/index.html', 'w') as f:
f.write(REDIRECT.format(url=dev_url))
dev_version = None

if latest_version:
html_files = glob.glob(f'{latest_version}/**/*.html', recursive=True)
for file in html_files:
outfile = file.strip(f'{latest_version}/')
dirname = os.path.dirname(outfile)
if dirname and not os.path.exists(dirname):
try:
os.makedirs(dirname)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise

write_redirect(file, '', outfile)
write_redirect('index.html', latest_version, 'latest/index.html')
write_redirect('objects.inv', latest_version, 'latest/objects.inv')

if dev_version:
write_redirect('index.html', dev_version, 'dev/index.html')

# ========= WRITE SUPER SITEMAP.XML =========
# make one big sitemap.xml
Expand Down