-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefresh.py
More file actions
executable file
·114 lines (94 loc) · 3.34 KB
/
refresh.py
File metadata and controls
executable file
·114 lines (94 loc) · 3.34 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
import os
import subprocess
import getopt
import shutil
import re
import time
from datetime import datetime
LATEX_LOG = "refresh.out"
EXEC_LOG = "refresh.log"
MASTER = ""
def make_serial(root):
return '-' + str(time.time())
def make_environ(latex_dir):
env = os.environ
path = env['PATH']
path += ':' + latex_dir
env['PATH'] = path
return env
def do_call(myargs, myenv, mylog):
mylog.write("Calling '" + reduce(lambda x, y : x + ' ' + y, myargs) + "'...\n")
mylog.flush()
p = subprocess.Popen(myargs, env=myenv, stdout=mylog, stderr=mylog)
p.wait()
if p.returncode == 0:
mylog.write("OK.\n")
return True
else:
mylog.write("Error calling " + myargs[0] + "\n")
return False
def call_pdflatex(env, log, serial):
return do_call(
['pdflatex', '-halt-on-error', 'dissertation' + serial],
env, log)
def call_bibtex(env, log, serial):
return do_call(['bibtex', 'dissertation' + serial],
env, log)
def call_cvs(root):
os.chdir(root)
p = subprocess.Popen(['cvs', 'up'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()
return p.stdout.read()
def call_git(root):
os.chdir(root)
p = subprocess.Popen(['git', 'pull', 'origin'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()
return p.stdout.read()
def refresh(env, root, serial):
with open(root + '/' + LATEX_LOG, 'wa') as log:
log.write("Refreshing document...\n")
log.flush()
if call_pdflatex(env, log, serial):
if call_bibtex(env, log, serial):
if call_pdflatex(env, log, serial):
return call_pdflatex(env, log, serial)
print "Error compiling document!"
return False
def setup(root, serial):
shutil.copyfile(root + '/dissertation.tex',
root + '/dissertation' + serial + '.tex')
shutil.copyfile(root + '/dissertation.bib',
root + '/dissertation' + serial + '.bib')
def swing_link(root, serial):
os.rename(root + '/dissertation' + serial + '.pdf',
root + '/dissertation' + MASTER + '.pdf')
def cleanup(root, serial):
for f in os.listdir(root):
head, tail = os.path.split(f)
if re.match("^dissertation" + serial, tail):
os.remove(f)
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:],'d:l:')
root = latex_dir = ''
for (k,v) in opts:
if (k == '-d'):
root = v
elif (k == '-l'):
latex_dir = v
if (root == '') | (latex_dir == ''):
print "Usage: refresh.py -d <root_dir> -l <latex_bin_dir>"
with open(root + '/' + EXEC_LOG, 'a') as log:
log.write(str(datetime.now()) + ': Starting execution...\n')
env = make_environ(latex_dir)
cvs_output = call_git(root).split('\n')
for word in cvs_output:
if ('.tex' in word) | ('.bib' in word):
serial = make_serial(root)
setup(root, serial)
log.write(str(datetime.now()) + ': Refreshing document...\n')
if refresh(env, root, serial):
swing_link(root, serial)
cleanup(root, serial)
break
log.write(str(datetime.now()) + ': Finished.\n')