| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# This file is a part of Memoize, a TeX package for externalization of |
| 4 |
# graphics and memoization of compilation results in general, available at |
| 5 |
# https://ctan.org/pkg/memoize and https://github.com/sasozivanovic/memoize. |
| 6 |
# |
| 7 |
# Copyright (c) 2020- Saso Zivanovic <[email protected]> |
| 8 |
# |
| 9 |
# This work may be distributed and/or modified under the conditions of the |
| 10 |
# LaTeX Project Public License, either version 1.3c of this license or (at |
| 11 |
# your option) any later version. The latest version of this license is in |
| 12 |
# https://www.latex-project.org/lppl.txt and version 1.3c or later is part of |
| 13 |
# all distributions of LaTeX version 2008 or later. |
| 14 |
# |
| 15 |
# This work has the LPPL maintenance status `maintained'. |
| 16 |
# The Current Maintainer of this work is Saso Zivanovic. |
| 17 |
# |
| 18 |
# The files belonging to this work and covered by LPPL are listed in |
| 19 |
# <texmf>/doc/generic/memoize/FILES. |
| 20 |
|
| 21 |
__version__ = '2024/12/02 v1.4.1' |
| 22 |
|
| 23 |
import argparse, re, sys, pathlib, os |
| 24 |
|
| 25 |
parser = argparse.ArgumentParser( |
| 26 |
description="Remove (stale) memo and extern files.", |
| 27 |
epilog = "For details, see the man page or the Memoize documentation " |
| 28 |
"(https://ctan.org/pkg/memoize)." |
| 29 |
) |
| 30 |
parser.add_argument('--yes', '-y', action = 'store_true', |
| 31 |
help = 'Do not ask for confirmation.') |
| 32 |
parser.add_argument('--all', '-a', action = 'store_true', |
| 33 |
help = 'Remove *all* memos and externs.') |
| 34 |
parser.add_argument('--quiet', '-q', action = 'store_true') |
| 35 |
parser.add_argument('--prefix', '-p', action = 'append', default = [], |
| 36 |
help = 'A path prefix to clean; this option can be specified multiple times.') |
| 37 |
parser.add_argument('mmz', nargs= '*', help='.mmz record files') |
| 38 |
parser.add_argument('--version', '-V', action = 'version', |
| 39 |
version = f"%(prog)s of Memoize " + __version__) |
| 40 |
args = parser.parse_args() |
| 41 |
|
| 42 |
re_prefix = re.compile(r'\\mmzPrefix *{(.*?)}') |
| 43 |
re_memo = re.compile(r'%? *\\mmz(?:New|Used)(?:CC?Memo|Extern) *{(.*?)}') |
| 44 |
re_endinput = re.compile(r' *\\endinput *$') |
| 45 |
|
| 46 |
prefixes = set(pathlib.Path(prefix).resolve() for prefix in args.prefix) |
| 47 |
keep = set() |
| 48 |
|
| 49 |
# We loop through the given .mmz files, adding prefixes to whatever manually |
| 50 |
# specified by the user, and collecting the files to keep. |
| 51 |
for mmz_fn in args.mmz: |
| 52 |
mmz = pathlib.Path(mmz_fn) |
| 53 |
mmz_parent = mmz.parent.resolve() |
| 54 |
try: |
| 55 |
with open(mmz) as mmz_fh: |
| 56 |
prefix = '' |
| 57 |
endinput = False |
| 58 |
empty = None |
| 59 |
for line in mmz_fh: |
| 60 |
line = line.strip() |
| 61 |
|
| 62 |
if not line: |
| 63 |
pass |
| 64 |
|
| 65 |
elif endinput: |
| 66 |
raise RuntimeError( |
| 67 |
rf'Bailing out, ' |
| 68 |
rf'\endinput is not the last line of file {mmz_fn}.') |
| 69 |
|
| 70 |
elif m := re_prefix.match(line): |
| 71 |
prefix = m[1] |
| 72 |
prefixes.add( (mmz_parent/prefix).resolve() ) |
| 73 |
if empty is None: |
| 74 |
empty = True |
| 75 |
|
| 76 |
elif m := re_memo.match(line): |
| 77 |
if not prefix: |
| 78 |
raise RuntimeError( |
| 79 |
f'Bailing out, no prefix announced before file "{m[1]}".') |
| 80 |
if not m[1].startswith(prefix): |
| 81 |
raise RuntimeError( |
| 82 |
f'Bailing out, prefix of file "{m[1]}" does not match ' |
| 83 |
f'the last announced prefix ({prefix}).') |
| 84 |
keep.add((mmz_parent / m[1])) |
| 85 |
empty = False |
| 86 |
|
| 87 |
elif re_endinput.match(line): |
| 88 |
endinput = True |
| 89 |
continue |
| 90 |
|
| 91 |
else: |
| 92 |
raise RuntimeError(fr"Bailing out, " |
| 93 |
fr"file {mmz_fn} contains an unrecognized line: {line}") |
| 94 |
|
| 95 |
if empty and not args.all: |
| 96 |
raise RuntimeError(fr'Bailing out, file {mmz_fn} is empty.') |
| 97 |
|
| 98 |
if not endinput and empty is not None and not args.all: |
| 99 |
raise RuntimeError( |
| 100 |
fr'Bailing out, file {mmz_fn} does not end with \endinput; ' |
| 101 |
fr'this could mean that the compilation did not finish properly. ' |
| 102 |
fr'You can only clean with --all.' |
| 103 |
) |
| 104 |
|
| 105 |
# It is not an error if the file doesn't exist. |
| 106 |
# Otherwise, cleaning from scripts would be cumbersome. |
| 107 |
except FileNotFoundError: |
| 108 |
pass |
| 109 |
|
| 110 |
tbdeleted = [] |
| 111 |
def populate_tbdeleted(folder, basename_prefix): |
| 112 |
re_aux = re.compile( |
| 113 |
re.escape(basename_prefix) + |
| 114 |
r'[0-9A-F]{32}(?:-[0-9A-F]{32})?' |
| 115 |
r'(?:-[0-9]+)?(?:\.memo|(?:-[0-9]+)?\.pdf|\.log)$') |
| 116 |
try: |
| 117 |
for f in folder.iterdir(): |
| 118 |
if re_aux.match(f.name) and (args.all or f not in keep): |
| 119 |
tbdeleted.append(f) |
| 120 |
except FileNotFoundError: |
| 121 |
pass |
| 122 |
|
| 123 |
for prefix in prefixes: |
| 124 |
# "prefix" is interpreted both as a directory (if it exists) and a basename prefix. |
| 125 |
if prefix.is_dir(): |
| 126 |
populate_tbdeleted(prefix, '') |
| 127 |
populate_tbdeleted(prefix.parent, prefix.name) |
| 128 |
|
| 129 |
allowed_dirs = [pathlib.Path().absolute()] # todo: output directory |
| 130 |
deletion_not_allowed = [f for f in tbdeleted if not f.is_relative_to(*allowed_dirs)] |
| 131 |
if deletion_not_allowed: |
| 132 |
raise RuntimeError("Bailing out, " |
| 133 |
"I was asked to delete these files outside the current directory:\n" + |
| 134 |
"\n".join(str(f) for f in deletion_not_allowed)) |
| 135 |
|
| 136 |
_cwd_absolute = pathlib.Path().absolute() |
| 137 |
def relativize(path): |
| 138 |
try: |
| 139 |
return path.relative_to(_cwd_absolute) |
| 140 |
except ValueError: |
| 141 |
return path |
| 142 |
|
| 143 |
if tbdeleted: |
| 144 |
tbdeleted.sort() |
| 145 |
if not args.yes: |
| 146 |
print('I will delete the following files:') |
| 147 |
for f in tbdeleted: |
| 148 |
print(relativize(f)) |
| 149 |
print("Proceed (y/n)? ") |
| 150 |
a = input() |
| 151 |
if args.yes or a == 'y' or a == 'yes': |
| 152 |
for f in tbdeleted: |
| 153 |
if not args.quiet: |
| 154 |
print("Deleting", relativize(f)) |
| 155 |
try: |
| 156 |
f.unlink() |
| 157 |
except FileNotFoundError: |
| 158 |
print(f"Cannot delete {f}") |
| 159 |
else: |
| 160 |
print("Bailing out.") |
| 161 |
elif not args.quiet: |
| 162 |
print('Nothing to do, the directory seems clean.') |
| 163 |
|
| 164 |
# Local Variables: |
| 165 |
# fill-column: 79 |
| 166 |
# after-save-hook: py2dtx |
| 167 |
# End: |