-
Notifications
You must be signed in to change notification settings - Fork 3
/
bundle.py
executable file
·114 lines (105 loc) · 5.14 KB
/
bundle.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
#!/usr/bin/env python3
import os
import tarfile
import argparse
from time import time
from shutil import rmtree, copyfile, move
from pathlib import Path
import bundle.grade_distribution
import bundle.subjects
import bundle.publications_courses
import bundle.publications_subjects
import bundle.generate_sitemap
import bundle.publications_colleges
import bundle.patch.publications_courses
import bundle.patch.groups
import bundle.patch.publications_core
import bundle.patch.ratemyprofessors
import bundle.patch.tccns
from colorama import init
init()
from colorama import Fore, Back, Style
parser = argparse.ArgumentParser(description='Do stuff')
parser.add_argument('-o', dest='tarloc', type=str, required=True, help='Where to generate the tar file')
parser.add_argument('--skiptar', dest='skiptar', action='store_true', help='Should the tar file be compression be skipped')
parser.add_argument('--skipmove', dest='skipmove', action='store_true', help='Should the resulting export files be moved to the \'export\' folder or kept in their described location')
parser.add_argument('--skiprmtree', dest='skiprmtree', action='store_true', help='Should the temporary export file directory be preserved')
parser.add_argument('--testbundle', dest='testbundle', type=str, required=False, default=None, help='A Path pattern to select matching CSV files to generate a test bundle off of')
args = parser.parse_args()
# total tasks
N = 15
M = 1
documents_path = Path(__file__).parent / '..' / 'documents'
exports_path = Path(__file__).parent / '..' / 'exports'
exports_path.mkdir(exist_ok=True)
# create directory where export will be staged
export_name = Path(os.path.splitext(args.tarloc)[0])
export_name.mkdir(exist_ok=True)
# always process this first
print(f'{Fore.CYAN}[{M} / {N}] Bundling edu.uh.grade_distribution{Style.RESET_ALL}')
bundle.grade_distribution.process(documents_path / 'edu.uh.grade_distribution', export_name / 'edu.uh.grade_distribution', csv_path_pattern=args.testbundle)
M += 1
# process the raw data, generate intermediary format
for fmt in documents_path.iterdir():
# print thing
if(fmt.name in ['com.collegescheduler.uh.subjects', 'edu.uh.publications.courses', 'io.cougargrades.groups', 'edu.uh.publications.subjects','edu.uh.publications.core']):
print(f'{Fore.CYAN}[{M} / {N}] Bundling {fmt.name}{Style.RESET_ALL}')
M += 1
# actually do
if(fmt.name == 'com.collegescheduler.uh.subjects'):
bundle.subjects.process(fmt.resolve(), export_name / fmt.name)
if(fmt.name == 'edu.uh.publications.subjects'):
bundle.publications_subjects.process(fmt.resolve(), export_name / fmt.name)
if(fmt.name == 'edu.uh.publications.courses'):
bundle.publications_courses.process(fmt.resolve(), export_name / fmt.name)
if(fmt.name == 'edu.uh.publications.colleges'):
bundle.publications_colleges.process(fmt.resolve(), export_name / fmt.name)
if(fmt.name == 'edu.uh.publications.core'):
(export_name / fmt.name).mkdir(exist_ok=True)
copyfile(fmt / 'core_curriculum.json', export_name / fmt.name / 'core_curriculum.json')
print('\t✔')
if(fmt.name == 'io.cougargrades.groups'):
(export_name / fmt.name).mkdir(exist_ok=True)
copyfile(fmt / 'defaults.json', export_name / fmt.name / 'defaults.json')
print('\t✔')
# generate sitemap.txt
print(f'{Fore.CYAN}[{M} / {N}] Generating io.cougargrades.sitemap{Style.RESET_ALL}')
M += 1
bundle.generate_sitemap.process(export_name / 'io.cougargrades.sitemap')
print('\t✔')
# generate patch files
for fmt in documents_path.iterdir():
# print thing
if(fmt.name in ['com.collegescheduler.uh.subjects', 'edu.uh.publications.courses', 'io.cougargrades.groups', 'edu.uh.publications.core', 'com.ratemyprofessors', 'edu.uh.academics.tccns']):
print(f'{Fore.CYAN}[{M} / {N}] Patching {fmt.name}{Style.RESET_ALL}')
M += 1
else:
continue
# actually do
if(fmt.name == 'edu.uh.publications.courses'):
bundle.patch.publications_courses.generate(export_name / fmt.name, fmt.resolve(), export_name / 'io.cougargrades.publicdata.patchfile')
if(fmt.name == 'io.cougargrades.groups'):
bundle.patch.groups.generate(fmt.resolve(), export_name / 'io.cougargrades.publicdata.patchfile')
if(fmt.name == 'edu.uh.publications.core'):
bundle.patch.publications_core.generate(fmt.resolve(), export_name / 'io.cougargrades.publicdata.patchfile')
if(fmt.name == 'com.ratemyprofessors'):
bundle.patch.ratemyprofessors.generate(fmt.resolve(), export_name / 'io.cougargrades.publicdata.patchfile')
if(fmt.name == 'edu.uh.academics.tccns'):
bundle.patch.tccns.generate(fmt.resolve(), export_name / 'io.cougargrades.publicdata.patchfile')
# generate the export file
print(f'{Fore.CYAN}[{M} / {N}] Compressing tarfile: {export_name}{Style.RESET_ALL}')
if(not args.skiptar):
with tarfile.open(exports_path / args.tarloc, 'w:gz') as tar:
for item in export_name.iterdir():
tar.add(name=item, arcname=item.name)
if(not args.skiprmtree):
rmtree(export_name)
else:
print('\tSkipped rmtree')
else:
print('\tSkipped tar.gz compression')
if(not args.skipmove):
move(export_name, exports_path / export_name.name)
else:
print('\tSkipped export move')
print(f'{Fore.MAGENTA}Done!{Style.RESET_ALL}')