/
meld_all.py
61 lines (44 loc) · 1.56 KB
/
meld_all.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
#!/usr/bin/env python
"""
useful for updating templates.
e.g.
meld_all .appveyor.yml
meld_all .appveyor.yml -l Fortran
"""
from pathlib import Path
from argparse import ArgumentParser
import filecmp
import subprocess
try:
import ghlinguist as ghl
LANG = ghl.linguist(Path(__file__).parent, rtype=True)
except ImportError:
ghl = None
EXE = 'meld'
def meldloop(root: Path, filename: Path, language: str=LANG, exe: str=EXE):
flist = list(root.rglob(filename.name))
print(f'comparing {len(flist)} files vs {filename}')
# Not using check_call due to spurious errors
for f in flist:
if filecmp.cmp(f, filename, shallow=False):
continue
if ghl is not None:
if ghl.linguist(f.parent, rtype=True) != language:
continue
subprocess.run([exe, str(filename), str(f)])
def main():
p = ArgumentParser()
p.add_argument('filename', help='filename to compare against')
p.add_argument('root', help='top-level directory to search under', nargs='?')
p.add_argument('-l', '--language', help='language to template', default=LANG)
p.add_argument('-exe', help='program to compare with', default=EXE)
p = p.parse_args()
fn = Path(p.filename).expanduser()
if not fn.is_file():
raise FileNotFoundError(f'{fn} is not a file')
root = fn.resolve().parents[1] if not p.root else Path(p.root).expanduser()
if not root.is_dir():
raise FileNotFoundError(f'{root} is not a directory')
meldloop(root, fn, p.language, p.exe)
if __name__ == '__main__':
main()