-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwikisync.py
More file actions
142 lines (120 loc) · 4.92 KB
/
wikisync.py
File metadata and controls
142 lines (120 loc) · 4.92 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
from igem_wikisync import wikisync as sync
from igem_wikisync.logger import logger
import os
import sys
from hashlib import md5
def upload_html(html_files, browser, config, upload_map):
for path in html_files.keys():
file_object = html_files[path]
path_str = str(file_object.path)
ext = file_object.extension
# open file
try:
with open(file_object.src_path, 'r', encoding='utf-8') as file:
contents = file.read()
except Exception:
message = f'Could not open/read {file_object.path}. Skipping.'
print(message)
# logger.error(message)
continue # FIXME Can this be improved?
preprocess = '' + contents + ''
preprocess = preprocess.replace('{', '<!-- ugly point -->{<!-- /ugly point -->')
preprocess = preprocess.replace('}', '<!-- ugly point -->}<!-- /ugly point -->')
# parse and modify contents
processed = sync.HTMLparser(
config, file_object.path, preprocess, upload_map)
processed = processed.replace('<!-- ugly point -->{<!-- /ugly point -->', '{')
processed = processed.replace('<!-- ugly point -->}<!-- /ugly point -->', '}')
processed = processed.replace('{{', '</html>{{').replace('}}', '}}<html>')
# calculate and store md5 hash of the modified contents
build_hash = md5(processed.encode('utf-8')).hexdigest()
if upload_map[ext][path_str]['md5'] == build_hash:
message = f'Contents of {file_object.path} have been uploaded previously. Skipping.'
print(message)
logger.info(message)
else:
upload_map[ext][path_str]['md5'] = build_hash
build_path = file_object.build_path
try:
# create directory if doesn't exist
if not os.path.isdir(build_path.parent):
os.makedirs(build_path.parent)
# and write the processed contents
with open(build_path, 'w', encoding='utf-8') as file:
file.write(processed)
except Exception:
message = f"Couldn not write {str(file_object.build_path)}. Skipping."
print(message)
logger.error(message)
continue
# FIXME Can this be improved?
# upload
successful = sync.iGEM_upload_page(browser, processed, file_object.upload_URL)
if not successful:
message = f'Could not upload {str(file_object.path)}. Skipping.'
print(message)
logger.error(message)
continue
# FIXME Can this be improved?
else:
pass
# counter[ext] += 1
config = {
'team': 'UTokyo',
'src_dir': 'public/',
'build_dir': 'build/',
'year': '2021',
'silence_warnings': False,
'poster_mode': False
}
build_dir = config['build_dir']
# * 2. Load or create upload_map
upload_map = sync.get_upload_map()
# * 3. Create build directory
if not os.path.isdir(build_dir):
os.mkdir(build_dir)
# ? error handling here?
# * 4. Get iGEM credentials from environment variables
credentials = {
'username': os.environ.get('IGEM_USERNAME'),
'password': os.environ.get('IGEM_PASSWORD')
}
# * 5. Load/create cookie file
browser, cookiejar = sync.get_browser_with_cookies()
# * 6. Login to iGEM
login = sync.iGEM_login(browser, credentials, config)
if not login:
message = 'Failed to login.'
# logger.critical(message)
sys.exit(2)
# # * 7. Save cookies
# # TODO: check if this works, might not
# cookiejar.save()
# * 8. Cache files
files = sync.cache_files(upload_map, config)
# * 9. Upload all assets and create a map
uploaded_assets = sync.upload_and_write_assets(files['other'], browser, upload_map, config)
for path in files['html'].keys():
html_file = files['html'][path]
if html_file._upload_path.startswith('/template/'):
upload_map['html'][str(path)]['link_URL'] = f'''https://{config['year']}.igem.org/Template:{config['team']}{html_file._upload_path}'''
files['html'][path]._upload_URL = f'''https://{config['year']}.igem.org/wiki/index.php?title=Template:{config['team']}{html_file._upload_path}&action=edit'''
# * 10. write upload map just in case
# things go wrong while dealing with code
sync.write_upload_map(upload_map)
# * 11. Build files and upload changed files
# UTokyo modification: only dealing with css and js files
uploaded_code = sync.build_and_upload({
'html': {},
'css': files['css'],
'js': files['js']
}, browser, config, upload_map)
uploaded_code_html = upload_html(files['html'], browser, config, upload_map)
# * 12. Write final upload map
sync.write_upload_map(upload_map)
sync.print_summary(uploaded_assets, uploaded_code)
# sync.run(
# team='UTokyo',
# src_dir='public', # folder where your wiki is stored
# build_dir='build' # folder where WikiSync will temporarily store your wiki before uploading
# )