]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Lib.pm
4140642b64a7ff000f8496b0b22a7b3b8c06c6c4
[debhelper.git] / Debian / Debhelper / Dh_Lib.pm
1 #!/usr/bin/perl -w
2 #
3 # Library functions for debhelper programs, perl version.
4 #
5 # Joey Hess, GPL copyright 1997-2000.
6
7 package Debian::Debhelper::Dh_Lib;
8 use strict;
9
10 use Exporter;
11 use vars qw(@ISA @EXPORT %dh);
12 @ISA=qw(Exporter);
13 @EXPORT=qw(&init &doit &complex_doit &verbose_print &error &warning &tmpdir
14             &pkgfile &pkgext &isnative &autoscript &filearray &GetPackages
15             &xargs %dh);
16
17 my $max_compat=3;
18
19 sub init {
20         # If DH_OPTIONS is set, prepend it @ARGV.
21         if (defined($ENV{DH_OPTIONS})) {
22                 unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
23         }
24
25         # Check to see if an argument on the command line starts with a dash.
26         # if so, we need to pass this off to the resource intensive 
27         # Getopt::Long, which I'd prefer to avoid loading at all if possible.
28         my $parseopt=undef;
29         my $arg;
30         foreach $arg (@ARGV) {
31                 if ($arg=~m/^-/) {
32                         $parseopt=1;
33                         last;
34                 }       
35         }
36         if ($parseopt) {
37                 eval "use Debian::Debhelper::Dh_Getopt";
38                 error($!) if $@;
39                 %dh=Debian::Debhelper::Dh_Getopt::parseopts();
40         }
41
42         # Check to see if DH_VERBOSE environment variable was set, if so,
43         # make sure verbose is on.
44         if (defined $ENV{DH_VERBOSE} && $ENV{DH_VERBOSE} ne "") {
45                 $dh{VERBOSE}=1;
46         }
47
48         # Check to see if DH_NO_ACT environment variable was set, if so, 
49         # make sure no act mode is on.
50         if (defined $ENV{DH_NO_ACT} && $ENV{DH_NO_ACT} ne "") {
51                 $dh{NO_ACT}=1;
52         }
53
54         # Get the name of the main binary package (first one listed in
55         # debian/control).
56         my @allpackages=GetPackages();
57         $dh{MAINPACKAGE}=$allpackages[0];
58
59         # Check if packages to build have been specified, if not, fall back to
60         # the default, doing them all.
61         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
62                 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
63                         # User specified that all arch (in)dep package be 
64                         # built, and there are none of that type.
65                         error("I have no package to act on");
66                 }
67                 push @{$dh{DOPACKAGES}},@allpackages;
68         }
69
70         # Check to see if -P was specified. If so, we can only act on a single
71         # package.
72         if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
73                 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
74         }
75
76         # Figure out which package is the first one we were instructed to build.
77         # This package gets special treatement: files and directories specified on
78         # the command line may affect it.
79         $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
80
81         # Split the U_PARAMS up into an array.
82         my $u=$dh{U_PARAMS};
83         undef $dh{U_PARAMS};
84         if (defined $u) {
85                 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
86         }
87 }
88
89 # Escapes out shell metacharacters in a line of shell script.
90 sub escape_shell {
91         my $line=shift;
92         # This list is from _Unix in a Nutshell_. (except '#')
93         $line=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
94         return $line;
95 }
96
97 # Run a command, and display the command to stdout if verbose mode is on.
98 # All commands that modifiy files in $TMP should be ran via this 
99 # function.
100 #
101 # Note that this cannot handle complex commands, especially anything
102 # involving redirection. Use complex_doit instead.
103 sub doit {
104         verbose_print(join(" ",map { escape_shell($_) } @_));
105         
106         if (! $dh{NO_ACT}) {
107                 system(@_) == 0 || error("command returned error code");
108         }
109 }
110
111 # Run a command and display the command to stdout if verbose mode is on.
112 # Use doit() if you can, instead of this function, because this function
113 # forks a shell. However, this function can handle more complicated stuff
114 # like redirection.
115 sub complex_doit {
116         verbose_print(join(" ",@_));
117         
118         if (! $dh{NO_ACT}) {
119                 # The join makes system get a scalar so it forks off a shell.
120                 system(join(" ",@_)) == 0
121                         || error("command returned error code");
122         }                       
123 }
124
125 # Run a command that may have a huge number of arguments, like xargs does.
126 # Pass in a reference to an array containing the arguments, and then other
127 # parameters that are the command and any parameters that should be passed to
128 # it each time.
129 sub xargs {
130         my $args=shift;
131
132         # The kernel can accept command lines up to 20k worth of characters.
133         my $command_max=20000; # LINUX SPECIFIC!!
134                         # I could use POSIX::ARG_MAX, but that would be slow.
135
136         # Figure out length of static portion of command.
137         my $static_length=0;
138         foreach (@_) {
139                 $static_length+=length($_)+1;
140         }
141         
142         my @collect=();
143         my $length=$static_length;
144         foreach (@$args) {
145                 if (length($_) + 1 + $static_length > $command_max) {
146                         error("This command is greater than the maximum command size allowed by the kernel, and cannot be split up further. What on earth are you doing? \"@_ $_\"");
147                 }
148                 $length+=length($_) + 1;
149                 if ($length < $command_max) {
150                         push @collect, $_;
151                 }
152                 else {
153                         doit(@_,@collect) if $#collect > -1;
154                         @collect=($_);
155                         $length=$static_length + length($_) + 1;
156                 }
157         }
158         doit(@_,@collect) if $#collect > -1;
159 }
160
161 # Print something if the verbose flag is on.
162 sub verbose_print {
163         my $message=shift;
164         
165         if ($dh{VERBOSE}) {
166                 print "\t$message\n";
167         }
168 }
169
170 # Output an error message and exit.
171 sub error {
172         my $message=shift;
173
174         warning($message);
175         exit 1;
176 }
177
178 # Output a warning.
179 sub warning {
180         my $message=shift;
181         
182         print STDERR basename($0).": $message\n";
183 }
184
185 # Returns the basename of the argument passed to it.
186 sub basename {
187         my $fn=shift;
188         
189         $fn=~s:^.*/(.*?)$:$1:;
190         return $fn;
191 }
192
193 # Returns the directory name of the argument passed to it.
194 sub dirname {
195         my $fn=shift;
196         
197         $fn=~s:^(.*)/.*?$:$1:;
198         return $fn;
199 }
200
201 # Pass in a number, will return true iff the current compatability level
202 # is less than or equal to that number.
203 sub compat {
204         my $num=shift;
205         
206         my $c=1;
207         if (defined $ENV{DH_COMPAT}) {
208                 $c=$ENV{DH_COMPAT};
209         }
210
211         if ($c > $max_compat) {
212                 error("Sorry, but $max_compat is the highest compatability level of debhelper currently supported.");
213         }
214
215         return ($c <= $num);
216 }
217
218 # Pass it a name of a binary package, it returns the name of the tmp dir to
219 # use, for that package.
220 sub tmpdir {
221         my $package=shift;
222
223         if ($dh{TMPDIR}) {
224                 return $dh{TMPDIR};
225         }
226         elsif (compat(1) && $package eq $dh{MAINPACKAGE}) {
227                 # This is for back-compatability with the debian/tmp tradition.
228                 return "debian/tmp";
229         }
230         else {
231                 return "debian/$package";
232         }
233 }
234
235 # Pass this the name of a binary package, and the name of the file wanted
236 # for the package, and it will return the actual existing filename to use.
237 #
238 # It tries several filenames:
239 #   * debian/package.filename.buildarch
240 #   * debian/package.filename
241 #   * debian/file (if the package is the main package)
242 sub pkgfile {
243         my $package=shift;
244         my $filename=shift;
245
246         if (-f "debian/$package.$filename.".buildarch()) {
247                 return "debian/$package.$filename.".buildarch();
248         }
249         elsif (-f "debian/$package.$filename") {
250                 return "debian/$package.$filename";
251         }
252         elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
253                 return "debian/$filename";
254         }
255         else {
256                 return "";
257         }
258 }
259
260 # Pass it a name of a binary package, it returns the name to prefix to files
261 # in debian for this package.
262 sub pkgext {
263         my $package=shift;
264
265         if ($package ne $dh{MAINPACKAGE}) {
266                 return "$package.";
267         }
268         return "";
269 }
270
271 # Returns 1 if the package is a native debian package, null otherwise.
272 # As a side effect, sets $dh{VERSION} to the version of this package.
273 {
274         # Caches return code so it only needs to run dpkg-parsechangelog once.
275         my %isnative_cache;
276         
277         sub isnative {
278                 my $package=shift;
279
280                 return $isnative_cache{$package} if defined $isnative_cache{$package};
281                 
282                 # Make sure we look at the correct changelog.
283                 my $isnative_changelog=pkgfile($package,"changelog");
284                 if (! $isnative_changelog) {
285                         $isnative_changelog="debian/changelog";
286                 }
287                 # Get the package version.
288                 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
289                 ($dh{VERSION})=$version=~m/Version:\s*(.*)/m;
290                 # Did the changelog parse fail?
291                 if (! defined $dh{VERSION}) {
292                         error("changelog parse failure");
293                 }
294
295                 # Is this a native Debian package?
296                 if ($dh{VERSION}=~m/.*-/) {
297                         return $isnative_cache{$package}=0;
298                 }
299                 else {
300                         return $isnative_cache{$package}=1;
301                 }
302         }
303 }
304
305 # Automatically add a shell script snippet to a debian script.
306 # Only works if the script has #DEBHELPER# in it.
307 #
308 # Parameters:
309 # 1: package
310 # 2: script to add to
311 # 3: filename of snippet
312 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
313 sub autoscript {
314         my $package=shift;
315         my $script=shift;
316         my $filename=shift;
317         my $sed=shift || "";
318
319         # This is the file we will append to.
320         my $outfile="debian/".pkgext($package)."$script.debhelper";
321
322         # Figure out what shell script snippet to use.
323         my $infile;
324         if (defined($ENV{DH_AUTOSCRIPTDIR}) && 
325             -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
326                 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
327         }
328         else {
329                 if (-e "/usr/share/debhelper/autoscripts/$filename") {
330                         $infile="/usr/share/debhelper/autoscripts/$filename";
331                 }
332                 else {
333                         error("/usr/share/debhelper/autoscripts/$filename does not exist");
334                 }
335         }
336
337         complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
338         complex_doit("sed \"$sed\" $infile >> $outfile");
339         complex_doit("echo '# End automatically added section' >> $outfile");
340 }
341
342 # Reads in the specified file, one word at a time, and returns an array of
343 # the result. If a value is passed in as the second parameter, then glob
344 # expansion is done in the directory specified by the parameter ("." is
345 # frequently a good choice).
346 sub filearray {
347         my $file=shift;
348         my $globdir=shift;
349
350         my @ret;
351         open (DH_FARRAY_IN, $file) || error("cannot read $file: $1");
352         while (<DH_FARRAY_IN>) {
353                 # Only do glob expansion in v3 mode.
354                 #
355                 # The tricky bit is that the glob expansion is done
356                 # as if we were in the specified directory, so the
357                 # filenames that come out are relative to it.
358                 if (defined $globdir && ! compat(2)) {
359                         for (map { glob "$globdir/$_" } split) {
360                                 s#^$globdir/##;
361                                 push @ret, $_;
362                         }
363                 }
364                 else {
365                         push @ret, split;
366                 }
367         }
368         close DH_FARRAY_IN;
369         
370         return @ret;
371 }
372
373 # Returns the build architecture. (Memoized)
374 {
375         my $arch;
376         
377         sub buildarch {
378                 return $arch if defined $arch;
379
380                 $arch=`dpkg --print-architecture` || error($!);
381                 chomp $arch;
382                 return $arch;
383         }
384 }
385
386 sub getcontrol {
387         return $ENV{DH_CONTROL} || 'debian/control';
388 }
389
390 # Returns a list of packages in the control file.
391 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
392 # -independant or same arch packages. If nothing is specified, returns all
393 # packages.
394 sub GetPackages {
395         my $type=shift;
396         
397         $type="" if ! defined $type;
398         
399         # Look up the build arch if we need to.
400         my $buildarch='';
401         if ($type eq 'same') {
402                 $buildarch=buildarch();
403         }
404
405         my $package="";
406         my $arch="";
407         my @list=();
408         open (CONTROL, getcontrol()) ||
409                 error("cannot read ".getcontrol().": $!\n");
410         while (<CONTROL>) {
411                 chomp;
412                 s/\s+$//;
413                 if (/^Package:\s*(.*)/) {
414                         $package=$1;
415                 }
416                 if (/^Architecture:\s*(.*)/) {
417                         $arch=$1;
418                 }
419                 if (!$_ or eof) { # end of stanza.
420                         if ($package &&
421                             (($type eq 'indep' && $arch eq 'all') ||
422                              ($type eq 'arch' && $arch ne 'all') ||
423                              ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
424                              ! $type)) {
425                                 push @list, $package;
426                                 $package="";
427                                 $arch="";
428                         }
429                 }
430         }
431         close CONTROL;
432
433         return @list;
434 }
435
436 1