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