-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtexfilehash.py
64 lines (56 loc) · 1.98 KB
/
texfilehash.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
#!/usr/bin/env python
# file: texfilehash.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2019 R.F. Smith <[email protected]>.
# SPDX-License-Identifier: MIT
# Created: 2019-06-28T16:13:39+0200
# Last modified: 2021-12-18T11:34:25+0100
"""
Create a file containing the abbreviated git commit hash for TeX source files.
If the file has uncommitted changes, it appends the status in red text.
It produces a file <filename>.hash for every <filename>.tex.
"""
import argparse
import subprocess as sp
import logging
import sys
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--log",
default="warning",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'warning')",
)
parser.add_argument(
"filenames", nargs="*", metavar="filename", help="TeX files to process"
)
args = parser.parse_args(sys.argv[1:])
logging.basicConfig(
level=getattr(logging, args.log.upper(), None), format="%(levelname)s: %(message)s"
)
if not args.filenames:
logging.warning("no filenames given")
sys.exit(1)
for infn in args.filenames:
logging.debug(f"processing file {infn}")
if not infn.endswith(".tex"):
logging.error(f"{infn} is not a TeX file; skipping")
continue # Skip.
outfn = infn[:-4] + ".hash"
logcmd = ["git", "--no-pager", "log", "-1", "--pretty=format:%h", infn]
cp = sp.run(logcmd, stdout=sp.PIPE, stderr=sp.DEVNULL, check=True, text=True)
logdata = cp.stdout
logging.debug(f'logdata: "{logdata}"')
statcmd = ["git", "status", "--porcelain", infn]
cp = sp.run(statcmd, stdout=sp.PIPE, stderr=sp.DEVNULL, check=True, text=True)
statdata = cp.stdout[:2].strip()
logging.debug(f'statdata: "{statdata}"')
if statdata:
logdata = logdata + r"\,\textcolor{red}{" + statdata + r"}%"
logging.info(f"{infn} has unrecorded changes")
else:
logdata += "%"
with open(outfn, "wt") as outf:
outf.write(logdata)
logging.info(f'wrote "{outfn}"')