-
Notifications
You must be signed in to change notification settings - Fork 1
/
tex_submission_purify.py
executable file
·470 lines (351 loc) · 14.2 KB
/
tex_submission_purify.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python3
from pathlib import Path
from collections import deque, namedtuple, Counter
from enum import Enum
from shutil import rmtree, copy as copy_file
from TexSoup import TexSoup
from TexSoup.data import Token, TexNode, TexCmd, TexExpr, TexText
import click
class NodeAction(Enum):
Continue = 0
StopDescent = 3
LATEX_COMPILATION_FILES_TO_IGNORE = ['.aux', '.blg', '.log', '.synctex.gz', '.pdf']
LATEX_COMPILATION_FILES_TO_KEEP = ['.bbl', '.brf']
def tex_expr_replace(expr, old, new):
if isinstance(expr, TexCmd):
"""
TexCmd stores children in `args` and throws exceptions if remove+insert is called directly on the expr itself
"""
for idx, val in enumerate(expr.args):
print(idx, val)
if val == old:
expr.args[idx] = new
return True
else:
idx = expr.remove(old)
expr.insert(idx, new)
def tex_expr_remove(expr, old):
expr.remove(old)
class TexSubmissionCleaner:
FileQueueEntry = namedtuple('FileQueueEntry', ['path', 'type'])
FILE_TYPE_TEX = 'tex'
FILE_TYPE_GRAPHICS = 'graphics'
FILE_TYPE_OPAQUE = 'copy'
FILE_TYPE_IGNORE = 'ignore'
def __init__(self, root_doc_path, out_dir):
# File queues
self.files_to_process = deque()
self.files_aware_of = set()
# Processors
self.setup_file_processors()
self.setup_node_processors()
self.stats = Counter()
self.keep_empty_comments = True
self.out_root_doc_name = 'ms'
# Paths
self.root_doc_path = Path(root_doc_path).resolve()
self.root_dir = self.root_doc_path.parent
self.out_dir = Path(out_dir).resolve()
if self.out_dir == self.root_dir:
raise ValueError('out_dir must be different from root dir')
if self.out_dir in self.root_dir.parents:
raise ValueError(f'Root dir must not be inside out dir (root_dir={self.root_dir}, out_dir={self.out_dir}')
self.all_files = set(self.root_dir.glob('**/*'))
def setup_file_processors(self):
self.file_processors = {
self.FILE_TYPE_TEX: self.process_file_tex,
self.FILE_TYPE_GRAPHICS: self.process_file_graphics,
self.FILE_TYPE_OPAQUE: self.process_file_copy,
self.FILE_TYPE_IGNORE: (lambda path: None),
}
def register_node_processor(self, name, func):
self.node_processors.setdefault(name, []).append(func)
def setup_node_processors(self):
self.token_node_processors = [
self.process_text_remove_comment,
]
self.node_processors = {}
# we will use the names to remove their declarations too
self.removed_command_names = set()
self.register_node_processor('input', self.node_input)
self.register_node_processor('newcommand', self.node_newcommand)
self.register_node_processor('usepackage', self.node_usepackage)
for image_cmd in ['includegraphics', 'overpic']:
self.register_node_processor(image_cmd, self.node_includegraphics)
self.commands_to_remove('comment')
def commands_to_remove(self, *commands):
if commands.__len__() == 1 and isinstance(commands[0], (list, tuple)):
commands = commands[0]
for cmd in commands:
self.removed_command_names.add(cmd)
self.register_node_processor(cmd, self.node_cmd_remove)
def commands_to_short_circuit(self, *commands):
if commands.__len__() == 1 and isinstance(commands[0], (list, tuple)):
commands = commands[0]
for cmd in commands:
self.removed_command_names.add(cmd)
self.register_node_processor(cmd, self.node_cmd_shortcircuit)
def additional_files_to_keep(self, *to_keep):
if to_keep.__len__() == 1 and isinstance(to_keep[0], (list, tuple)):
to_keep = to_keep[0]
for path in to_keep:
path = Path(path)
path_if_relative = self.root_dir / path
if path_if_relative.is_file():
self.add_file_to_process(path_if_relative, self.FILE_TYPE_OPAQUE)
elif path.is_file():
self.add_file_to_process(path, self.FILE_TYPE_OPAQUE)
else:
raise FileNotFoundError(f'File {path_if_relative} requested by --keep-file does not exist')
def clear_out_dir(self):
if self.out_dir.exists():
rmtree(self.out_dir)
def add_file_to_process(self, path, type):
path = Path(path)
if path not in self.files_aware_of:
self.files_aware_of.add(path)
self.files_to_process.append(self.FileQueueEntry(path, type))
def run(self):
# process files recursively starting from root .tex
self.add_file_to_process(self.root_doc_path, self.FILE_TYPE_TEX)
while self.files_to_process:
queue_entry = self.files_to_process.popleft()
self.process_file(queue_entry.path, queue_entry.type)
# rename top level .tex to ms.tex
root_doc_name = self.root_doc_path.name
for ext in ['.tex'] + LATEX_COMPILATION_FILES_TO_KEEP:
rename_from = (self.out_dir / root_doc_name).with_suffix(ext)
rename_to = (self.out_dir / self.out_root_doc_name).with_suffix(ext)
if rename_from.is_file() and not rename_to.exists():
rename_from.rename(rename_to)
def print_statistics(self):
print('\n=== Statistics ===\n ' + '\n '.join(f'{sk}: {sv}' for sk, sv in sorted(self.stats.items())))
def get_unused_files(self):
# all_files set in constructor
unused_files = list(self.all_files.difference(self.files_aware_of))
unused_files.sort()
unused_files = [str(f.relative_to(self.root_dir)) for f in unused_files if f.is_file()]
# remove paths starting with ., like .git or .svn
unused_files = [f for f in unused_files if not f.startswith('.')]
return unused_files
def notify_about_unused_files(self):
print('\n=== Unused files ===\n ' + '\n '.join(self.get_unused_files()))
def out_path(self, for_path):
return self.out_dir / for_path.relative_to(self.root_dir)
def process_file(self, path, type):
proc = self.file_processors.get(type, None)
if proc:
proc(path)
else:
print(f'No processor for type {type} given to file {path}')
def process_file_copy(self, path):
out_file_path = self.out_path(path)
out_file_path.parent.mkdir(parents=True, exist_ok=True)
copy_file(path, out_file_path)
def process_file_graphics(self, path):
out_file_path = self.out_path(path)
out_file_path.parent.mkdir(parents=True, exist_ok=True)
copy_file(path, out_file_path)
def process_file_tex(self, path):
self.current_doc_path = path
self.current_doc_path_relative = self.current_doc_path.relative_to(self.root_dir)
print('TEX', self.current_doc_path_relative)
with path.open('r') as f_in:
try:
doc = TexSoup(f_in)
except EOFError as e:
print(f'File {path}: {e}')
# go through the parse tree
self.process_tex_expr(doc.expr, parent_node = None, doc=doc)
# write processed tex
out_file_path = self.out_path(path)
out_file_path.parent.mkdir(parents=True, exist_ok=True)
out_file_path.write_text(str(doc))
# ignore trash files resulting from latex compilation
for trash_ext in LATEX_COMPILATION_FILES_TO_IGNORE:
trash_file_path = path.with_suffix(trash_ext)
if trash_file_path.is_file():
self.add_file_to_process(trash_file_path, self.FILE_TYPE_IGNORE)
# copy the files from compilation needed by arxiv
for result_ext in LATEX_COMPILATION_FILES_TO_KEEP:
result_file_path = path.with_suffix(result_ext)
if result_file_path.is_file():
self.add_file_to_process(result_file_path, self.FILE_TYPE_OPAQUE)
def apply_processors_to_text(self, text, parent_node, doc):
action = NodeAction.Continue
for f in self.token_node_processors:
action = f(text, parent_node=parent_node, doc=doc, cleaner=self)
if action == NodeAction.StopDescent:
return action
return action
def apply_processors_to_tex_expr(self, tex_node, parent_node, doc):
action = NodeAction.Continue
for f in self.node_processors.get(tex_node.name, []):
action = f(tex_node, parent_node=parent_node, doc=doc, cleaner=self)
if action == NodeAction.StopDescent:
return action
return action
def process_tex_expr(self, tex_expr, parent_node=None, doc=None):
"""
tex_expr can be a piece of text (TexText or Token)
or a TexExpr (command or environment)
"""
if isinstance(tex_expr, (TexText, Token)):
self.apply_processors_to_text(tex_expr, parent_node=parent_node, doc=doc)
# everything else
elif isinstance(tex_expr, TexExpr):
if hasattr(tex_expr, 'name'):
action = self.apply_processors_to_tex_expr(tex_expr, parent_node, doc)
else:
action = NodeAction.Continue
if action != NodeAction.StopDescent:
sub_nodes = []
if isinstance(tex_expr, TexCmd):
sub_nodes = tex_expr.args
else:
sub_nodes = tex_expr.all
for sub_node in sub_nodes:
self.process_tex_expr(sub_node, parent_node=tex_expr, doc=doc)
else:
raise NotImplementedError(f'Expr class {type(tex_expr)} for {tex_expr}')
####################################################################################
# Node processing functions
####################################################################################
@staticmethod
def process_text_remove_comment(text, parent_node, cleaner, **_):
if text.startswith('%'):
cleaner.stats['num_inline_comments'] += 1
if cleaner.keep_empty_comments:
tex_expr_replace(parent_node, text, '%')
else:
tex_expr_remove(parent_node, text)
return NodeAction.StopDescent
@staticmethod
def node_cmd_remove(node, parent_node, cleaner, **_):
""" This node and its children are not written to the output file """
tex_expr_remove(parent_node, node)
# node.delete()
cleaner.stats['num_cmds_removed_' + node.name] += 1
return NodeAction.StopDescent
@staticmethod
def node_cmd_shortcircuit(node, parent_node, cleaner, **_):
""" This is replaced with its children """
num_args = node.args.__len__()
if num_args == 0:
tex_expr_remove(parent_node, node)
elif num_args == 1:
tex_expr_replace(parent_node, node, node.args[0])
else:
raise ValueError(f'Short-circuit cmd has {num_args} args: {node} in {parent_node}')
return NodeAction.Continue
@staticmethod
def node_newcommand(node, parent_node, cleaner, **_):
command_being_declared = str(node.args[0]).lstrip('{\\').rstrip('}')
if command_being_declared in cleaner.removed_command_names:
tex_expr_remove(parent_node, node)
cleaner.stats['num_declarations_removed'] += 1
return NodeAction.StopDescent
@staticmethod
def node_includegraphics(node, cleaner, **_):
for arg in node.args:
if arg.begin == '{':
value = ''.join(list(arg.args) + list(arg.contents))
graphics_path = cleaner.root_dir / value
if graphics_path.is_file():
cleaner.add_file_to_process(graphics_path, cleaner.FILE_TYPE_GRAPHICS)
else:
print(f'Failed to find graphic {value} included from {cleaner.current_doc_path_relative}')
return NodeAction.StopDescent
@staticmethod
def find_included_document_path(root_dir, input_link):
no_ext = root_dir / str(input_link)
if no_ext.is_file():
return no_ext
with_ext = no_ext.with_suffix('.tex')
if with_ext.is_file():
return with_ext
return None
@staticmethod
def node_input(node, parent_node, doc, cleaner):
input_args = list(node.contents)
if input_args.__len__() != 1:
if node.name == 'input':
print(f'Anomalous \\input, node.args should be length 1: {node}')
# else it can be a \usepackage
else:
link_target = str(input_args[0])
included_document_path = cleaner.find_included_document_path(cleaner.root_dir, link_target)
if included_document_path:
cleaner.add_file_to_process(included_document_path, cleaner.FILE_TYPE_TEX)
else:
print(f'Failed to find file {link_target} included from {cleaner.current_doc_path_relative}')
@staticmethod
def node_usepackage(node, parent_node, doc, cleaner):
"""
If `\\usepackage{p}` referfs to an existing `p.sty` file, copy that file.
"""
input_args = list(node.contents)
if input_args.__len__() != 1:
print(f'Anomalous \\input, node.args should be length 1: {node}')
else:
link_target = str(input_args[0])
package_path = (cleaner.root_dir / link_target).with_suffix('.sty')
if package_path.is_file():
cleaner.add_file_to_process(package_path, cleaner.FILE_TYPE_OPAQUE)
####################################################################################
# CLI
####################################################################################
@click.command()
@click.argument(
'src_root_document', type=click.Path(exists=True, file_okay=True, dir_okay=False))
#help='Example: my_article/top.tex') # click.Arguments don't have `help`, it is a deficiency of click
@click.argument(
'dest_dir', type=click.Path())
#help='Example: my_article_arxiv_submission')
@click.option(
'--remove-cmd', type=str,
help='Commands to be removed (\\comment is removed by default). Separate commands with , but not space, for example: comment,KL,WL')
@click.option(
'--short-circuit-cmd', type=str,
help='Commands to be replaced with their contents. Separate commands with , but not space, for example: kl,wl')
@click.option(
'--keep-file', type=click.Path(), multiple=True,
help='Copy this file to the output directory even if it is not references, repeat the option to add more files')
@click.option(
'--out-root-doc-name', type=str,
help='Rename the root doc to this name, by default `ms.tex`')
@click.option(
'--clear-out-dir', is_flag=True, default=False,
help='Delete the output directory before outputting')
@click.option(
'--remove-comments-completely/--keep-empty-comments', is_flag=True, default=False,
help='Removes the comments including the % (by default the comment body is removed but the % is kept)')
def main(
src_root_document, dest_dir,
remove_cmd = None, short_circuit_cmd=None,
keep_file = [], out_root_doc_name = None,
clear_out_dir=False, remove_comments_completely=False):
src_root_document = Path(src_root_document)
dest_dir = Path(dest_dir)
cleaner = TexSubmissionCleaner(src_root_document, dest_dir)
cleaner.keep_empty_comments = not remove_comments_completely
if clear_out_dir:
cleaner.clear_out_dir()
if out_root_doc_name:
if out_root_doc_name.endswith('.tex'):
out_root_doc_name = out_root_doc_name[:-4]
cleaner.out_root_doc_name = out_root_doc_name
if remove_cmd:
cmds_to_remove = remove_cmd.split(',')
print('Removing commands:', ', '.join(cmds_to_remove))
cleaner.commands_to_remove(*cmds_to_remove)
if short_circuit_cmd:
cmds_to_sc = short_circuit_cmd.split(',')
print('Short-circuiting commands:', ', '.join(cmds_to_sc))
cleaner.commands_to_short_circuit(*cmds_to_sc)
cleaner.additional_files_to_keep(keep_file)
cleaner.run()
cleaner.notify_about_unused_files()
cleaner.print_statistics()
if __name__ == '__main__':
main()