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