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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
# This file belongs to TLPM v2.20, TeX Live Package Manager
# Public Domain, P.Jackowski@gust.org.pl
# packages handling
# here the main work starts
%source_packages; # read only once (constant)
%target_packages; # read each time is requested (affected by installing / uninstalling)
$source_pkges = \%source_packages;
$target_pkges = \%target_packages;
sub read_pkges # used for reading both source and target packages
{
my ($dir,$pkges) = @_; # <root_directory> <hash_reference>
# StaW reports that the condition alike '-d E:' doesn't work in some cases.
# Not reproduced... However, '-d E:\texmf' works fine, so
unless(-d $dir || -d "$dir$tl_texmf")
{
return $error{'miss_dir'} -> ($dir);
}
$dir .= $tl_lists;
unless(-d $dir)
{
return $error{'miss_dir'} -> ($dir);
}
my ($pkg,$fcount,$pcount,$platform);
local (*DIR,*INP);
$message -> ("Reading packages from '$dir'...");
opendir(DIR,$dir) or return $error{'cant_odir'} -> ($dir);
readdir DIR; readdir DIR; # remove '.' and '..'; dirty but works and make things a bit faster
foreach $pkg (readdir DIR)
{
($pcount,$fcount) = (0,0);
if($pkg =~ /$reg_binary/)
{
$platform = $1;
if($platform =~ s/\A\.//) # bin-<pkg>.<platform>
{
unless(defined $tl_binaries{$platform}){next}
}
else # bin-<pkg>
{
foreach(keys %tl_binaries) # binaries are not linked by tmps
{
$pkges -> {$pkg}{'requires'}{"$pkg.$_"} = ++$pcount;
}
}
}
elsif($pkg =~ /$reg_library/)
{
$platform = $1;
if($platform =~ s/\A\.//) # only win32?
{
unless(defined $tl_binaries{$platform}){next}
}
else
{
$platform = 'win32';
if(defined $tl_binaries{$platform})
{
$pkges -> {$pkg}{'requires'}{"$pkg.$platform"} = ++$pcount;
}
}
}
open(INP,"<$dir$chr_dirsep$pkg")
or $error{'wrong_inp'} -> ("$dir$chr_dirsep$pkg") and next;
foreach(<INP>)
{
chomp;
if(s/$reg_require//)
{
$pkges -> {$pkg}{'requires'}{$_} = ++$pcount;
}
elsif(/$reg_about|$reg_action|$reg_empty/){}
else
{
$pkges -> {$pkg}{'contains'}{&dirsep2dirsep($_)} = ++$fcount;
}
}
close INP;
}
return $error{'empty_dir'} -> ($dir) unless keys %$pkges;
&find_missing($pkges); # not all bin-packages binaries for all platforms
closedir DIR;
return(0);
}
sub find_missing
{
my $pkges = shift;
my $pkg;
foreach $pkg (&get_pkg_all($pkges))
{
foreach(&get_pkg_requires($pkg,$pkges))
{
unless(exists $pkges -> {$_})
{
$pkges -> {$pkg}{'missing'}{$_} = $pkges -> {$pkg}{'requires'}{$_};
delete $pkges -> {$pkg}{'requires'}{$_};
}
}
}
}
sub get_pkg_miss
{
my ($pkg,$pkges) = @_;
return sort keys %{$pkges -> {$pkg}{'missing'}};
}
sub read_source
{
my $err;
if($err = &set_source()){return $err}
return(0) if %$source_pkges; # read once per session
$err = &read_pkges($tl_source,$source_pkges);
&undef_source if $err;
return $err;
}
sub read_target
{
&set_target();
return(0) if %$target_pkges; # read once per session (except if forced)
return &read_pkges($tl_target,$target_pkges);
}
sub set_source
{
return(0) if $tl_source_indeed;
$message -> ("Looking for TeX Live source...");
if(defined $tl_source) # firt check if predefined one is TL source indeed
{
if(&is_tl_root($tl_source))
{
return(0);
}
else
{
$error{'not_tl_source'} -> ($tl_source);
}
}
foreach($parent_dir,$curr_dir) # in case TLPM located on CD or DVD
{
if(&is_tl_root($_)){return(0)}
}
if($win32) # sorry, no heuristic for others
{
foreach(D..Z)
{
if(&is_tl_root("$_:")){return(0)}
}
unless($batch_mode) # the last chance is prompt
{
print STDOUT "TeX Live source drive: ";
$tl_source = <STDIN>;
chomp $tl_source;
&resolve_env($tl_source);
if(&is_tl_root($tl_source))
{
return(0);
}
else
{
$error{'not_tl_source'} -> ($tl_source);
}
}
}
else
{
unless($batch_mode)
{
print STDOUT "TeX Live source mounting point: ";
$tl_source = <STDIN>;
chomp $tl_source;
&resolve_env($tl_source);
if(&is_tl_root($tl_source))
{
return(0);
}
else
{
$error{'not_tl_source'} -> ($tl_source);
}
}
}
&undef_source(); # not found anyhow -> reset presets
return $error{'tl_not_found'} -> ();
}
sub is_tl_root
{
my $dir = shift;
foreach(@tl_roots)
{
next if &no_source($dir . $_);
$message -> ("Welcome to TeX Live $tl_year $tl_type! (release $tl_date)");
return $true;
}
return $false;
}
sub no_source # source detection heuristic
{
my $dir = shift;
my ($inst,$live) = ("00INST.TL","00LIVE.TL");
&norm_path($dir);
$dir = &rel2abs($dir);
#if(-f "$dir$chr_dirsep.mkisofsrc")# hidden can be easily missed while copying...
#{ # (Hartmut Henkel report)
if(-f "$dir$chr_dirsep$inst")
{
$messdbg -> ("Found $inst in $dir");
$tl_type = 'CD'; $tl_file = $inst;
}
elsif(-f "$dir$chr_dirsep$live")
{
$messdbg -> ("Found $live in $dir");
$tl_type = 'DVD'; $tl_file = $live;
}
else
{
$messdbg -> ("Neither $inst nor $live found in $dir");
return $true;
}
$tl_source = $dir; $tl_source_indeed = $true;
local *INP;
open(INP,"<$dir$chr_dirsep$tl_file") or return $error{'wrong_inp'} -> ($tl_file);
my @lines = <INP>; close INP;
foreach(@lines)
{
next unless /\ARelease date: (\d{8})/;
$tl_date = $1;
$tl_year = substr($tl_date,0,4);
return $false;
}
$messdbg -> ("Cannot retrieve release info from $tl_file");
#}
return $true;
}
# in opposite to source packages, target is read whenever requested
sub set_target
{
if($tl_target_indeed and $tl_target_new eq ''){return 0} # target defined and not provided in cmdline
if($tl_target_new ne '')
{
&undef_target();
$tl_target = $tl_target_new;
$tl_target_new = undef;
}
my $msg = 'TeX Live installation root directory';
if($tl_target eq '')
{
if($batch_mode)
{
$tl_target = $curr_dir; # hm... what shall we do?
}
else # prompt for target directory
{
print STDOUT ('Type ' . $msg . ': ');
$tl_target = <STDIN>;
&set_target_();
}
}
else
{
&set_target_();
}
# I tried to test if the directory is writable, but even CD drive respond to be writable...
#unless(-w $tl_target)
#{
# &undef_target();
# return $error{'cant_wdir'} -> ($tl_target);
#}
$message -> ($msg . ' set to ' . &singleq($tl_target));
return(0);
}
sub set_target_
{
&norm_path($tl_target);
&resolve_env($tl_target);
$tl_target = &rel2abs($tl_target,$curr_dir);
$tl_target_indeed = $true;
}
sub undef_source
{
%source_packages = ();
$tl_source = undef;
$tl_source_indeed = $false;
}
sub undef_target
{
%target_packages = ();
$tl_target = undef;
$tl_target_indeed = $false;
}
# some operations on loaded packages stored in hashes
sub get_pkg_all
{
my $pkges = shift;
return sort keys %$pkges;
}
sub get_file_all
{
my $pkges = shift;
return map {sort keys %{$pkges -> {$_}{'contains'}}} &get_pkg_all($pkges);
}
sub get_pkg_requires
{
my ($pkg,$pkges) = @_;
return sort
{
$pkges -> {$pkg}{'requires'}{$a} <=> $pkges -> {$pkg}{'requires'}{$b}
}
keys %{$pkges -> {$pkg}{'requires'}};
}
sub get_pkg_requires_all
{
my ($pkg,$pkges) = @_;
local (%trace,@trace);
local $level = 0;
&get_pkg_requires_recurse($pkg,$pkges);
# delete $trace{$pkg}; # package never requires itself
shift @trace;
return @trace;
}
sub get_pkg_requires_recurse
{
my ($pkg,$pkges) = @_;
return(0) if exists $trace{$pkg};
#++$level;
$trace{$pkg} = $true;
push(@trace,$pkg);
foreach(&get_pkg_requires($pkg,$pkges))
{
&get_pkg_requires_recurse($_,$pkges);
}
#--$level;
}
sub get_pkg_contains
{
my ($pkg,$pkges) = @_;
return sort keys %{$pkges -> {$pkg}{'contains'}};
}
sub get_pkg_contains_all # assuming each file belongs to ONE package!
{
my ($pkg,$pkges) = @_;
return
(
&get_pkg_contains($pkg,$pkges),
map {&get_pkg_contains($_,$pkges)} &get_pkg_requires_all($pkg,$pkges)
);
}
sub get_pkg_belongs
{
my ($pkg,$pkges) = @_;
my ($key, @arr);
foreach $key (&get_pkg_all($pkges))
{
foreach(&get_pkg_requires($key,$pkges))
{
if($_ eq $pkg)
{
push(@arr,$key);
}
}
}
return @arr;
}
# no longer used
#sub is_required
#{
# my $pkg = shift;
# my $key;
# foreach $key (&get_pkg_all($target_pkges))
# {
# foreach (&get_pkg_requires($key,$target_pkges))
# {
# return 1 if $_ eq $pkg;
# }
# }
# return 0;
#}
sub get_file_belongs
{
my $file = quotemeta(shift @_);
my $pkges = shift;
$file = qr/$reg_dirsep$file\z/i;
my ($key,@arr);
foreach $key (&get_pkg_all($pkges))
{
foreach(&get_pkg_contains($key,$pkges))
{
if($_ =~ /$file/)
{
push @arr, $key;
}
}
}
return @arr;
}
sub is_pkg
{
my ($pkg,$pkges) = @_;
return $true if exists $pkges -> {$pkg};
return $false
}
sub is_file
{
my ($file,$pkges) = @_;
$file = quotemeta($file);
$file = qr/$reg_dirsep$file\z/i;
foreach(&get_file_all($pkges))
{
return $true if /$file/;
}
return $false;
}
# searching for tpm file to show info
sub get_tpm
{
my $pkg = shift;
my $file;
if(defined $tl_target)
{
if($file = &get_tpm_($tl_target,$pkg))
{
return $file;
}
}
return('') if &set_source();
if($file = &get_tpm_($tl_source,$pkg))
{
return $file;
}
return('');
}
sub get_tpm_
{
my ($dir,$pkg) = @_;
my $file;
foreach(@tl_tpms)
{
$file = "$dir$_$chr_dirsep$pkg.tpm";
if(-f $file)
{
return $file;
}
}
return '';
}
1;
|