-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.py
More file actions
74 lines (61 loc) · 2.23 KB
/
compile.py
File metadata and controls
74 lines (61 loc) · 2.23 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
from cli import Script
from typing import List
from util import get_config
from pathlib import Path
from os import path, environ
import logging as log
import subprocess
import time
import sys
start_time = time.time()
root = path.join(environ.get('LECTURE_NOTES_HOME'), 'courses')
#s_course = sys.argv[1]
# Fetch LaTeX compiler configuration
latex_config = get_config('latex')
latex_compiler = latex_config['compiler']
compiler_arguments = latex_config['compiler_arguments']
class CompileCourse(Script):
"""
Compiles the notes for course given the name of the directory
where it is located.
If no course is provided, all the courses will be compiled by
default, which may take a while.
"""
name: str = 'compile'
actions: List[str] = ['compile_course']
arguments: List[str] = ['course']
usage: str = f'{name} [course]'
description: str = 'Compiles (a) course(s)'
def action(self, args: List[str]):
if len(args) > 0:
course = args[0]
self.compile_course(course)
else:
print('\nWork in progress\n')
def compile_course(self, course: str):
course = str(course)
course_dir = path.join(root, course)
course_path = Path(course_dir).expanduser()
master_path = course_path / 'master.tex'
if not course_path.is_dir():
log.err(f'\nCourse {course} does not exists :(\n')
return False
if not master_path.is_file():
log.err(f'\nCourse {course} does not have a master.tex file!\n')
return False
print()
print(f'Compiling {course} course notes...')
cmd = [latex_compiler]
args = compiler_arguments.split(' ')
for arg in args:
cmd.append(arg)
cmd.append(master_path)
result = subprocess.run(cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd=course_path
)
if not result:
log.err(f'A fatal error ocurred during compilation. Check courses/{course}/master.log, solve the errors and try again.\n')
else:
log.ok(f'{course} course notes compiled successfully in {time.time() - start_time:.2f} seconds!\n')