Skip to content

Commit

Permalink
added stubs and updated url request
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyminium committed Jun 14, 2020
1 parent 8861ad7 commit c05165c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
57 changes: 50 additions & 7 deletions maintainer/update_versions_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
import os

try:
from urllib.request import urlopen
from urllib.request import Request, urlopen
except ImportError:
from urllib2 import urlopen
from urllib2 import Request, urlopen

URL = "https://www.mdanalysis.org/mdanalysis/"
# ========= WRITE JSON =========
URL = "https://mdanalysis.github.io/mdanalysis/"

VERSION = os.environ['VERSION']
url = os.path.join(URL, 'versions.json')

try:
data = urlopen(url).read().decode()
versions = json.loads(data)
except:
page = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
data = urlopen(page).read().decode()
except Exception as e:
print(e)
try:
with open('versions.json', 'r') as f:
versions = json.loads(f)
except:
except IOError as e:
print(e)
versions = []
else:
versions = json.loads(data)

existing = [item['version'] for item in versions]
already_exists = VERSION in existing
Expand All @@ -39,3 +45,40 @@
with open("versions.json", 'w') as f:
json.dump(versions, f, indent=2)

# ========= WRITE HTML STUBS =========
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']
break
else:
try:
latest_url = versions[-1]['url']
except IndexError:
latest_url = URL

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

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

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

with open('dev.html', 'w') as f:
f.write(REDIRECT.format(url=dev_url))
2 changes: 1 addition & 1 deletion package/doc/sphinx/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
}

html_context = {
'versions_json_url': 'https://mdanalysis.org/mdanalysis/versions.json'
'versions_json_url': 'https://mdanalysis.github.io/mdanalysis/versions.json'
}

# Add any paths that contain custom themes here, relative to this directory.
Expand Down

0 comments on commit c05165c

Please sign in to comment.