summaryrefslogtreecommitdiff
path: root/Master/tlpkg/TeXLive/TLUtils.pm
blob: f30b2410f1f626657e96a5f99e3db21843cab141 (plain)
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
# $Id$
# TeXLive::TLUtils.pm
# common functions for TeX Live Infrastructure
#
# Copyright 2007 Norbert Preining
# This file is licensed under the GNU General Public License version 2
# or any later version.
#
# ideas from Fabrice Popineau's FileUtils.pm.

package TeXLive::TLUtils;

=pod

=head1 NAME

C<TeXLive::TLUtils> -- Utilities used in the TeX Live Infrastructure Modules

=head1 SYNOPSIS

  use TeXLive::TLUtils;

  TeXLive::TLUtils::sort_uniq(@list);
  TeXLive::TLUtils::push_uniq(\@list, @items);
  TeXLive::TLUtils::member($item, @list);
  TeXLive::TLUtils::debug($string);
  TeXLive::TLUtils::getenv($string);
  TeXLive::TLUtils::which($string);
  TeXLive::TLUtils::initialze_installer;
  TeXLive::TLUtils::media;
  TeXLive::TLUtils::platform;
  TeXLive::TLUtils::win32;
  TeXLive::TLUtils::program_exists($program);
  TeXLive::TLUtils::binaries_available_from_disk($platform);
  TeXLive::TLUtils::binaries_available_from_net($platform);

=head1 DESCRIPTION

=cut

use File::Basename;

BEGIN {
  use Exporter ();
  use Cwd;
  use File::Path;
  use vars qw( @ISA @EXPORT_OK );
  @ISA = qw(Exporter);
  @EXPORT_OK = qw(
    &sort_uniq
    &push_uniq
    &member
    &debug
    &getenv
    &which
    &media
    &platform
    &initialize_installer
    &win32
    &program_exists
    &binaries_available_from_disk
    &binaries_available_from_net
  );
}

=pod

=over 6

=item C<sort_uniq(@list)>

The C<sort_uniq> function sorts the given array and throws away multiple
occurrences of elements. It returns a sorted and unified array.

=cut

sub sort_uniq {
  my (@l) = @_;
  my ($e, $f, @r);
  $f = "";
  @l = sort(@l);
  foreach $e (@l) {
    if ($e ne $f) {
      $f = $e;
      push @r, $e;
    }
  }
  return @r;
}

=pod

=item C<push_uniq(\@list, @items)>

The C<push_uniq> function pushes the last elements on the list referenced
by the first argument.

=cut

sub push_uniq {
  # can't we use $l as a reference, and then use my?  later ...
  local (*l, @le) = @_;
  foreach my $e (@le) {
    if (! &member($e, @l)) {
      push @l, $e;
    }
  }
}

=pod

=item C<member($item, @list)>

The C<member> function returns true if the the first argument is contained
in the list of the remaining arguments.

=cut

sub member {
  my ($e, @l) = @_;
  my ($f);
  foreach $f (@l) {
    if ($e eq $f) {
      return 1;
    }
  }
  return 0;
}


=pod

=item C<initialize_installer>

The function C<initialize_installer> first determines the directory in
which tl-install.pl resides (which is C<`dirname $0`> on UNIX).  Then
it calls C<media> and C<platform> in order to set the variables
C<$::_media_> and C<$::_platform_>.

Repeatedly calls to C<media> or C<platform> do not access the CD/DVD
again.

=cut

sub initialize_installer {
  ($::_installerdir_=$0)=~s/(.*)\/.*$/$1/;
  &media;
  &platform;
}


=pod

=item C<debug($string)>

The C<debug> function echos the argument string to STDERR in case that
the global varialbe C<opt_debug> is set.

=cut

sub debug {
  print STDERR @_ if ($::opt_debug);
}

=pod

=item C<getenv($string)>

Get an environment variable.  It is assumed that the environment
variable contains a path.  On Windows all backslashes are replaced by
forward slashes as required by Perl.  If this behavior is not desired,
use C<$ENV{"$variable"}> instead.  C<0> is returned if the
environment variable is not set.

=cut

sub getenv {
  my $envvar=shift;
  my $var=$ENV{"$envvar"};
  return 0 unless (defined "$var");
  if (&win32) {
    $var=~s@\\@/@g;  # change \ -> / (required by Perl)
  }
  return "$var";
}

=pod

=item C<which($string)>

C<which> does the same as the UNIX command C<which(1)>, but it is
supposed to work on Windows too.  On Windows we have to try all the
extensions given in the C<PATHEXT> environment variable.  We also try
without appending an extension because if C<$string> comes from an
environment variable, an extension might aleady be present.

=cut

sub which {
	my $prog=shift;
	my @PATH;
	my $PATH=getenv('PATH');
	if (&win32) {
		my @PATHEXT=split ';', getenv('PATHEXT');
		push @PATHEXT, '';  # if argument contains an extension
		@PATH=split ';', $PATH;
		for my $dir (@PATH) {
			for my $ext (@PATHEXT) {
				if (-f "$dir/$prog$ext") {
					return "$dir/$prog$ext";
				}
			}
		}
	} else {
		@PATH=split ':', $PATH;
		for my $dir (@PATH) {
			if (-x "$dir/$prog") {
				return "$dir/$prog";
			}
		}
	} 
	return 0;
}

=pod


=item C<media>

Return media type.  Either C<CD>, C<DVD>, or C<NET>.

The result is stored in a global variable C<$::_media_>.  If you call
C<media> repeatedly, only the first call of C<media> will access the
CD/DVD.

=cut

sub media {
  unless (defined $::_media_) {
    if (-d "$::_installerdir_/archive") {
      $::_media_="CD";
    } elsif (-d "$::_installerdir_/texmf/web2c") {
      $::_media_="DVD";
    } else {
      $::_media_="NET";
    }
  }
  return $::_media_;
}

=pod

=item C<platform>

If the directory C</usr/bin> doesn't exist we know that we're on
Windows and we set the global variable C<$::_platform_> to C<win32>.
If it exists we call C<config.guess>.  The output of C<config.guess>
is filtered as described below.

CPU type is determined by a regexp.  And it's necessary to
C<s/i.86/i386/>.

For OS we need a list because we probably have something like
C<linux-gnu> but we need C<linux>.  This list might/should contain OSs
which are not currently supported.  The list currently supports all
platforms supported by TeXLive 2007 plus Cygwin.

If a particular platform is not found in this list we use the regexp
C</.*-(.*$)/> as a last resort and hope it provides something useful.

The result is stored in a global variable C<$::_platform_>.  If you call
C<platform> repeatedly, only the first call of C<platform> will access
the HD/CD/DVD.

=cut

sub platform {
  my @OSs=qw(aix cygwin darwin freebsd hpux irix linux netbsd openbsd
             solaris); 

  unless (defined $::_platform_) {
    if (-d "/usr/bin") {
      my $CPU; # CPU type as reported by config.guess.
      my $OS;  # O/S type as reported by config.guess.
      my $guessed_platform=`$::_installerdir_/config.guess`;
      chomp $guessed_platform;
      ($CPU=$guessed_platform)=~s/(.*?)-.*/$1/;
      foreach my $os (@OSs) {
        $OS=$os if $guessed_platform=~/$os/;
      }
      if ($OS=~/^i.86$/) {
        $OS=~s/i.86/i386/;
      }      
      if (length "$OS" < 0) {
        ($OS=$guessed_platform)=~s/.*-(.*)/$1/;
      }
      $::_platform_="$CPU-$OS";
    } else {
      $::_platform_="win32";
    }
  }
  return $::_platform_;
}

=pod 

=item C<win32>

Return C<1> if platform is Windows and C<0> otherwise.

=cut

sub win32 {
  return (&platform eq "win32")? 1:0;
}

=pod 

=item C<program_exists($program)>

Return C<1> if C<$program> is in C<PATH> and C<0> otherwise.

=cut

sub program_exists {
  my $program=shift;
  return (&which ("$program"))? 0:1;
}

=pod

=item C<binaries_available_from_disk>

The function C<binaries_available_from_disk> returns a list of
supported platforms.  Media type has to be either C<CD> or C<DVD>.

=cut

sub binaries_available_from_disk {
  my @dir;
  my @platforms;
	if (&media eq "CD") {
    opendir DIR, "$::_installerdir_/archive";
  } elsif (&media eq "DVD") {
    opendir DIR, "$::_installerdir_/texmf/bin";
  } else {
    die "Media type must be CD or DVD.\n";
  }
  @dir=readdir DIR;
  chomp @dir;
  if (&media eq "CD") {
    for (@dir) {
      if (/bin-kpathsea.(.*).zip$/) {
        push @platforms, $1;
      }
    }
  } elsif (&media eq "DVD") {
    for (@dir) {
      push @platforms, $1 unless (/^\./);
    }
  } else {
    die "Media type must be CD or DVD.\n";
  }
  return @platforms;
}
  
=pod

=item C<binaries_available_from_net>

The function C<binaries_available_from_net> returns a list of
platforms supported by the TeXLive server.  It downloads the file
C<$::texlive_url/texlive.tlpdb> and looks for files
C</^bin\/(.*)\/kpsewhich/>.

=cut
               
sub binaries_available_from_net {
  my @platforms;
  open TLPDB, "wget -q --output-document=- $::texlive_url/texlive.tlpdb|"
       or die "Can't open $::texlive_url/texlive.tlpdb";
  while (<TLPDB>) {
    if (/^bin\/(.*)\/kpsewhich/) {
      push @platforms, $1;
    }
  }
  return @platforms;
}

1;


=pod

=back

=head1 SEE ALSO

The modules L<TeXLive::TLPSRC>, L<TeXLive::TLPOBJ>, 
L<TeXLive::TLPDB>, L<TeXLive::TLTREE>, and the
document L<Perl-API.txt> and the specification in the TeX Live
repository trunk/Master/tlpkg/doc/.

=head1 AUTHORS AND COPYRIGHT

This script and its documentation were written for the TeX Live
distribution (L<http://tug.org/texlive>) and both are licensed under the
GNU General Public License Version 2 or later.

=cut

### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #