-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure_presentation.py
341 lines (292 loc) · 10.4 KB
/
configure_presentation.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
''' Presentation Configuration
'''
import datetime as dt
import json
import logging
import os
import re
import shlex
import subprocess
import time
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Set
import pytz
import schema
import yaml
from git import Repo
config_schema = schema.Schema(
{
'projects': {
str: {
'name': str,
'latex': str,
'branch': str,
'assignees': [str],
}
},
'schedule': {
str: [str]
}
}
)
def main():
"""Main Presentation Configuration
"""
# Configure loggers
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
date_fmt = '%Y-%m-%dT%H:%M:%S'
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
error_formatter = logging.Formatter('%(asctime)s.%(msecs)03d - %(name)s - %(levelname)s'
' - %(message)s', datefmt=date_fmt)
console_handler.setFormatter(error_formatter)
root_logger.addHandler(console_handler)
logging.Formatter.converter = time.gmtime
# Set timezone
timezone = pytz.timezone('America/Los_Angeles')
exec_timestamp = dt.datetime.now(timezone)
# Read schedule from schedule.yml
with open('config.yml', 'r', encoding='utf-8') as handle:
data = yaml.safe_load(handle)
config = config_schema.validate(data)
root_logger.info('Using config %s', json.dumps(config))
# allow assert - we want the action to fail if the current week is not supported
raw_schedule: Dict[str, List[str]] = config['schedule']
full_schedule = {dt.datetime.fromisoformat(
key): value for key, value in raw_schedule.items()}
future_schedule = {date: sequence
for date, sequence in full_schedule.items()
if date > exec_timestamp}
root_logger.info('Configuring git user')
_exec_cmd(
['git', 'config', 'user.email', '[email protected]']
)
_exec_cmd(
['git', 'config', 'user.name', 'E4E GitHub Actions']
)
root_logger.info('Setting next execute date')
if len(future_schedule) <= 0:
_set_next_execute_date(None)
return
next_date = min(future_schedule.keys())
_set_next_execute_date(next_date)
current_projects: List[str] = future_schedule[next_date]
root_logger.info('Current Projects: %s', str(current_projects))
projects: Dict = config['projects']
all_call_projects: Set[str] = set(
projects.keys()).difference(current_projects)
root_logger.info('All Call Projects: %s', str(all_call_projects))
__clear_announcements(next_date)
__update_latex(current_projects, projects, all_call_projects)
# Create the appropriate branches
__create_branches(current_projects, projects, next_date.date())
def _set_next_execute_date(presentation_date: Optional[dt.date]):
logger = logging.getLogger('_set_next_execute_date')
if presentation_date:
logger.info('Setting next execute date %s',
presentation_date.isoformat())
else:
logger.info('Disabling cron')
latex_build_file = Path('.github/workflows/latex_build.yml')
create_branch_file = Path('.github/workflows/create_project_branches.yml')
if not presentation_date:
build_time = None
next_project_create_time = None
commit_msg = 'ci: Disables next execution'
else:
build_time = presentation_date - dt.timedelta(minutes=30)
next_project_create_time = presentation_date + dt.timedelta(hours=12)
commit_msg = f'ci: updates next execution for {presentation_date.isoformat()}'
_set_cron_string(latex_build_file, build_time)
_set_cron_string(create_branch_file, next_project_create_time)
repo = Repo('.')
if len(repo.index.diff(None)) == 0:
return
_exec_cmd(
['git', 'add', latex_build_file.as_posix()]
)
_exec_cmd(
['git', 'add', create_branch_file.as_posix()]
)
_exec_cmd(
['git', 'commit', '-m', commit_msg]
)
def _set_cron_string(file_to_modify, execute_time: dt.datetime):
if not execute_time:
new_cron_string = '59 23 29 4 6'
desc = 'This is basically never'
else:
utc_dt: dt.date = execute_time.astimezone(pytz.utc)
new_cron_string = utc_dt.strftime('%M %H %d %m *')
desc = f'Executes at {execute_time.isoformat()}'
with open(file_to_modify, 'r', encoding='utf-8') as handle:
file_contents = ''.join(handle.readlines())
regex = r"- cron: '(.*)' # (.*)"
subst = f"- cron: '{new_cron_string}' # {desc}"
result = re.sub(regex, subst, file_contents, 1)
with open(file_to_modify, 'w', encoding='utf-8') as handle:
handle.write(result)
def __clear_announcements(presentation_date: dt.date):
logger = logging.getLogger('Clear Announcements')
logger.info('Clearning annoumcements')
with open('announcements.tex', 'w', encoding='utf-8') as handle:
handle.write(
f'% Announcements for {presentation_date.isoformat()}\n'
'% \\begin{frame}{Announcements}\n'
'% \\begin{itemize}\n'
'% \\item\n'
'% \\end{itemize}\n'
'% \\end{frame}\n'
)
repo = Repo('.')
if len(repo.index.diff(None)) == 0:
return
_exec_cmd(
['git', 'add', 'announcements.tex']
)
_exec_cmd(
['git', 'commit', '-m', 'fix: Clears announcements']
)
def __update_latex(current_projects: List[str], projects: Dict, all_call_projects: Set[str]):
logger = logging.getLogger('Update Latex')
logger.info('Updating LaTex')
logger.info('Setting All Call Sequence')
with open('active_all_call.tex', 'w', encoding='utf-8') as handle:
if len(all_call_projects) == 0:
handle.write(f'\\item None\n')
for project in all_call_projects:
handle.write(f'\\item {projects[project]["name"]}\n')
logger.info('Setting Active Sequence')
with open('active_order.tex', 'w', encoding='utf-8') as handle:
for project in current_projects:
handle.write(f'\\item {projects[project]["name"]}\n')
logger.info('Setting Active Sections')
with open('active_sections.tex', 'w', encoding='utf-8') as handle:
for project in current_projects:
handle.write(f'\\section{{{projects[project]["name"]}}}\n')
handle.write(f'\\include{{{projects[project]["latex"]}}}\n')
_exec_cmd(
[
'git', 'add',
'active_all_call.tex',
'active_order.tex',
'active_sections.tex'
]
)
repo = Repo('.')
if len(repo.index.diff(None)) == 0:
return
_exec_cmd([
'git', 'commit',
'-m', f'feat!: Configures presentation for {", ".join(current_projects)}'
])
_exec_cmd([
'git', 'push'
])
_exec_cmd([
'git', 'pull'
])
def __create_branches(current_projects: List[str],
projects: Dict,
presentation_date: dt.date):
logger = logging.getLogger('Create Branches')
logger.info('Creating Branches')
logger.info('Deleting Images')
path_to_rm: List[Path] = []
for img in Path('images').rglob('*'):
if not img.is_file():
continue
if img.name == 'README.md':
continue
path_to_rm.append(img)
for path in path_to_rm:
_exec_cmd(
['git', 'rm', path.as_posix()]
)
logger.info('Writing Project Slides')
for project in current_projects:
project_params = projects[project]
# Reset the slides
with open('base_project.tex', 'r', encoding='utf-8') as reference_handle, \
open(project_params['latex'] + '.tex', 'w', encoding='utf-8') as target_handle:
for line in reference_handle:
target_handle.write(line)
_exec_cmd(
['git', 'add', project_params['latex'] + '.tex']
)
repo = Repo('.')
if len(repo.index.diff(repo.head.commit)) == 0:
return
_exec_cmd(
['git', 'commit', '-m', 'fix: Resetting slides']
)
_exec_cmd(
['git', 'push']
)
_exec_cmd(
['git', 'pull']
)
logger.info('Creating branches and PRs')
for project in current_projects:
project_params = projects[project]
branch_name = f'{project_params["branch"]}_{presentation_date.isoformat()}'
_exec_cmd(
['git', 'checkout', 'main']
)
_exec_cmd(
['git', 'checkout', '-b', branch_name]
)
# Reset the slides
with open('base_project.tex', 'r', encoding='utf-8') as reference_handle, \
open(project_params['latex'] + '.tex', 'w', encoding='utf-8') as target_handle:
target_handle.write(
f'% Slides for {presentation_date.isoformat()}\n')
for line in reference_handle:
target_handle.write(line)
_exec_cmd(
['git', 'add', project_params['latex'] + '.tex']
)
if len(repo.index.diff(repo.head.commit)) == 0:
return
_exec_cmd(
['git', 'commit', '-m', 'fix: Adds date']
)
_exec_cmd(
['git', 'push', '--set-upstream', 'origin', branch_name]
)
_exec_cmd([
'git', 'push'
])
command = [
'gh', 'pr', 'create',
'-B', 'main',
'-t', project_params['name'],
'-d',
'-H', branch_name,
'-b', (f'Weekly presentation slides for {project_params["name"]} for '
f'{presentation_date.isoformat()}')
]
for assignee in project_params['assignees']:
command.extend(('-a', assignee))
_exec_cmd(
command
)
def _exec_cmd(cmd: Sequence[str]):
# only exec if GH_TOKEN is defined, i.e. in GitHub Actions
logger = logging.getLogger('_exec_cmd')
logger.info('Exec `%s`', shlex.join(cmd))
if 'GH_TOKEN' in os.environ:
try:
subprocess.check_call(
args=cmd,
env=os.environ.copy()
)
except subprocess.CalledProcessError as exc:
logger.exception(exc)
logger.critical(exc.stdout)
logger.critical(exc.stderr)
logger.info(os.environ['GH_TOKEN'][-4:])
raise exc
if __name__ == '__main__':
main()