3 # Library functions for debhelper programs, perl version.
5 # Joey Hess, GPL copyright 1997, 1998.
11 use vars qw(@ISA @EXPORT %dh);
13 @EXPORT=qw(&init &doit &complex_doit &verbose_print &error &warning &tmpdir
14 &pkgfile &pkgext &isnative &autoscript &filearray &GetPackages
21 # If DH_OPTIONS is set, prepend it @ARGV.
22 if (defined($ENV{DH_OPTIONS})) {
23 unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
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.
31 foreach $arg (@ARGV) {
40 %dh=Dh_Getopt::parseopts();
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 "") {
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 "") {
55 # Get the name of the main binary package (first one listed in
57 my @allpackages=GetPackages();
58 $dh{MAINPACKAGE}=$allpackages[0];
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");
68 push @{$dh{DOPACKAGES}},@allpackages;
71 # Check to see if -P was specified. If so, we can only act on a single
73 if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
74 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
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];
82 # Split the U_PARAMS up into an array.
86 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
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;
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
101 # Note that this cannot handle complex commands, especially anything
102 # involving redirection. Use complex_doit instead.
104 verbose_print(join(" ",map { escape_shell($_) } @_));
108 || error("command returned error code");
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
118 verbose_print(join(" ",@_));
121 # The join makes system get a scalar so it forks off a shell.
122 system(join(" ",@_)) == 0
123 || error("command returned error code");
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
134 # The kernel can accept command lines up to 20k worth of characters.
135 my $command_max=20000;
137 # Figure out length of static portion of command.
140 $static_length+=length($_)+1;
144 my $length=$static_length;
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? \"@_ $_\"");
149 $length+=length($_) + 1;
150 if ($length < $command_max) {
154 doit(@_,@collect) if $#collect > -1;
156 $length=$static_length;
159 doit(@_,@collect) if $#collect > -1;
162 # Print something if the verbose flag is on.
163 sub verbose_print { my $message=shift;
165 print "\t$message\n";
169 # Output an error message and exit.
170 sub error { my $message=shift;
176 sub warning { my $message=shift;
177 print STDERR basename($0).": $message\n";
180 # Returns the basename of the argument passed to it.
181 sub basename { my $fn=shift;
182 $fn=~s:^.*/(.*?)$:$1:;
186 # Returns the directory name of the argument passed to it.
187 sub dirname { my $fn=shift;
188 $fn=~s:^(.*)/.*?$:$1:;
192 # Pass in a number, will return true iff the current compatability level
193 # is equal to that number.
198 if (defined $ENV{DH_COMPAT}) {
202 if ($c > $max_compat) {
203 error("Sorry, but $max_compat is the highest compatability level of debhelper currently supported.");
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;
215 elsif (compat(1) && $package eq $dh{MAINPACKAGE}) {
216 # This is for back-compatability with the debian/tmp tradition.
220 return "debian/$package";
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 filename to use. For
226 # example if the package is foo, and the file is somefile, it will look for
227 # debian/somefile, and if found return that, otherwise, if the package is
228 # the main package, it will look for debian/foo, and if found, return that.
229 # Failing that, it will return nothing.
230 sub pkgfile { my $package=shift; my $filename=shift;
231 if (-f "debian/$package.$filename") {
232 return "debian/$package.$filename";
234 elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
235 return "debian/$filename";
240 # Pass it a name of a binary package, it returns the name to prefix to files
241 # in debian for this package.
242 sub pkgext { my $package=shift;
243 if ($package ne $dh{MAINPACKAGE}) {
249 # Returns 1 if the package is a native debian package, null otherwise.
250 # As a side effect, sets $dh{VERSION} to the version of this package.
252 # Caches return code so it only needs to run dpkg-parsechangelog once.
255 sub isnative { my $package=shift;
256 if (! defined $isnative_cache{$package}) {
257 # Make sure we look at the correct changelog.
258 my $isnative_changelog=pkgfile($package,"changelog");
259 if (! $isnative_changelog) {
260 $isnative_changelog="debian/changelog";
263 # Get the package version.
264 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
265 ($dh{VERSION})=$version=~m/Version: (.*)/m;
267 # Is this a native Debian package?
268 if ($dh{VERSION}=~m/.*-/) {
269 $isnative_cache{$package}=0;
272 $isnative_cache{$package}=1;
276 return $isnative_cache{$package};
280 # Automatically add a shell script snippet to a debian script.
281 # Only works if the script has #DEBHELPER# in it.
285 # 2: script to add to
286 # 3: filename of snippet
287 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
288 sub autoscript { my $package=shift; my $script=shift; my $filename=shift; my $sed=shift || "";
289 # This is the file we will append to.
290 my $outfile="debian/".pkgext($package)."$script.debhelper";
292 # Figure out what shell script snippet to use.
294 if (defined($ENV{DH_AUTOSCRIPTDIR}) &&
295 -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
296 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
299 if (-e "/usr/share/debhelper/autoscripts/$filename") {
300 $infile="/usr/share/debhelper/autoscripts/$filename";
303 error("/usr/share/debhelper/autoscripts/$filename does not exist");
307 # TODO: do this in perl, perhaps?
308 complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
309 complex_doit("sed \"$sed\" $infile >> $outfile");
310 complex_doit("echo '# End automatically added section' >> $outfile");
313 # Reads in the specified file, one word at a time, and returns an array of
315 sub filearray { my $file=shift;
317 open (DH_FARRAY_IN,"<$file") || error("cannot read $file: $1");
318 while (<DH_FARRAY_IN>) {
319 push @ret,split(' ',$_);
326 # Returns a list of packages in the control file.
327 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
328 # -independant or same arch packages. If nothing is specified, returns all
330 sub GetPackages { my $type=shift;
331 $type="" if ! defined $type;
333 # Look up the build arch if we need to.
335 if ($type eq 'same') {
336 $buildarch=`dpkg --print-architecture` || error($!);
343 open (CONTROL,"<debian/control") ||
344 error("cannot read debian/control: $!\n");
348 if (/^Package:\s+(.*)/) {
351 if (/^Architecture:\s+(.*)/) {
354 if (!$_ or eof) { # end of stanza.
356 (($type eq 'indep' && $arch eq 'all') ||
357 ($type eq 'arch' && $arch ne 'all') ||
358 ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
360 push @list, $package;