Debian Codegraph @

texk/texlive/linked_scripts/memoize/memoize-clean.pl

raw
1 #!/usr/bin/env perl
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 my $PROG = 'memoize-clean.pl';
22 my $VERSION = '2024/12/02 v1.4.1';
23
24 use strict;
25 use Getopt::Long;
26 use Cwd 'realpath';
27 use File::Spec;
28 use File::Basename;
29
30 my $usage = "usage: $PROG [-h] [--yes] [--all] [--quiet] [--prefix PREFIX] " .
31 "[mmz ...]\n";
32 my $Help = <<END;
33 Remove (stale) memo and extern files produced by package Memoize.
34
35 positional arguments:
36 mmz .mmz record files
37
38 options:
39 -h, --help show this help message and exit
40 --version, -V show version and exit
41 --yes, -y Do not ask for confirmation.
42 --all, -a Remove *all* memos and externs.
43 --quiet, -q
44 --prefix PREFIX, -p PREFIX
45 A path prefix to clean;
46 this option can be specified multiple times.
47
48 For details, see the man page or the Memoize documentation.
49 END
50
51 my ($yes, $all, @prefixes, $quiet, $help, $print_version);
52 GetOptions(
53 "yes|y" => \$yes,
54 "all|a" => \$all,
55 "prefix|p=s" => \@prefixes,
56 "quiet|q" => \$quiet,
57 "help|h|?" => \$help,
58 "version|V" => \$print_version,
59 ) or die $usage;
60 $help and die "$usage\n$Help";
61 if ($print_version) { print("memoize-clean.pl of Memoize $VERSION\n"); exit 0 }
62
63 my (%keep, %prefixes);
64
65 my $curdir = Cwd::getcwd();
66
67 for my $prefix (@prefixes) {
68 $prefixes{Cwd::realpath(File::Spec->catfile(($curdir), $prefix))} = '';
69 }
70
71 my @mmzs = @ARGV;
72
73 for my $mmz (@mmzs) {
74 my ($mmz_filename, $mmz_dir) = File::Basename::fileparse($mmz);
75 @ARGV = ($mmz);
76 my $endinput = 0;
77 my $empty = -1;
78 my $prefix = "";
79 while (<>) {
80 if (/^ *$/) {
81 } elsif ($endinput) {
82 die "Bailing out, \\endinput is not the last line of file $mmz.\n";
83 } elsif (/^ *\\mmzPrefix *{(.*?)}/) {
84 $prefix = $1;
85 $prefixes{Cwd::realpath(File::Spec->catfile(($curdir,$mmz_dir), $prefix))} = '';
86 $empty = 1 if $empty == -1;
87 } elsif (/^%? *\\mmz(?:New|Used)(?:CC?Memo|Extern) *{(.*?)}/) {
88 my $fn = $1;
89 if ($prefix eq '') {
90 die "Bailing out, no prefix announced before file $fn.\n";
91 }
92 $keep{Cwd::realpath(File::Spec->catfile(($mmz_dir), $fn))} = 1;
93 $empty = 0;
94 if (rindex($fn, $prefix, 0) != 0) {
95 die "Bailing out, prefix of file $fn does not match " .
96 "the last announced prefix ($prefix).\n";
97 }
98 } elsif (/^ *\\endinput *$/) {
99 $endinput = 1;
100 } else {
101 die "Bailing out, file $mmz contains an unrecognized line: $_\n";
102 }
103 }
104 die "Bailing out, file $mmz is empty.\n" if $empty && !$all;
105 die "Bailing out, file $mmz does not end with \\endinput; this could mean that " .
106 "the compilation did not finish properly. You can only clean with --all.\n"
107 if $endinput == 0 && !$all;
108 }
109
110 my @tbdeleted;
111 sub populate_tbdeleted {
112 my ($basename_prefix, $dir, $suffix_dummy) = @_;
113 opendir(MD, $dir) or die "Cannot open directory '$dir'";
114 while( (my $fn = readdir(MD)) ) {
115 my $path = File::Spec->catfile(($dir),$fn);
116 if ($fn =~
117 /\Q$basename_prefix\E[0-9A-F]{32}(?:-[0-9A-F]{32})?(?:-[0-9]+)?#
118 (\.memo|(?:-[0-9]+)?\.pdf|\.log)/x
119 and ($all || !exists($keep{$path}))) {
120 push @tbdeleted, $path;
121 }
122 }
123 closedir(MD);
124 }
125 for my $prefix (keys %prefixes) {
126 my ($basename_prefix, $dir, $suffix);
127 if (-d $prefix) {
128 populate_tbdeleted('', $prefix, '');
129 }
130 populate_tbdeleted(File::Basename::fileparse($prefix));
131 }
132 @tbdeleted = sort(@tbdeleted);
133
134 my @allowed_dirs = ($curdir);
135 my @deletion_not_allowed;
136 for my $f (@tbdeleted) {
137 my $f_allowed = 0;
138 for my $dir (@allowed_dirs) {
139 if ($f =~ /^\Q$dir\E/) {
140 $f_allowed = 1;
141 last;
142 }
143 }
144 push(@deletion_not_allowed, $f) if ! $f_allowed;
145 }
146 die "Bailing out, I was asked to delete these files outside the current directory:\n" .
147 join("\n", @deletion_not_allowed) if (@deletion_not_allowed);
148
149 if (scalar(@tbdeleted) != 0) {
150 my $a;
151 unless ($yes) {
152 print("I will delete the following files:\n" .
153 join("\n",@tbdeleted) . "\n" .
154 "Proceed (y/n)? ");
155 $a = lc(<>);
156 chomp $a;
157 }
158 if ($yes || $a eq 'y' || $a eq 'yes') {
159 foreach my $fn (@tbdeleted) {
160 print "Deleting ", $fn, "\n" unless $quiet;
161 unlink $fn;
162 }
163 } else {
164 die "Bailing out.\n";
165 }
166 } elsif (!$quiet) {
167 print "Nothing to do, the directory seems clean.\n";
168 }
169
170 # Local Variables:
171 # after-save-hook: pl2dtx
172 # End: