Add graph to Serway and Jewett v8's 25.27.b solution.
[course.git] / latex / test / write_problemlist.py
index 2e3ef27b9cfff61f11564aa11fc499731ca8eda1..fe3fae24afa2f14dc24708c8c9bd325b2145d856 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import hashlib
 import os
 import os.path
 
@@ -7,7 +8,7 @@ import os.path
 _this_dir = os.path.dirname(__file__)
 
 
-def write_problemlist(problem_dir=None, blacklist=()):
+def write_problemlist(problem_dir=None, blacklist=(), symlink=False):
     # remove evidence from previous runs
     for name in os.listdir(_this_dir):
         if os.path.islink(name):
@@ -15,7 +16,8 @@ def write_problemlist(problem_dir=None, blacklist=()):
 
     # generate new symlinks and problemlist.tex
     f = open(os.path.join(_this_dir, 'problemlist.tex'), 'w')
-    i = 1
+    if symlink:
+        i = 1
     for source in sorted(os.listdir(problem_dir)):
         if source in blacklist:
             continue
@@ -26,14 +28,26 @@ def write_problemlist(problem_dir=None, blacklist=()):
         for problem in sorted(os.listdir(source_dir)):
             path = os.path.join(source_dir, problem)
             if os.path.isfile(path):
+                force_link = False
                 relpath = os.path.relpath(path, _this_dir)
                 if path.endswith('.tex'):
-                    pname = 'problem%d.tex' % i
-                    i += 1
+                    if symlink:
+                        pname = 'problem%d.tex' % i
+                        i += 1
+                    else:
+                        pname = relpath
                     f.write('\\input{%s}\n' % pname)
                 else:  # utility file (e.g. image)
-                    pname = problem
-                os.symlink(relpath, pname)
+                    pname = '.'.join(problem.split('.')[-2:])
+                    if not symlink:
+                        force_link = True
+                if symlink or force_link:
+                    if os.path.islink(pname):
+                        ha = hashlib.sha1(open(relpath, 'r').read()).digest()
+                        hb = hashlib.sha1(open(pname, 'r').read()).digest()
+                        assert ha == hb, (relpath, pname)
+                    else:
+                        os.symlink(relpath, pname)
     f.close()
 
 
@@ -41,14 +55,13 @@ if __name__ == '__main__':
     write_problemlist(
         problem_dir=os.path.join(_this_dir, '..', 'problems'),
         blacklist=[
-            'Giancoli_6',
+#            'Giancoli_6',
             'Serway_and_Jewett_4',
-            'Serway_and_Jewett_4_venkat',
-            'Serway_and_Jewett_4_wking',
+#            'Serway_and_Jewett_4_venkat',
+#            'Serway_and_Jewett_4_wking',
 #            'Serway_and_Jewett_8',
-            'Thornton_and_Rex_3',
-            'Thornton_and_Rex_3_wking',
-            'Young_and_Freedman_12',
-            'wking',
+#            'Thornton_and_Rex_3_wking',
+#            'Young_and_Freedman_12',
+#            'wking',
             ]
         )