lib/LaTeXML/Util/Pathname.pm

filter: Statementstmt 19.5% 49 / 251 Branchbran 7.2% 11 / 152 Conditioncond 2.3% 1 / 43 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 27.2% 9 / 33 Podpod 72.0% 18 / 25 totaltotal 16.6% 88 / 529 CC 97 SCAR 52.8
CC
97
Cov
14%
candidate_pathnames
756.0
build_kpse_cache
182.0
pathname_make
90.0
CRAP 197.1
« lib/LaTeXML/Core/Token.pm ↑ Summary lib/LaTeXML/Post/MathML/Linebreaker.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Util::Pathname | #
3 # | Pathname Utilities for LaTeXML | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 #======================================================================
13 # Sanely combine features of File::Spec and File::Basename
14 # Somehow, both modules tend to bite me at random times.
15 # eg. sometimes Basename's fileparse doesn't extract extension.
16 # sometimes File::Spec seems to do too many filesystem checks (gets slow!)
17 # File::Spec->splitpath "may or may not ... trailing '/'" ... Huh?
18 #======================================================================
19 # My first instinct is that this should bless the pathnames,
20 # but strings as pathnames come so naturally in perl;
21 # But I may still do it...
22 #======================================================================
23 # Some portability changes for Windows, thanks to Ioan Sucan.
24 #======================================================================
25 # Packages in the LaTeXML::Util package set have no dependence on LaTeXML
26 # objects or context.
27 #======================================================================
28 package LaTeXML::Util::Pathname;
29 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 29 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 29 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 29 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 29 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 29 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 29 our @EXPORT = qw( &pathname_find &pathname_findall &pathname_kpsewhich
37 &pathname_make &pathname_canonical
38 &pathname_split &pathname_directory &pathname_name &pathname_type
39 &pathname_timestamp
40 &pathname_concat
41 &pathname_relative &pathname_absolute
42 &pathname_is_absolute &pathname_is_contained
43 &pathname_is_url &pathname_is_literaldata
44 &pathname_protocol
45 &pathname_cwd &pathname_chdir &pathname_mkdir &pathname_copy
46 &pathname_installation);
47  
48 # NOTE: For absolute pathnames, the directory component starts with
49 # whatever File::Spec considers to be the volume, or "/".
50 #======================================================================
51 # Ioan Sucan suggests switching this to '\\' for windows, but notes
52 # that it works as it is, so we'll leave it (for now).
53 ### my $SEP = '/'; # [CONSTANT]
54 # Some indicators that this is not sufficient? (calls to libraries/externals???)
55 # PRELIMINARY test, probably need to be even more careful
56 29 my $ISWINDOWS = $^O =~ /^(MSWin|NetWare|cygwin)/i;
57 29 ▶my $SEP = ($ISWINDOWS ? '\\' : '/'); # [CONSTANT]
Branch: 1 branch
BranchTrueFalseTotal
$ISWINDOWS ? : 0 29 29
58 29 ▶my $KPATHSEP = ($ISWINDOWS ? ';' : ':'); # [CONSTANT]
Branch: 1 branch
BranchTrueFalseTotal
$ISWINDOWS ? : 0 29 29
59 29 my $LITERAL_RE = '(?:literal)(?=:)'; # [CONSTANT]
60 29 my $PROTOCOL_RE = '(?:https|http|ftp)(?=:)'; # [CONSTANT]
61  
62 #======================================================================
63 # pathname_make(dir=>dir, name=>name, type=>type);
64 # Returns a pathname. This will be an absolute path if
65 # dir (or the first, if dir is an array), is absolute.
66 sub pathname_make {
67 0 ▶ my (%pieces) = @_;
Subroutine pathname_make: not called CC=9 SCAR=45.0
Pod documented
68 0 my $pathname = '';
69 0 ▶ if (my $dir = $pieces{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = $pieces{"dir"}) 0 0 0
70 0 ▶ my @dirs = (ref $dir eq 'ARRAY' ? @$dir : ($dir));
Branch: 1 branch
BranchTrueFalseTotal
ref $dir eq 'ARRAY' ? : 0 0 0
71 0 $pathname = shift(@dirs);
72 0 foreach my $d (@dirs) {
73 0 $pathname =~ s|\Q$SEP\E$||; $dir =~ s|^\Q$SEP\E||;
74 0 $pathname .= $SEP . $dir; } }
75 0 ▶ $pathname .= $SEP if $pathname && $pieces{name} && $pathname !~ m|\Q$SEP\E$|;
Branch: 1 branch
BranchTrueFalseTotal
if $pathname and $pieces{"name"} and not $pathname =~ /\Q$SEP\E$/ 0 0 0
Condition0%0/6
$pathname and $pieces{"name"} and not $pathname =~ /\Q$SEP\E$/
!ll&&!rl&&r
000
$pathname and $pieces{"name"}
!ll&&!rl&&r
000
MC/DC0%0/3
$pathname and $pieces{"name"} and not $pathname =~ /\Q$SEP\E$/
$pathname $pieces{"name"} not $pathname =~ /\Q$SEP\E$/
Truth table0%0/4
A and B and not(C)
A= $pathnameB= $pieces{"name"}C= not $pathname =~ /\Q$SEP\E$/
ABCresult
0XX0
10X0
1100
1111
76 0 ▶ $pathname .= $pieces{name} if $pieces{name};
Branch: 1 branch
BranchTrueFalseTotal
if $pieces{"name"} 0 0 0
77 0 ▶ $pathname .= '.' . $pieces{type} if $pieces{type};
Branch: 1 branch
BranchTrueFalseTotal
if $pieces{"type"} 0 0 0
78 0 return pathname_canonical($pathname); }
79  
80 # Split the pathname into components (dir,name,type).
81 # If pathname is absolute, dir starts with volume or '/'
82 sub pathname_split {
83 0 ▶ my ($pathname) = @_;
Subroutine pathname_split: not called CC=6 SCAR=37.4
Pod documented
84 0 $pathname = pathname_canonical($pathname);
85 0 my ($vol, $dir, $name) = File::Spec->splitpath($pathname);
86 # Hmm, for /, we get $dir = / but we want $vol='/' ?????
87 0 ▶ if ($vol) { $dir = $vol . $dir; }
Branch: 2 branches
BranchTrueFalseTotal
if ($vol) { } 0 0 0
elsif ('File::Spec'->file_name_is_absolute($pathname) and not 'File::Spec'->file_name_is_absolute($dir)) { } 0 0 0
88 0 ▶ elsif (File::Spec->file_name_is_absolute($pathname) && !File::Spec->file_name_is_absolute($dir)) { $dir = $SEP . $dir; }
Condition0%0/3
"File::Spec"->file_name_is_absolute($pathname) and not "File::Spec"->file_name_is_absolute($dir)
!ll&&!rl&&r
000
MC/DC0%0/2
"File::Spec"->file_name_is_absolute($pathname) and not "File::Spec"->file_name_is_absolute($dir)
"File::Spec"->file_name_is_absolute($pathname) not "File::Spec"->file_name_is_absolute($dir)
Truth table0%0/3
A and not(B)
A= "File::Spec"->file_name_is_absolute($pathname)B= not "File::Spec"->file_name_is_absolute($dir)
ABresult
0X0
100
111
89 # $dir shouldn't end with separator, unless it is root.
90 0 ▶ $dir =~ s/\Q$SEP\E$// unless $dir eq $SEP;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir eq $SEP 0 0 0
91 0 my $type = '';
92 0 ▶ if ($name =~ s/\.([^\.]+)$//) { $type = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/\.([^\.]+)$//) 0 0 0
93 0 return ($dir, $name, $type); }
94  
95 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 # This likely needs portability work!!! (particularly regarding urls, separators, ...)
98 # AND, care about symbolic links and collapsing ../ !!!
99 sub pathname_canonical {
100 232 ▶ my ($pathname) = @_;
Subroutine pathname_canonical: called CC=7 SCAR=21.3
Pod documented
101 232 ▶ if ($pathname =~ /^($LITERAL_RE)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ /^($LITERAL_RE)/) 0 232 232
102 0 return $pathname; }
103 # Don't call pathname_is_absolute, etc, here, cause THEY call US!
104 232 ▶ confess "Undefined pathname!" unless defined $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pathname 0 232 232
105 # File::Spec->canonpath($pathname); }
106 232 $pathname =~ s|^~|$ENV{HOME}|;
107 # We CAN canonicalize urls, but we need to be careful about the // before host!
108 # OHHH, but we DON'T want \ for separator!
109 232 my $urlprefix = undef;
110 232 ▶ if ($pathname =~ s|^($PROTOCOL_RE//[^/]*)/|/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ s[^($PROTOCOL_RE//[^/]*)/][/]) 0 232 232
111 0 $urlprefix = $1; }
112  
113 232 ▶ if ($pathname =~ m|//+/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ m[//+/]) 0 232 232
114 0 Carp::cluck "Recursive pathname? : $pathname\n"; }
115 ## $pathname =~ s|//+|/|g;
116 232 $pathname =~ s|/\./|/|g;
117 # Collapse any foo/.. patterns, but not ../..
118 232 ▶ while ($pathname =~ s|/(?!\.\./)[^/]+/\.\.(/\|$)|$1|) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ s[/(?!\.\./)[^/]+/\.\.(/|$)][$1]) 2 232 234
119 232 $pathname =~ s|^\./||;
120 232 ▶ return (defined $urlprefix ? $urlprefix . $pathname : $pathname); }
Branch: 1 branch
BranchTrueFalseTotal
defined $urlprefix ? : 0 232 232
121  
122 # Convenient extractors;
123 sub pathname_directory {
124 0 ▶ my ($pathname) = @_;
Subroutine pathname_directory: not called CC=1 SCAR=6.9
Pod documented
125 0 my ($dir, $name, $type) = pathname_split($pathname);
126 0 return $dir; }
127  
128 sub pathname_name {
129 0 ▶ my ($pathname) = @_;
Subroutine pathname_name: not called CC=1 SCAR=6.9
Pod documented
130 0 my ($dir, $name, $type) = pathname_split($pathname);
131 0 return $name; }
132  
133 sub pathname_type {
134 0 ▶ my ($pathname) = @_;
Subroutine pathname_type: not called CC=1 SCAR=6.9
Pod documented
135 0 my ($dir, $name, $type) = pathname_split($pathname);
136 0 return $type; }
137  
138 # Note that this returns ONLY recognized protocols!
139 sub pathname_protocol {
140 0 ▶ my ($pathname) = @_;
Subroutine pathname_protocol: not called CC=2 SCAR=17.9
Pod undocumented
141 0 ▶ return ($pathname =~ /^($PROTOCOL_RE|$LITERAL_RE)/ ? $1 : 'file'); }
Branch: 1 branch
BranchTrueFalseTotal
$pathname =~ /^($PROTOCOL_RE|$LITERAL_RE)/ ? : 0 0 0
142  
143 #======================================================================
144 sub pathname_concat {
145 0 ▶ my ($dir, $file) = @_;
Subroutine pathname_concat: not called CC=5 SCAR=34.0
Pod documented
146 0 ▶ return $file unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
147 0 ▶ return $dir if !defined $file || ($file eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "." 0 0 0
Condition0%0/3
not defined $file or $file eq "."
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $file or $file eq "."
not defined $file $file eq "."
Truth table0%0/3
not(A) or B
A= not defined $fileB= $file eq "."
ABresult
1X1
011
000
148 0 ▶ return pathname_canonical(File::Spec->catpath('', $dir || '', $file)); }
Condition0%0/2
$dir || ''
l!l
00
MC/DC0%0/1
$dir || ''
$dir
Truth table0%0/2
A
A= $dir
Aresult
11
00
149  
150 #======================================================================
151 # Is $pathname an absolute pathname ?
152 # pathname_is_absolute($pathname) => (0|1)
153 sub pathname_is_absolute {
154 0 ▶ my ($pathname) = @_;
Subroutine pathname_is_absolute: not called CC=2 SCAR=17.9
Pod documented
155 0 ▶ return $pathname && File::Spec->file_name_is_absolute(pathname_canonical($pathname)); }
Condition0%0/3
$pathname && 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
!ll&&!rl&&r
000
MC/DC0%0/2
$pathname && 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
$pathname 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
Truth table0%0/3
A && B
A= $pathnameB= 'File::Spec'->file_name_is_absolute(pathname_canonical($pathname))
ABresult
0X0
100
111
156  
157 sub pathname_is_url {
158 0 ▶ my ($pathname) = @_;
Subroutine pathname_is_url: not called CC=3 SCAR=24.8
Pod documented
159 0 ▶ return $pathname && $pathname =~ /^($PROTOCOL_RE)/ && $1; } # Other protocols?
Condition0%0/6
$pathname && $pathname =~ /^($PROTOCOL_RE)/ && $1
!ll&&!rl&&r
000
$pathname and $pathname =~ /^($PROTOCOL_RE)/
!ll&&!rl&&r
000
MC/DC0%0/3
$pathname && $pathname =~ /^($PROTOCOL_RE)/ && $1
$pathname $pathname =~ /^($PROTOCOL_RE)/ $1
Truth table0%0/4
A and B && C
A= $pathnameB= $pathname =~ /^($PROTOCOL_RE)/C= $1
ABCresult
0XX0
10X0
1100
1111
160  
161 sub pathname_is_literaldata {
162 0 ▶ my ($pathname) = @_;
Subroutine pathname_is_literaldata: not called CC=2 SCAR=17.9
Pod undocumented
163 0 ▶ if ($pathname =~ /^($LITERAL_RE)/) { return $1; } else { return; } }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ /^($LITERAL_RE)/) { } 0 0 0
164  
165 # Check whether $pathname is contained in (ie. underneath) $base
166 # Returns the relative pathname if it is underneath; undef otherwise.
167 sub pathname_is_contained {
168 0 ▶ my ($pathname, $base) = @_;
Subroutine pathname_is_contained: not called CC=2 SCAR=17.9
Pod documented
169 # after assuring that both paths are absolute,
170 # get $pathname relative to $base
171 0 my $rel = pathname_canonical(pathname_relative(pathname_absolute($pathname),
172 pathname_absolute($base)));
173 # If the relative pathname starts with "../" that it apparently is NOT underneath base!
174 0 ▶ return ($rel =~ m|^\.\.(?:/\|\Q$SEP\E)| ? undef : $rel); }
Branch: 1 branch
BranchTrueFalseTotal
$rel =~ m[^\.\.(?:/|\Q$SEP\E)] ? : 0 0 0
175  
176 # pathname_relative($pathname,$base) => $relativepathname
177 # If $pathname is an absolute, non-URL pathname,
178 # return the pathname relative to $base,
179 # else just return its canonical form.
180 # Actually, if it's a url and $base is also url, to SAME host! & protocol...
181 # we _could_ make relative...
182 sub pathname_relative {
183 0 ▶ my ($pathname, $base) = @_;
Subroutine pathname_relative: not called CC=4 SCAR=30.0
Pod documented
184 0 $pathname = pathname_canonical($pathname);
185 0 ▶ return ($base && pathname_is_absolute($pathname) && !pathname_is_url($pathname)
Branch: 1 branch
BranchTrueFalseTotal
$base && pathname_is_absolute($pathname) && !pathname_is_url($pathname) ? : 0 0 0
Condition0%0/6
$base and pathname_is_absolute($pathname) and not pathname_is_url($pathname)
!ll&&!rl&&r
000
$base and pathname_is_absolute($pathname)
!ll&&!rl&&r
000
MC/DC0%0/3
$base and pathname_is_absolute($pathname) and not pathname_is_url($pathname)
$base pathname_is_absolute($pathname) not pathname_is_url($pathname)
Truth table0%0/4
A and B and not(C)
A= $baseB= pathname_is_absolute($pathname)C= not pathname_is_url($pathname)
ABCresult
0XX0
10X0
1100
1111
186 ? File::Spec->abs2rel($pathname, pathname_canonical($base))
187 : $pathname); }
188  
189 sub pathname_absolute {
190 0 ▶ my ($pathname, $base) = @_;
Subroutine pathname_absolute: not called CC=4 SCAR=30.0
Pod documented
191 0 $pathname = pathname_canonical($pathname);
192 0 ▶ return (!pathname_is_absolute($pathname) && !pathname_is_url($pathname)
Branch: 2 branches
BranchTrueFalseTotal
!(pathname_is_absolute($pathname) || pathname_is_url($pathname)) ? : 0 0 0
$base ? : 0 0 0
Condition0%0/3
pathname_is_absolute($pathname) || pathname_is_url($pathname)
l!l&&r!l&&!r
000
MC/DC0%0/2
pathname_is_absolute($pathname) || pathname_is_url($pathname)
pathname_is_absolute($pathname) pathname_is_url($pathname)
Truth table0%0/3
A || B
A= pathname_is_absolute($pathname)B= pathname_is_url($pathname)
ABresult
1X1
011
000
193 ? File::Spec->rel2abs($pathname, ($base ? pathname_canonical($base) : pathname_cwd()))
194 : $pathname); }
195  
196 #======================================================================
197 # Actual file system operations.
198 sub pathname_timestamp {
199 0 ▶ my ($pathname) = @_;
Subroutine pathname_timestamp: not called CC=2 SCAR=17.9
Pod documented
200 0 ▶ return -f $pathname ? (stat($pathname))[9] : 0; }
Branch: 1 branch
BranchTrueFalseTotal
-f $pathname ? : 0 0 0
201  
202 29 our $CWD = undef;
203 # DO NOT use pathname_cwd, unless you also use pathname_chdir to change dirs!!!
204 sub pathname_cwd {
205 0 ▶ if (!defined $CWD) {
Subroutine pathname_cwd: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $LaTeXML::Util::Pathname::CWD) 0 0 0
206 0 ▶ if (my $cwd = cwd()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cwd = cwd()) { } 0 0 0
207 0 $CWD = pathname_canonical($cwd); }
208 else {
209 # Fatal not imported
210 0 die "INTERNAL: Could not determine current working directory (cwd)"
211 . "Perhaps a problem with Perl's locale settings?"; } }
212 0 return $CWD; }
213  
214 sub pathname_chdir {
215 0 ▶ my ($directory) = @_;
Subroutine pathname_chdir: not called CC=1 SCAR=6.9
Pod undocumented
216 0 chdir($directory);
217 0 pathname_cwd(); # RE-cache $CWD!
218 0 return; }
219  
220 sub pathname_mkdir {
221 0 ▶ my ($directory) = @_;
Subroutine pathname_mkdir: not called CC=5 SCAR=34.0
Pod documented
222 0 ▶ return unless $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless $directory 0 0 0
223 0 $directory = pathname_canonical($directory);
224 0 my ($volume, $dirs, $last) = File::Spec->splitpath($directory);
225 0 my (@dirs) = (File::Spec->splitdir($dirs), $last);
226 0 ▶ for (my $i = 0 ; $i <= $#dirs ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#dirs) 0 0 0
227 0 my $dir = File::Spec->catpath($volume, File::Spec->catdir(@dirs[0 .. $i]), '');
228 0 ▶ if (!-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
229 0 ▶ mkdir($dir) or return; } }
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or return 0 0 0
230 0 return $directory; }
231  
232 # copy a file, preserving attributes, if possible.
233 # Why doesn't File::Copy preserve attributes on Unix !?!?!?
234 sub pathname_copy {
235 0 ▶ my ($source, $destination) = @_;
Subroutine pathname_copy: not called CC=6 SCAR=37.4
Pod documented
236 # If it _needs_ to be copied:
237 0 $source = pathname_canonical($source);
238 0 $destination = pathname_canonical($destination);
239 0 ▶ if ((!-f $destination) || (pathname_timestamp($source) > pathname_timestamp($destination))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $destination or pathname_timestamp($source) > pathname_timestamp($destination)) 0 0 0
Condition0%0/3
not -f $destination or pathname_timestamp($source) > pathname_timestamp($destination)
l!l&&r!l&&!r
000
MC/DC0%0/2
not -f $destination or pathname_timestamp($source) > pathname_timestamp($destination)
not -f $destination pathname_timestamp($source) > pathname_timestamp($destination)
Truth table0%0/3
not(A) or B
A= not -f $destinationB= pathname_timestamp($source) > pathname_timestamp($destination)
ABresult
1X1
011
000
240 0 ▶ if (my $destdir = pathname_directory($destination)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $destdir = pathname_directory($destination)) 0 0 0
241 0 ▶ pathname_mkdir($destdir) or return; }
Branch: 1 branch
BranchTrueFalseTotal
pathname_mkdir($destdir) or return 0 0 0
242 ### if($^O =~ /^(MSWin32|NetWare)$/){ # Windows
243 ### # According to Ioan, this should work:
244 ### system("xcopy /P $source $destination")==0 or return; }
245 ### else { # Unix
246 ### system("cp --preserve=timestamps $source $destination")==0 or return; }
247 # Hopefully this portably copies, preserving timestamp.
248 0 ▶ copy($source, $destination) or return;
Branch: 1 branch
BranchTrueFalseTotal
copy($source, $destination) or return 0 0 0
249 0 my ($atime, $mtime) = (stat($source))[8, 9];
250 0 utime $atime, $mtime, $destination; # And set the modification time
251 }
252 0 return $destination; }
253  
254 #======================================================================
255 # pathname_find($pathname, paths=>[...], types=>[...]) => $absolute_pathname;
256 # Find a file corresponding to $pathname returning the absolute,
257 # completed pathname if found, else undef
258 # * If $pathname is a not an absolute pathname
259 # (although it may still have directory components)
260 # then if search $paths are given, search for it relative to
261 # each of the directories in $paths,
262 # else search for it relative to the current working directory.
263 # * If types is given, then search (in each searched directory)
264 # for the first file with the given extension.
265 # The extension "" (empty string) means to search for the exact name.
266 # * If types is not given, search for the exact named file
267 # without additional extension.
268 # * If installation_subdir is given, look in that subdirectory of where LaTeXML
269 # was installed, by appending it to the paths.
270  
271 # This is presumably daemon safe...
272 232 ▶my @INSTALLDIRS = grep { (-f "$_.pm") && (-d $_) }
Branch: 1 branch
BranchTrueFalseTotal
-f "$_.pm" and -d $_ 87 145 232
273 29 map { pathname_canonical($_ . $SEP . 'LaTeXML') } @INC; # [CONSTANT]
274  
275 sub pathname_installation {
276 0 ▶ return $INSTALLDIRS[0]; }
Subroutine pathname_installation: not called CC=1 SCAR=6.9
Pod undocumented
277  
278 sub pathname_find {
279 0 ▶ my ($pathname, %options) = @_;
Subroutine pathname_find: not called CC=4 SCAR=30.0
Pod documented
280 0 ▶ return unless $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless $pathname 0 0 0
281 0 my @paths = candidate_pathnames($pathname, %options);
282 0 foreach my $path (@paths) {
283 0 ▶ return $path if -f $path; }
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 0 0 0
284 0 return; }
285  
286 sub pathname_findall {
287 0 ▶ my ($pathname, %options) = @_;
Subroutine pathname_findall: not called CC=2 SCAR=17.9
Pod documented
288 0 ▶ return unless $pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless $pathname 0 0 0
289 0 my @paths = candidate_pathnames($pathname, %options);
290 0 return grep { -f $_ } @paths; }
291  
292 # It's presumably cheep to concatinate all the pathnames,
293 # relative to the cost of testing for files,
294 # and this simplifies overall.
295 sub candidate_pathnames {
296 0 ▶ my ($pathname, %options) = @_;
Subroutine candidate_pathnames: not called CC=27 SCAR=66.3
Pod undocumented
297 0 my @dirs = ();
298 0 ▶ $pathname = pathname_canonical($pathname) unless $pathname eq '*';
Branch: 1 branch
BranchTrueFalseTotal
unless $pathname eq "*" 0 0 0
299 0 ▶ my ($pathdir, $name, $type) = ($pathname eq '*' ? (undef, '*', undef) : pathname_split($pathname));
Branch: 1 branch
BranchTrueFalseTotal
$pathname eq '*' ? : 0 0 0
300 0 ▶ $name .= '.' . $type if (defined $type) && ($type ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $type ne "" 0 0 0
Condition0%0/3
defined $type and $type ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type ne ""
defined $type $type ne ""
Truth table0%0/3
A and B
A= defined $typeB= $type ne ""
ABresult
0X0
100
111
301 # generate the set of search paths we'll use.
302 0 ▶ if (pathname_is_absolute($pathname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pathname_is_absolute($pathname)) { } 0 0 0
303 0 push(@dirs, $pathdir); }
304 else {
305 0 my $cwd = pathname_cwd();
306 0 ▶ if ($options{paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"paths"}) 0 0 0
307 0 foreach my $p (@{ $options{paths} }) {
308 # Complete the search paths by prepending current dir to relative paths,
309 0 ▶ my $pp = pathname_concat((pathname_is_absolute($p) ? pathname_canonical($p) : pathname_concat($cwd, $p)),
Branch: 1 branch
BranchTrueFalseTotal
pathname_is_absolute($p) ? : 0 0 0
310 $pathdir);
311 0 ▶ push(@dirs, $pp) unless grep { $pp eq $_ } @dirs; } } # but only include each dir ONCE
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$pp eq $_;} @dirs 0 0 0
312 0 ▶ push(@dirs, pathname_concat($cwd, $pathdir)) unless @dirs; # At least have the current directory!
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 0 0 0
313 # And, if installation dir specified, append it.
314 0 ▶ if (my $subdir = $options{installation_subdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subdir = $options{"installation_subdir"}) 0 0 0
315 0 push(@dirs, map { pathname_concat($_, $subdir) } @INSTALLDIRS); } }
316  
317 # extract the desired extensions.
318 0 my @exts = ();
319 0 ▶ if ($options{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"type"}) 0 0 0
320 0 push(@exts, '.' . $options{type}); }
321 0 ▶ if ($options{types}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"types"}) 0 0 0
322 0 foreach my $ext (@{ $options{types} }) {
323 0 ▶ if ($ext eq '') { push(@exts, ''); }
Branch: 3 branches
BranchTrueFalseTotal
if ($ext eq '') { } 0 0 0
elsif ($ext eq '*') { } 0 0 0
elsif ($pathname =~ /\.\Q$ext\E$/i) { } 0 0 0
324 elsif ($ext eq '*') {
325 0 push(@exts, '.*', ''); }
326 elsif ($pathname =~ /\.\Q$ext\E$/i) {
327 0 push(@exts, ''); }
328 else {
329 0 push(@exts, '.' . $ext); } } }
330 0 ▶ push(@exts, '') unless @exts;
Branch: 1 branch
BranchTrueFalseTotal
unless @exts 0 0 0
331  
332 0 my @paths = ();
333 # Now, combine; precedence to leading directories.
334 0 foreach my $dir (@dirs) {
335 0 foreach my $ext (@exts) {
336 0 ▶ if ($name eq '*') { # Unfortunately, we've got to test the file system NOW...
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '*') { } 0 0 0
elsif ($ext eq '.*') { } 0 0 0
337 0 ▶ if ($ext eq '.*') { # everything
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq '.*') { } 0 0 0
338 0 ▶ opendir(DIR, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or next 0 0 0
339 0 push(@paths, map { pathname_concat($dir, $_) } grep { !/^\./ } readdir(DIR));
340 0 closedir(DIR); }
341 else {
342 0 ▶ opendir(DIR, $dir) or next; # ???
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or next 0 0 0
343 0 push(@paths, map { pathname_concat($dir, $_) } grep { /\Q$ext\E$/ } readdir(DIR));
344 0 closedir(DIR); } }
345 elsif ($ext eq '.*') { # Unfortunately, we've got to test the file system NOW...
346 0 ▶ opendir(DIR, $dir) or next; # ???
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or next 0 0 0
347 0 push(@paths, map { pathname_concat($dir, $_) } grep { /^\Q$name\E\.\w+$/ } readdir(DIR));
348 0 closedir(DIR); }
349 else {
350 0 push(@paths, pathname_concat($dir, $name . $ext)); } } }
351 0 return @paths; }
352  
353 #======================================================================
354 29 ▶our $kpsewhich = which($ENV{LATEXML_KPSEWHICH} || 'kpsewhich');
Condition50%1/2
$ENV{'LATEXML_KPSEWHICH'} || 'kpsewhich'
l!l
029
MC/DC0%0/1
$ENV{'LATEXML_KPSEWHICH'} || 'kpsewhich'
$ENV{'LATEXML_KPSEWHICH'}
Truth table50%1/2
A
A= $ENV{'LATEXML_KPSEWHICH'}
Aresult
11
00
355 29 our $kpse_cache = undef;
356 29 our $kpse_toolchain = "";
357  
358 sub pathname_kpsewhich {
359 0 ▶ my (@candidates) = @_;
Subroutine pathname_kpsewhich: not called CC=8 SCAR=42.8
Pod undocumented
360 0 ▶ return unless $kpsewhich;
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::Util::Pathname::kpsewhich 0 0 0
361 0 ▶ build_kpse_cache() unless $kpse_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::Util::Pathname::kpse_cache 0 0 0
362 0 foreach my $file (@candidates) {
363 0 ▶ if (my $result = $$kpse_cache{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $LaTeXML::Util::Pathname::kpse_cache->{$file}) 0 0 0
364 0 return $result; } }
365 # If we've failed to read the cache, try directly calling kpsewhich
366 # For multiple calls, this is slower in general. But MiKTeX, eg., doesn't use texmf ls-R files!
367 0 my $files = join(' ', @candidates);
368 0 ▶ if ($kpsewhich && (my $result = `"$kpsewhich" $files $kpse_toolchain`)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LaTeXML::Util::Pathname::kpsewhich and my $result = `"$LaTeXML::Util::Pathname::kpsewhich" $files $LaTeXML::Util::Pathname::kpse_toolchain`) 0 0 0
Condition0%0/3
$LaTeXML::Util::Pathname::kpsewhich and my $result = `"$LaTeXML::Util::Pathname::kpsewhich" $files $LaTeXML::Util::Pathname::kpse_toolchain`
!ll&&!rl&&r
000
MC/DC0%0/2
$LaTeXML::Util::Pathname::kpsewhich and my $result = `"$LaTeXML::Util::Pathname::kpsewhich" $files $LaTeXML::Util::Pathname::kpse_toolchain`
$LaTeXML::Util::Pathname::kpsewhich my $result = `"$LaTeXML::Util::Pathname::kpsewhich" $files $LaTeXML::Util::Pathname::kpse_toolchain`
Truth table0%0/3
A and B
A= $LaTeXML::Util::Pathname::kpsewhichB= my $result = `"$LaTeXML::Util::Pathname::kpsewhich" $files $LaTeXML::Util::Pathname::kpse_toolchain`
ABresult
0X0
100
111
369 0 ▶ if ($result =~ /^\s*(.+?)\s*\n/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ /^\s*(.+?)\s*\n/s) 0 0 0
370 0 return $1; } }
371 0 return; }
372  
373 sub build_kpse_cache {
374 0 ▶ $kpse_cache = {}; # At least we've tried.
Subroutine build_kpse_cache: not called CC=13 SCAR=52.0
Pod undocumented
375 0 ▶ return unless $kpsewhich;
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::Util::Pathname::kpsewhich 0 0 0
376 # This finds ALL the directories looked for for any purposes, including docs, fonts, etc
377 0 ▶ if ($ENV{"APPVEYOR"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"APPVEYOR"}) 0 0 0
378 0 $kpse_toolchain = "--miktex-admin";
379 }
380 0 my $texmf = `"$kpsewhich" --expand-var \'\\\$TEXMF\' $kpse_toolchain`; chomp($texmf);
381 # These are directories which contain the tex related files we're interested in.
382 # (but they're typically below where the ls-R indexes are!)
383 0 my $texpaths = `"$kpsewhich" --show-path tex $kpse_toolchain`; chomp($texpaths);
384 0 my @filters = ();
385 0 foreach my $path (split(/$KPATHSEP/, $texpaths)) {
386 0 $path =~ s/^!!//; $path =~ s|//+$|/|;
387 0 ▶ push(@filters, $path) if -d $path; }
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 0 0 0
388 0 $texmf =~ s/^["']//; $texmf =~ s/["']$//;
389 0 $texmf =~ s/^\s*\\\{(.+?)}\s*$/$1/s;
390 0 $texmf =~ s/\{\}//g;
391 0 my @dirs = split(/,/, $texmf);
392 0 foreach my $dir (@dirs) {
393 0 $dir =~ s/^!!//;
394 # Presumably if no ls-R, we can ignore the directory?
395 0 ▶ if (-f "$dir/ls-R") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$dir/ls-R") 0 0 0
396 0 my $LSR;
397 my $subdir;
398 0 my $skip = 0; # whether to skip entries in the current subdirectory.
399 0 ▶ open($LSR, '<', "$dir/ls-R") or die "Cannot read $dir/ls-R: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $LSR, "<", "$dir/ls-R" or die "Cannot read $dir/ls-R: $!" 0 0 0
400 0 ▶ while (<$LSR>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $LSR)) 0 0 0
401 0 chop;
402 0 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
403 0 ▶ if (/^%/) { }
Branch: 3 branches
BranchTrueFalseTotal
if (/^%/) { } 0 0 0
elsif (/^(.*?):$/) { } 0 0 0
elsif (not $skip) { } 0 0 0
404 elsif (/^(.*?):$/) { # Move to a new subdirectory
405 0 $subdir = $1;
406 0 $subdir =~ s|^\./||; # remove prefix
407 0 my $d = $dir . '/' . $subdir; # Hopefully OS safe, for comparison?
408 0 $skip = !grep { $d =~ /^\Q$_\E/ } @filters; } # check if one of the TeX paths
409 elsif (!$skip) {
410 # Is it safe to use '/' here?
411 0 my $sep = '/';
412 0 $$kpse_cache{$_} = join($sep, $dir, $subdir, $_); } }
413 0 close($LSR); } }
414 0 return; }
415  
416 #======================================================================
417 29 1;
418  
419 __END__
« lib/LaTeXML/Core/Token.pm ↑ Summary lib/LaTeXML/Post/MathML/Linebreaker.pm »