ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
texify.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import argparse
4 import os
5 import tempfile
6 import shutil
7 import subprocess
8 
9 def Touch(file_path):
10  with open(file_path, 'a'):
11  os.utime(file_path, None)
12 
13 def FullPath(path):
14  return os.path.abspath(os.path.expanduser(path))
15 
16 def Texify(input_dirs, output_dir, tag, keep_logs = False):
17  orig_dir = os.getcwd()
18 
19  in_dirs = frozenset([FullPath(d) for d in input_dirs if os.path.exists(d)])
20 
21  for raw_in_dir in in_dirs:
22  in_dir = FullPath(raw_in_dir)
23  out_dir = ""
24  if output_dir == None:
25  out_dir = in_dir
26  else:
27  out_dir = FullPath(output_dir)
28  if not os.path.exists(out_dir):
29  os.makedirs(out_dir)
30  tmp_dir = FullPath(tempfile.mkdtemp(prefix="tmp_texify_", dir=out_dir))
31 
32  files = [f for f in os.listdir(in_dir) if f.endswith(".tex") and (tag==None or tag in f)]
33  for f in files:
34  the_pdf = f.replace(".tex",".pdf")
35  the_log = f.replace(".tex",".log")
36  tmp_pdf = os.path.join(tmp_dir, the_pdf)
37  out_pdf = os.path.join(out_dir, the_pdf)
38  tmp_log = os.path.join(tmp_dir, the_log)
39  out_log = os.path.join(out_dir, the_log)
40  in_tex = os.path.join(in_dir, f)
41 
42  if os.path.exists(out_pdf) and os.path.getmtime(in_tex) < os.path.getmtime(out_pdf):
43  print("Kept pre-existing "+out_pdf)
44  continue
45  elif os.path.exists(out_log) and os.path.getmtime(in_tex) < os.path.getmtime(out_log):
46  print("Ignoring uncompilable "+in_tex)
47  continue
48 
49  os.symlink(os.path.join(in_dir, f), os.path.join(tmp_dir, f))
50  os.chdir(tmp_dir)
51  command = ["pdflatex","--shell-escape","--interaction","batchmode",f]
52  null_file = open(os.devnull, 'w')
53  subprocess.call(command, stdout=null_file)
54  if os.path.exists(tmp_pdf):
55  subprocess.call(command, stdout=null_file)
56  subprocess.call(command, stdout=null_file)
57  os.rename(tmp_pdf, out_pdf)
58  if keep_logs:
59  os.rename(tmp_log, out_log)
60  print("Produced "+out_pdf)
61  else:
62  os.rename(tmp_log, out_log)
63  print("Failed to compile "+in_tex)
64 
65  os.chdir(orig_dir)
66  shutil.rmtree(tmp_dir)
67 
68 if __name__ == "__main__":
69  parser = argparse.ArgumentParser(description="Compiles all .tex documents in a set of directories",
70  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
71  parser.add_argument("-o", "--output", default=None, metavar="OUTPUT_DIR",
72  help="Directory in which to put resulting .pdf files. Uses input directories if omitted.")
73  parser.add_argument("-t", "--tag", default=None, metavar="TAG",
74  help = "Only process files containing %(metavar)s in their names")
75  parser.add_argument("-k", "--keep_logs", action='store_true',
76  help = "Keep log file even in case of successful compilation")
77  parser.add_argument("input", nargs="*", default=["tables"], metavar="INPUT_DIR",
78  help="List of directories with .tex files to compile")
79  args = parser.parse_args()
80 
81  Texify(args.input, args.output, args.tag, args.keep_logs)
def FullPath(path)
Definition: texify.py:13
def Texify(input_dirs, output_dir, tag, keep_logs=False)
Definition: texify.py:16
def Touch(file_path)
Definition: texify.py:9