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