-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_site.py
77 lines (57 loc) · 2.06 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
"""Prepare the OpenLists 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/structuredscience/{}'
RM_COMMAND = 'rm -rf {}'
# Define what to add to the files
ADD_LINES = [
'---\n',
'title: {}\n',
'layout: page\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 index file
os.system(CLONE_COMMAND.format('Overview'))
copyfile(Path('Overview') / 'README.md', FOLDER / 'index.md')
update_file(FOLDER / 'index.md', ADD_LINES, 'StructuredScience')
os.system(RM_COMMAND.format('Overview'))
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 header lines
for line in reversed(add_lines):
contents.insert(0, line)
with open(filename, 'w') as file:
file.writelines(contents)
if __name__ == "__main__":
main()