-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpage.py
155 lines (129 loc) · 4.98 KB
/
webpage.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
import argparse
import os
import pathlib
import shutil
import subprocess
def parse_arguments():
"""
Read and process command line arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'--checkout',
nargs='?', const='gh-pages', default=None,
help='branch to checkout /v directory contents from. For example, --checkout=upstream/gh-pages. --checkout is equivalent to --checkout=gh-pages. If --checkout is ommitted, no checkout is performed.',
)
parser.add_argument(
'--version',
default=os.environ.get('TRAVIS_COMMIT', 'local'),
help="Used to create webpage/v/{version} directory. Generally a commit hash, tag, or 'local'",
)
args = parser.parse_args()
return args
def configure_directories(args):
"""
Add directories to args and create them if neccessary.
Note that versions_directory is the parent of version_directory.
"""
args_dict = vars(args)
# Directory where Manubot outputs reside
args_dict['output_directory'] = pathlib.Path('output')
# Set webpage directory
args_dict['webpage_directory'] = pathlib.Path('webpage')
# Create webpage/v directory (if it doesn't already exist)
args_dict['versions_directory'] = args.webpage_directory.joinpath('v')
args.versions_directory.mkdir(exist_ok=True)
# Checkout existing version directories
checkout_existing_versions(args)
# Create empty webpage/v/version directory
version_directory = args.versions_directory.joinpath(args.version)
if version_directory.is_dir():
print(f'{version_directory} exists: replacing it with an empty directory')
shutil.rmtree(version_directory)
version_directory.mkdir()
args_dict['version_directory'] = version_directory
# Symlink webpage/v/latest to point to webpage/v/commit
latest_directory = args.versions_directory.joinpath('latest')
if latest_directory.is_symlink() or latest_directory.is_file():
latest_directory.unlink()
elif latest_directory.is_dir():
shutil.rmtree(latest_directory)
latest_directory.symlink_to(args.version, target_is_directory=True)
args_dict['latest_directory'] = latest_directory
# Create freeze directory
freeze_directory = args.versions_directory.joinpath('freeze')
freeze_directory.mkdir(exist_ok=True)
args_dict['freeze_directory'] = freeze_directory
return args
def checkout_existing_versions(args):
"""
Must populate webpage/v from the gh-pages branch to get history
References:
http://clubmate.fi/git-checkout-file-or-directories-from-another-branch/
https://stackoverflow.com/a/2668947/4651668
https://stackoverflow.com/a/16493707/4651668
Command modeled after:
git --work-tree=webpage checkout upstream/gh-pages -- v
"""
if not args.checkout:
return
command = [
'git',
f'--work-tree={args.webpage_directory}',
'checkout',
args.checkout,
'--',
'v',
]
print('Attempting checkout with the following command:', ' '.join(command), sep='\n')
process = subprocess.run(command, stderr=subprocess.PIPE)
if process.returncode == 0:
# Addresses an odd behavior where git checkout stages v/* files that don't actually exist
subprocess.run(['git', 'add', 'v'])
else:
print(f'Checkout returned a nonzero exit status. See stderr:\n{process.stderr.decode()}')
def create_version(args):
"""
Populate the version directory for a new version.
"""
# Copy content/images to webpage/v/commit/images
shutil.copytree(
src=pathlib.Path('content/images'),
dst=args.version_directory.joinpath('images'),
)
# Copy output files to to webpage/v/version/
renamer = {
'manuscript.html': 'index.html',
'manuscript.pdf': 'manuscript.pdf',
}
for src, dst in renamer.items():
shutil.copy2(
src=args.output_directory.joinpath(src),
dst=args.version_directory.joinpath(dst),
)
# Copy webpage/github-pandoc.css to to webpage/v/version/github-pandoc.css
shutil.copy2(
src=args.webpage_directory.joinpath('github-pandoc.css'),
dst=args.version_directory.joinpath('github-pandoc.css'),
)
# Create v/freeze to redirect to v/commit
path = pathlib.Path('build/assets/redirect-template.html')
redirect_html = path.read_text()
redirect_html = redirect_html.format(url=f'../{args.version}/')
args.freeze_directory.joinpath('index.html').write_text(redirect_html)
def get_versions():
"""
Extract versions from the webpage/v directory, which should each contain
a manuscript.
"""
versions = {x.name for x in args.versions_directory.iterdir() if x.is_dir()}
versions -= {'freeze', 'latest'}
versions = sorted(versions)
return versions
if __name__ == '__main__':
args = parse_arguments()
configure_directories(args)
print(args)
create_version(args)
versions = get_versions()
print(versions)