10 with open(file_path,
'a'):
11 os.utime(file_path,
None)
14 return os.path.abspath(os.path.expanduser(path))
16 def Texify(input_dirs, output_dir, tag, keep_logs = False):
17 orig_dir = os.getcwd()
19 in_dirs = frozenset([
FullPath(d)
for d
in input_dirs
if os.path.exists(d)])
21 for raw_in_dir
in in_dirs:
24 if output_dir ==
None:
28 if not os.path.exists(out_dir):
30 tmp_dir =
FullPath(tempfile.mkdtemp(prefix=
"tmp_texify_", dir=out_dir))
32 files = [f
for f
in os.listdir(in_dir)
if f.endswith(
".tex")
and (tag==
None or tag
in f)]
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)
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)
45 elif os.path.exists(out_log)
and os.path.getmtime(in_tex) < os.path.getmtime(out_log):
46 print(
"Ignoring uncompilable "+in_tex)
49 os.symlink(os.path.join(in_dir, f), os.path.join(tmp_dir, f))
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)
59 os.rename(tmp_log, out_log)
60 print(
"Produced "+out_pdf)
62 os.rename(tmp_log, out_log)
63 print(
"Failed to compile "+in_tex)
66 shutil.rmtree(tmp_dir)
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()
81 Texify(args.input, args.output, args.tag, args.keep_logs)
def Texify(input_dirs, output_dir, tag, keep_logs=False)