-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_and_move.py
executable file
·79 lines (61 loc) · 1.91 KB
/
clean_and_move.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
#!/usr/bin/env python
"""A simple script to remove auxiliary files generated by LaTeX compilation and move PDF files into the correct directory."""
import os
from glob import glob
from typing import List, Callable, Optional
import sys
GLOB_EXTENSIONS = (
'*.log',
'*.aux',
'*.dvi',
'*.lof',
'*.lot',
'*.bit',
'*.idx',
'*.glo',
'*.bbl',
'*.bcf',
'*.ilg',
'*.toc',
'*.ind',
'*.out',
'*.blg',
'*.fdb_latexmk',
'*.fls',
'*.run.xml',
'*.synctex.gz'
)
def clean_files(directory: str = None) -> None:
"""Remove auxiliary files generated by LaTeX compilation."""
direc = directory + os.sep if directory is not None else ''
# This sum is to remove any empty lists caused by failed matches, and to flatten the list
# It's not supposed to be used like this, but it works
auxiliary_files: List[str] = sum([glob(direc + extension) for extension in GLOB_EXTENSIONS], [])
for f in auxiliary_files:
os.remove(f)
def move_pdfs(directory: str = None) -> None:
"""Move PDF files into the correct directory."""
direc = directory + os.sep if directory is not None else ''
pdfs = glob(direc + '*.pdf')
for pdf in pdfs:
new = os.path.join(direc + 'pdfs', os.path.split(pdf)[-1])
try:
os.remove(new)
except FileNotFoundError:
pass
os.rename(pdf, new)
def do_all(func: Callable[[Optional[str]], None]) -> None:
"""Run a function for all subdirectories."""
for d in os.listdir():
func(d)
if __name__ == "__main__":
if len(sys.argv) == 1: # If no args
do_all(clean_files)
do_all(move_pdfs)
elif sys.argv[1] == 'clean':
do_all(clean_files)
elif sys.argv[1] == 'move':
do_all(move_pdfs)
else:
print(f'Unrecognised argument "{sys.argv[1]}"')
print(f'Usage: ./{sys.argv[0].split(os.sep)[-1]} [ clean | move ]')