Clean up latex test harness and build all but Serway and Jewett v4.
[course.git] / latex / test / write_problemlist.py
1 #!/usr/bin/env python
2
3 import hashlib
4 import os
5 import os.path
6
7
8 _this_dir = os.path.dirname(__file__)
9
10
11 def write_problemlist(problem_dir=None, blacklist=(), symlink=False):
12     # remove evidence from previous runs
13     for name in os.listdir(_this_dir):
14         if os.path.islink(name):
15             os.unlink(name)
16
17     # generate new symlinks and problemlist.tex
18     f = open(os.path.join(_this_dir, 'problemlist.tex'), 'w')
19     if symlink:
20         i = 1
21     for source in sorted(os.listdir(problem_dir)):
22         if source in blacklist:
23             continue
24         source_dir = os.path.join(problem_dir, source)
25         if not os.path.isdir(source_dir):
26             continue
27         f.write('\\section{%s}\n' % source.replace('_', ' '))
28         for problem in sorted(os.listdir(source_dir)):
29             path = os.path.join(source_dir, problem)
30             if os.path.isfile(path):
31                 force_link = False
32                 relpath = os.path.relpath(path, _this_dir)
33                 if path.endswith('.tex'):
34                     if symlink:
35                         pname = 'problem%d.tex' % i
36                         i += 1
37                     else:
38                         pname = relpath
39                     f.write('\\input{%s}\n' % pname)
40                 else:  # utility file (e.g. image)
41                     pname = '.'.join(problem.split('.')[-2:])
42                     if not symlink:
43                         force_link = True
44                 if symlink or force_link:
45                     if os.path.islink(pname):
46                         ha = hashlib.sha1(open(relpath, 'r').read()).digest()
47                         hb = hashlib.sha1(open(pname, 'r').read()).digest()
48                         assert ha == hb, (relpath, pname)
49                     else:
50                         os.symlink(relpath, pname)
51     f.close()
52
53
54 if __name__ == '__main__':
55     write_problemlist(
56         problem_dir=os.path.join(_this_dir, '..', 'problems'),
57         blacklist=[
58 #            'Giancoli_6',
59             'Serway_and_Jewett_4',
60 #            'Serway_and_Jewett_4_venkat',
61 #            'Serway_and_Jewett_4_wking',
62 #            'Serway_and_Jewett_8',
63 #            'Thornton_and_Rex_3_wking',
64 #            'Young_and_Freedman_12',
65 #            'wking',
66             ]
67         )