forked from particleKIT/StaticKIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.py
More file actions
executable file
·265 lines (228 loc) · 8.88 KB
/
Copy pathpublish.py
File metadata and controls
executable file
·265 lines (228 loc) · 8.88 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/python3
from jinja import template, Template
import yaml
import os
from shutil import copy2, copystat
import argparse
import logging
from datetime import datetime
import re
import sys
"""
parse arguments and print help file
"""
parser = argparse.ArgumentParser(description='Build static htmls from Jinja2 templates that fake the KIT layout')
parser.add_argument('--verbose',
'-v',
action='count',
help='more output (use multiple times to increase verbosity level)',
default=0)
parser.add_argument('--dryrun',
'-d',
action='store_true',
help='don\'t write anything to files')
parser.add_argument('--force',
'-f',
action='store_true',
help='don\'t ask to override existing files')
parser.add_argument('--inputdir',
'-i',
default='.',
help='directory containing the jinja pages and a config.yml (defaults to the working directory)')
parser.add_argument('--outputdir',
'-o',
default=os.path.expanduser('~') + '/.public_html',
help='destination directory for static html website (defaults to ~/.public_html)')
parser.add_argument('--init',
'-ii',
nargs='*',
help='initialize a new project at directory INIT')
parser.add_argument('--diff',
'-dd',
action='store_true',
help='show a simple diff between edited files')
"""
basic variable substitions in
yaml files using jinja2
"""
def parse_vars(yamlin):
# save the initial object
if 'global_yaml' not in globals():
global global_yaml
global_yaml = yamlin
# recursively go through the object
if isinstance(yamlin, list):
yamlout=[]
for sub in yamlin:
yamlout.append(parse_vars(sub))
elif isinstance(yamlin, dict):
yamlout={}
for sub in yamlin:
yamlout[sub] = parse_vars(yamlin[sub])
# treat each string as jinja template
elif isinstance(yamlin, str):
yamlout = Template(yamlin).render(global_yaml)
# be sure nothing gets lost
else:
yamlout = yamlin
return yamlout
"""
anotherversion of copytree
src: source directory
dst: destination directory
ignore: list of ignored files/dirs
"""
def copytree(src, dst, ignore=[], verbose=0, force=False):
if os.path.isdir(src) and not os.path.exists(dst):
os.makedirs(dst)
names = os.listdir(src)
for name in names:
if name in ignore:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
if os.path.isdir(srcname):
if not os.path.exists(dstname):
os.makedirs(dstname)
copytree(srcname, dstname, ignore, verbose, force)
else:
if os.path.exists(dstname):
srclastedit = os.path.getmtime(srcname)
dstlastedit = os.path.getmtime(dstname)
if srclastedit != dstlastedit:
override = input(dstname + " was edited, override? (y/n): ") if not force else 'y'
if override == "y":
if verbose > 0:
print('removing ' + dstname)
os.remove(dstname)
else:
continue
else:
continue
if verbose > 0:
print('copying ' + srcname + ' to ' + dstname)
try:
copy2(srcname, dstname)
except FileNotFoundError:
logging.error(srcname + ' not found')
try:
copystat(src, dst)
except FileNotFoundError:
logging.error(srcname + ' not found')
"""
initialize a new project
dest: destination where the project is initialized
"""
def init(dest, dryrun=False, force=False):
if len(dest) != 2 or not os.path.isdir(os.path.dirname(os.path.abspath(__file__)) + '/init/' + dest[1]):
logging.error('--init takes two arguments: [destination directory] [project type]')
logging.error('The following project types are available:\n' + '\n'.join(os.listdir(os.path.dirname(os.path.abspath(__file__)) + '/init/')))
exit(1)
if os.path.isdir(dest[0]) and os.listdir(dest[0]) !="" and not force:
logging.error('directory ' + dest[0] + ' already exists and is not empty!')
exit(1)
if not dryrun:
print("copying " + dest[1] + " project to " + dest[0])
copytree(os.path.dirname(os.path.abspath(__file__)) + '/init/' + dest[1], dest[0], [], force)
"""
ckeck if a path exists
"""
def ispath(path):
path = os.path.abspath(path)
if os.path.exists(path):
return path
else:
logging.error(path + ' does not exists')
return False
"""
load YAML rules from file
and build htmls wih jinja2
"""
def main():
args = parser.parse_args()
logging.basicConfig(format='%(levelname)s: %(message)s')
if args.verbose == 0:
logging.getLogger().setLevel(logging.ERROR)
elif args.verbose == 1:
logging.getLogger().setLevel(logging.INFO)
elif args.verbose >= 2:
logging.getLogger().setLevel(logging.DEBUG)
logging.debug("running in debug mode")
else:
logging.getLogger().setLevel(logging.DEBUG)
if args.init:
init(args.init, args.dryrun, args.force)
else:
sourcesdir = ispath(os.path.dirname(os.path.abspath(__file__)) + '/sources')
inputdir = ispath(args.inputdir)
pagesdir = ispath(args.inputdir + '/pages')
outputdir = os.path.abspath(args.outputdir)
configfile = ispath(args.inputdir + '/config.yml')
logging.debug("input location: " + inputdir)
logging.debug("output location: " + outputdir)
logging.debug("using config file from: " + configfile)
logging.debug("using sources from: " + sourcesdir)
try:
logging.info("opening config file " + configfile)
stream = open(configfile, 'r')
except FileNotFoundError:
logging.error("could not open config file")
logging.error("file not found:" + configfile)
exit(1)
try:
logging.info("parsing config.yml")
rules = yaml.load(stream)
for i in range(len(rules)): # do this to catch sub definitions
rules = parse_vars(rules)
except Exception as e:
# TODO raise typical yaml exceptions
logging.error("config.yml seems not to be a valid yaml file.")
logging.error(e)
exit(1)
logging.debug("setting additional template variables")
rules.update({'date': datetime.now() })
logging.debug("using YAML structure:\n+++++\n" + yaml.dump(rules) + "+++++")
logging.info("applying template rules from YAML")
html = template()
html.add_subst(rules)
mode = 'r' if args.dryrun else 'w'
if pagesdir:
logging.info("saving resulting documents to " + outputdir)
print('building html pages from ' + inputdir)
html.save(pagesdir, outputdir + '/pages', mode, args.diff)
if sourcesdir and ispath(sourcesdir + '/index.html'):
print('building homepage from ' + sourcesdir + '/index.html')
html.save(sourcesdir + '/index.html', outputdir + '/index.html', mode, args.diff)
else:
logging.error('cannot create an index.html')
exit(1)
if 'root_templates' in rules and isinstance(rules['root_templates'], list):
print('building extra template files in document root.')
for tpl in rules['root_templates']:
if not os.path.isfile(inputdir + '/' + tpl):
logging.error('file not found: ' + inputdir + '/' + tpl)
continue
if not args.dryrun:
html.save(inputdir + '/' + tpl, outputdir + '/' + tpl, mode, args.diff)
html.clear()
if 'copy_files' in rules and isinstance(rules['copy_files'], list):
print('copying extra files')
for f in rules['copy_files']:
i = inputdir + '/' + f
o = outputdir + '/' + f
if os.path.isdir(i) and not args.dryrun:
copytree(i, o, [], args.verbose, args.force)
elif os.path.isfile(i) and not args.dryrun:
copy2(i, o)
if not args.dryrun and sourcesdir:
copytree(sourcesdir,
outputdir,
['index.html'],
args.verbose,
args.force)
print('done!')
"""
run main if this file is executed
"""
if __name__ == "__main__":
main()