-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathupdatesite.py
executable file
·227 lines (192 loc) · 6.35 KB
/
updatesite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# HORTON: Helpful Open-source Research TOol for N-fermion systems.
# Copyright (C) 2011-2022 The HORTON Development Team
#
# This file is part of HORTON.
#
# HORTON is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# HORTON is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
#
# --
"""Generates documentation and ammends it to the gh-pages branch.
This script can manage different documentation versions and generates an index.html with
the directory of different documentation versions. The style of the index is similar to
the read_the_docs theme in Sphinx.
"""
from distutils.version import StrictVersion
import os
from glob import glob
import json
import shutil
import subprocess
css_template = r"""
body {
font-family: "Lato",sans-serif;
background-color: #edf0f2;
font-size: 16px;
}
a {
color: #2980B9;
text-decoration: none;
cursor: pointer;
}
a:visited {
color: #9B59B6;
}
h1 {
font-size: 28px;
font-weight: 700;
font-family: "Roboto Slab",sans-serif;
}
h2 {
font-size: 24px;
font-weight: 700;
font-family: "Roboto Slab",sans-serif;
}
span.fineprint {
font-size: 12px;
color: #999999;
}
"""
html_template = r'''<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HORTON Documentation Directory</title>
<meta name="author" content="Toon Verstraelen">
<link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,700' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<style>
{css}
</style>
</head>
<body>
<h1>HORTON Documentation Directory</h1>
<h2>Stable releases</h2>
<ul>{stable}</ul>
<h2>Beta releases (feature freeze)</h2>
<ul>{beta}</ul>
<h2>Alpha releases (features being added)</h2>
<ul>{alpha}</ul>
</body>
</html>
'''
release_template = r'''
<li><a href="{version}/index.html">HORTON {version}</a> released on {release_date}.<br \>
<span class='fineprint'>Documentation built on {doc_build_date} from HORTON {describe} ({doc_release_date}) .</span>
</li>
'''
redirect_template = r'''<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Latest HORTON Documentation (redirect)</title>
<meta http-equiv="Refresh" content="2; url=../{version}/index.html">
<meta name="author" content="Toon Verstraelen">
<link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,700' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<style>
{css}
</style>
</head>
<body>
<h2>Redirecting to the latest stable version of the HORTON documentation ...</h2>
<p>... or you can click <a href="../{version}/index.html">here</a>
</html>
'''
def version_sort_key(releaseinfo):
"""Return a sort key for versioninfo dictionaries."""
return StrictVersion(releaseinfo['version'])
def format_releases(releases):
"""Format a list of releases into HTML bullet points.
Parameters
----------
versions : list of str
The list of versions.
"""
version_lines = [release_template.format(**releaseinfo) for releaseinfo in releases]
return ''.join(version_lines)
def update_index():
"""Update index.html (the documentation directory)."""
releases = {
'stable': [],
'beta': [],
'alpha': [],
}
for fn_ri in sorted(glob('*.*.*/releaseinfo.json')):
with open(fn_ri) as f:
releaseinfo = json.load(f)
print('Found', releaseinfo['version'])
if 'b' in releaseinfo['version']:
releases['beta'].append(releaseinfo)
elif 'a' in releaseinfo['version']:
releases['alpha'].append(releaseinfo)
else:
releases['stable'].append(releaseinfo)
for values in releases.values():
values.sort(key=version_sort_key, reverse=True)
# Write the index file
subs = dict((key, format_releases(versions))
for key, versions in releases.items())
subs['css'] = css_template
with open('index.html', 'w') as f:
f.write(html_template.format(**subs))
# Write the redirect file.
if len(releases['stable']) > 0:
subs = releases['stable'][0].copy()
subs['css'] = css_template
if not os.path.isdir('latest'):
os.mkdir('latest')
with open('latest/index.html', 'w') as f:
f.write(redirect_template.format(**subs))
def call(command, cwd=None):
"""Call a command and print it on screen.
Parameters
----------
command : list
A list of command line arguments, the first being the executable.
cwd : str
When given, the script is executed in the directory `cwd`.
"""
command_str = ' '.join(('\'%s\'' % arg) if (' ' in arg) else arg for arg in command)
if cwd is None:
print(command_str)
else:
print(command_str, '(in %s)' % cwd)
return subprocess.check_output(command, cwd=cwd)
def main():
"""Main program."""
# Get the version string from the git tag
version = call(['git', 'describe', '--tags'])
version = version[:version.find('-')]
# de-cruft
call(['./cleanfiles.sh'])
# Build the docs
call(['make', 'html'], cwd='doc')
# Copy releas info file
shutil.copy('doc/releaseinfo.json', 'doc/_build/html/releaseinfo.json')
# Copy the docs to the gh-pages branch, removing the old copy for the current version
call(['git', 'checkout', 'gh-pages'])
if os.path.exists(version):
call(['git', 'rm', '-r', version])
os.mkdir(version)
call(['rsync', '-av', 'doc/_build/html/', version])
# Update the documentation directory
update_index()
# Add new files to commit
call(['git', 'add', version, 'latest', 'index.html'])
# Commit
call(['git', 'commit', '-a', '--amend', '-m', 'Automatic documentation update', '--no-verify'])
if __name__ == '__main__':
main()