lib/LaTeXML/Util/Pathname.pm

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