-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffify.py
More file actions
175 lines (130 loc) · 4.97 KB
/
diffify.py
File metadata and controls
175 lines (130 loc) · 4.97 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
import subprocess
from subprocess import PIPE
import os
import time
def isComment(line):
if len(line.strip())>0:
return line.strip()[0]=='%'
else:
return False
def flattenFile(filein, fileout, no_comments):
for line in filein:
if ('\input' in line and not isComment(line)) :
sub_name = line.strip()
sub_name = sub_name[7:-1]
fsub = open(sub_name+'.tex', 'r')
flattenFile(fsub, fileout,no_comments)
fsub.close()
print('Pulling from '+sub_name+'.tex')
elif ((not no_comments) or (not isComment(line))):
fileout.write(line)
def countdown(n):
for x in range(n):
print(n-x)
time.sleep(1)
def flatten(fin_name, fout_name, commit='master',no_comments = False):
if commit != 'master':
myCommand(['git', 'checkout', commit])
fin = open(fin_name, 'r')
fout = open(fout_name, 'w')
print('Flattening '+fin_name+' into '+fout_name)
flattenFile(fin, fout,no_comments)
fin.close()
fout.close()
if commit != 'master':
myCommand(['git', 'checkout', 'master'])
def myCommand(command, stdout=None, stderr=None, shell=False):
# Excexute a terminal command
print(command)
output = subprocess.Popen(command, stdout=stdout, stderr=stderr, shell=shell)
stdout, stderr = output.communicate()
print(stdout)
print(stderr)
def cleanup(fileList):
# Delete temp files
for file in fileList:
try:
os.remove(file)
print('Removing '+file)
except:
print(file+' not found')
def pdflatex(filename):
# compile TeX files into PDF
try:
myCommand('pdflatex '+filename, stdout=None, stderr=None, shell=True)
myCommand('bibtex '+filename[:-4], stdout=None, stderr=None, shell=True)
myCommand('pdflatex '+filename, stdout=None, stderr=None, shell=True)
myCommand('pdflatex '+filename, stdout=None, stderr=None, shell=True)
latexAuxList = ['.aux', '.toc', '.log', '.syntex.gz', '.bbl', '.blg', 'Notes.bib']
for ext in latexAuxList:
cleanup([filename[:-4]+ext])
except:
print('Something went wrong. Pdflatex failed!')
def pathSplit(s):
# Split path into path and file name
slashPos = [pos for pos, char in enumerate(s) if char == '/']
# print(slashPos)
if slashPos:
return s[0:slashPos[-1]+1],s[slashPos[-1]+1:]
else:
return '', s
# print(s[0:slashPos[-1]+1])
# print(s[slashPos[-1]+1:])
def change_markup_type(file_name, style_add='{\\protect\\color{blue} #1}', style_del=''):
fin = open(file_name, 'r')
fout = open(file_name+'.txt', 'w')
for line in fin:
if '\\providecommand{\\DIFdel}[1]' in line:
fout.write('\\providecommand{\\DIFdel}[1]{'+style_del+'} %DIF PREAMBLE \n')
elif '\\providecommand{\\DIFadd}[1]' in line:
fout.write('\\providecommand{\\DIFadd}[1]{'+style_add+'} %DIF PREAMBLE \n')
else:
fout.write(line)
fin.close()
fout.close()
# Remove the old file and rename the new file to .tex
import os
os.remove(file_name)
os.rename(file_name+'.txt', file_name)
return True
def diffify(
main_path,
old_ver,
new_ver ='master',
make_pdf =True,
clean_up =True,
style_add ='{\\protect\\color{blue} #1}',
style_del ='{\\protect\\color{red} \\sout{#1}}',
no_comments = False):
## move to the project folder
dir_name, fin_name = pathSplit(main_path)
try:
dir_path = os.path.dirname(os.path.realpath(__file__))+'/'
# dir_path = ''
except:
dir_path = ''
print(dir_path)
os.chdir(dir_name)
## name old version, current version and diff files
import random
random_tail = '-'+str(random.randint(1,1001)) ## Add a random tail to the file name to avoid having the same file name as something already in the repo
flattenedOldFileName = fin_name[:-4]+'-flat-'+old_ver[:6]+random_tail+'.tex'
flattenedNewFileName = fin_name[:-4]+'-flat-'+new_ver[:6]+random_tail+'.tex'
diffFileName = fin_name[:-4]+'-diff-'+old_ver[:6]+'-vs-'+new_ver[:6]+'.tex'
### flatten the old version
flatten(fin_name, flattenedOldFileName, commit=old_ver, no_comments = no_comments)
### flatten the current version
flatten(fin_name, flattenedNewFileName, commit=new_ver, no_comments = no_comments)
### Make a diff file
command = 'latexdiff '+flattenedOldFileName+' '+flattenedNewFileName+' > '+diffFileName
myCommand(command, stdout=PIPE, stderr=PIPE, shell=True)
### Change markup styles
change_markup_type(diffFileName, style_add, style_del)
### Compile the diff file is make_pdf = True
if make_pdf:
pdflatex(diffFileName)
### Clean up if clean_up = True
if clean_up:
cleanup([flattenedOldFileName, flattenedNewFileName, diffFileName])
print('Finished!')