-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
combine.py
executable file
·147 lines (118 loc) · 4.93 KB
/
combine.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
#!/usr/bin/env python
import argparse
import logging
import os
import shutil
import json
import time
# TODO: git clean -dXf
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--name', help='name of the slides')
parser.add_argument('--all', action='store_true', help='collect all the slides')
parser.add_argument('--target', help='Optional target directory. If not given automatically set.')
return parser.parse_args()
def copy_examples(slides_path, resources):
src_examples = os.path.join(slides_path, 'examples')
if os.path.exists(src_examples):
for example in os.listdir(src_examples):
#print(example)
example_path = os.path.join(src_examples, example)
if os.path.islink(example_path):
logging.debug(f"Skipping symlink {example_path}")
elif os.path.isdir(example_path):
logging.debug(f"Copy dir {example_path}")
target_dir = os.path.join(resources, 'examples', example)
if os.path.exists(target_dir):
exit(f"Directory {target_dir} already exists. Are there two subjects with same examples subdir?")
shutil.copytree(example_path, target_dir)
else:
exit(f"Could not handle: '{example_path}'. All examples are expected to be in subdirectories.")
#logging.error(f"{example_path}")
# TODO maybe I should not even handle this just let the code blow up so I will be forced to fix
# this
def copy_images(slides_path, resources):
src_img = os.path.join(slides_path, 'img')
img_dir = os.path.join(resources, 'img')
if not os.path.exists(img_dir):
os.mkdir(img_dir)
if os.path.exists(src_img):
for img in os.listdir(src_img):
img_path = os.path.join(src_img, img)
if os.path.isfile(img_path):
logging.debug(f"Copy image: {img_path}")
shutil.copy(img_path, os.path.join(resources, 'img', img))
def copy_book_cover(slides_path, resources):
src = os.path.join(slides_path, 'title_page.png')
trg = os.path.join(resources, 'title_page.png')
if os.path.exists(src):
shutil.copy(src, trg)
def process_slides(source, target, name, config_file=None):
logging.info(f"Processing slides of {name}")
book = []
resources = os.path.join(target, 'resources')
if not os.path.exists(resources):
os.mkdir(resources)
slides_path = os.path.join(source, name)
if config_file is not None:
with open(os.path.join(slides_path, config_file)) as fh:
config_data = json.load(fh)
md_files = config_data['files']
else:
md_files = list(filter(lambda thing: thing.endswith('.md') and thing != 'README.md', os.listdir(slides_path)))
#print(md_file)
for md_file in md_files:
md_file_path = os.path.join(slides_path, md_file)
new_filename = f"{name}-{md_file}"
shutil.copy(md_file_path, os.path.join(target, new_filename))
book.append(new_filename)
copy_examples(slides_path, resources)
copy_images(slides_path, resources)
copy_book_cover(slides_path, resources)
return book
def main():
logging.basicConfig(
level = logging.DEBUG,
format = '%(asctime)s - %(levelname)-5s - %(funcName)s - %(name)s - %(message)s',
datefmt = "%Y-%m-%d %H:%M:%S"
)
args = get_arguments()
root = os.path.dirname(os.path.abspath(__file__))
start = time.time()
if args.all:
target = os.path.join(root, '../slides-all-book-generated')
elif args.name:
target = os.path.join(root, f'../slides-{args.name}-book-generated')
else:
exit("Either --all or --name must be provided")
if args.target:
target = args.target
target = os.path.abspath(target)
target = os.path.join(target, 'manuscript')
source = os.path.abspath(root)
logging.info(f"target: {target}")
logging.info(f"source: {source}")
if os.path.exists(target):
shutil.rmtree(target)
os.mkdir(target)
book = []
slides_file = os.path.join(source, 'slides.txt')
with open(slides_file, 'r') as fh:
for line in fh:
line = line.rstrip("\n")
if args.all or line == args.name:
book.extend(process_slides(source, target, line))
books_file = os.path.join(source, 'books.txt')
with open(books_file, 'r') as fh:
for line in fh:
line = line.rstrip("\n")
filepath, target_dir = line.split(',')
dir_name, json_file = filepath.split('/')
if args.all or line.startswith(args.name):
book.extend(process_slides(source, target, dir_name, json_file))
with open(os.path.join(target, 'Book.txt'), 'w') as fh:
for line in book:
fh.write(line + "\n")
end = time.time()
logging.info("Done. Elapsed time: {}".format(end-start))
main()