-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_site.py
136 lines (104 loc) · 3.69 KB
/
prep_site.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
"""Prepare the HSUPipeline site."""
import os
from pathlib import Path
from copy import deepcopy
from shutil import copyfile
###################################################################################################
###################################################################################################
# Define the string definitions of commands to use
CLONE_COMMAND = 'git clone https://github.com/HSUPipeline/{}'
RM_COMMAND = 'rm -rf {}'
# Define repo(s) to copy from
REPO = 'Overview'
# Define what to add to the files
ADD_LINES = [
'---\n',
'title: {}\n',
'layout: page\n',
'permalink: /{}/\n',
'---\n',
'\n'
]
# Define output folder
FOLDER = Path('outputs')
###################################################################################################
###################################################################################################
def main():
"""Main function to manage site creation."""
# Check for and create (if missing) outputs folder
if not os.path.exists(FOLDER):
os.mkdir(FOLDER)
# Process files to create webpage
os.system(CLONE_COMMAND.format(REPO))
create_page(REPO, 'README.md', 'index.md', 'HSUPipeline')
drop_lines(FOLDER / 'index.md', ['permalink'])
create_page(REPO, 'Templates.md', 'templates.md', 'Templates')
create_page(REPO, 'Sorting.md', 'sorting.md', 'Sorting')
create_page(REPO, 'Converting.md', 'converting.md', 'Converting')
create_page(REPO, 'Analysis.md', 'analysis.md', 'Analysis')
create_page(REPO, 'Projects.md', 'projects.md', 'Projects')
create_page(REPO, 'CodeMap.md', 'codemap.md', 'CodeMap')
os.system(RM_COMMAND.format(REPO))
def create_page(repo, file_name, page_name, label):
"""Create web page(s) by cloning and copying from a repository.
Parameter
---------
repo : str
Repository name.
file_name : str or list of str
File name to copy.
page_name : str or list of str
Name to give the webpage.
label : str or list of str
Label to add into the header information.
"""
copyfile(Path(repo) / file_name, FOLDER / page_name)
update_file(FOLDER / page_name, ADD_LINES, label)
def update_file(filename, add_lines, label):
"""Helper function to update file contents.
Parameters
----------
filename : str or Path
Name of the file to load and update.
add_lines : list of str
Lines to add to the file.
label : str
Label to add into the header information.
"""
add_lines = deepcopy(add_lines)
with open(filename, 'r') as file:
contents = file.readlines()
# Drop the first couple lines (title gets added from header info)
contents = contents[2:]
# Add in header information
add_lines[1] = add_lines[1].format(label)
add_lines[3] = add_lines[3].format(label.lower())
# Add header lines
for line in reversed(add_lines):
contents.insert(0, line)
with open(filename, 'w') as file:
file.writelines(contents)
def drop_lines(filename, lines_to_drop):
"""Helper function to drop lines from files.
Parameters
----------
filename : str or Path
Name of the file to load and update.
lines_to_drop : list of str
Lines to drop from the file.
"""
with open(filename, 'r') as file:
contents = file.readlines()
output = []
for line in contents:
dropped = False
for drop in lines_to_drop:
if drop in line:
dropped = True
break
if not dropped:
output.append(line)
with open(filename, 'w') as file:
file.writelines(output)
if __name__ == "__main__":
main()