forked from qiskit-community/qiskit-textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_redirections.py
44 lines (36 loc) · 1.61 KB
/
create_redirections.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
'''
This script creates HTTP redirections for those routes defined in the module
variable REDIRECTIONS.
'''
import os
import sys
overwrite_option = '--overwrite'
# Add keys and pairs where:
# - The key is the old absolute URL. Discard the baseurl and the leading slash.
# - The value is the new URL relative to the old one.
REDIRECTIONS = {
'ch-prerequisites/linear_algebra.html': '../ch-appendix/linear_algebra.html',
'ch-algorithms/deutsch-josza.html': '../ch-algorithms/deutsch-jozsa.html',
'ch-quantum-hardware/calibrating-qubits-openpulse.html': 'calibrating-qubits-pulse.html'
}
def create_redirection (buildpath, old, new, overwrite=False):
'''
Creates a HTML redirection.
'''
redirectionpath = os.path.join(buildpath, old)
redirectioncontent = f'<head><meta http-equiv="Refresh" content="0; URL={new}"></head>'
if os.path.exists(redirectionpath) and not overwrite:
print(f'File `{redirectionpath}` already exists. Pass {overwrite_option} to force overwritting.')
return
folder = os.path.dirname(redirectionpath)
os.makedirs(folder, exist_ok=True)
with open(redirectionpath, 'w') as file_:
print(f'Creating redirection at {redirectionpath} -> {new} ')
file_.write(redirectioncontent)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit(f'Usage: python3 create_redirections.py [{overwrite_option}] <build-dir>')
overwrite = sys.argv[1] == overwrite_option
base_dir = sys.argv[1] if not overwrite else sys.argv[2]
for (old, new) in REDIRECTIONS.items():
create_redirection(base_dir, old, new, overwrite=overwrite)