]> git.donarmstrong.com Git - debhelper.git/blobdiff - Debian/Debhelper/Dh_Lib.pm
Allow individual debhelper programs to define their own special options by passing...
[debhelper.git] / Debian / Debhelper / Dh_Lib.pm
index f1b39226be061c4e7fa86475202f0885138b4e21..82efc5be241e7e506ea8a7a3b99e008fafe2c3c9 100644 (file)
@@ -2,7 +2,7 @@
 #
 # Library functions for debhelper programs, perl version.
 #
-# Joey Hess, GPL copyright 1997-2000.
+# Joey Hess, GPL copyright 1997-2008.
 
 package Debian::Debhelper::Dh_Lib;
 use strict;
@@ -14,11 +14,14 @@ use vars qw(@ISA @EXPORT %dh);
            &pkgfile &pkgext &pkgfilename &isnative &autoscript &filearray
            &filedoublearray &getpackages &basename &dirname &xargs %dh
            &compat &addsubstvar &delsubstvar &excludefile &package_arch
-           &is_udeb &udeb_filename &debhelper_script_subst &escape_shell);
+           &is_udeb &udeb_filename &debhelper_script_subst &escape_shell
+           &inhibit_log);
 
-my $max_compat=5;
+my $max_compat=7;
 
 sub init {
+       my %params=@_;
+
        # If DH_OPTIONS is set, prepend it @ARGV.
        if (defined($ENV{DH_OPTIONS})) {
                # Ignore leading/trailing whitespace.
@@ -41,7 +44,7 @@ sub init {
        if ($parseopt) {
                eval "use Debian::Debhelper::Dh_Getopt";
                error($!) if $@;
-               %dh=Debian::Debhelper::Dh_Getopt::parseopts();
+               Debian::Debhelper::Dh_Getopt::parseopts($params{options});
        }
 
        # Another way to set excludes.
@@ -55,7 +58,7 @@ sub init {
                foreach (@{$dh{EXCLUDE}}) {
                        my $x=$_;
                        $x=escape_shell($x);
-                       $x=~s/\./\\./g;
+                       $x=~s/\./\\\\./g;
                        $dh{EXCLUDE_FIND}.="-regex .\\*$x.\\* -or ";
                }
                $dh{EXCLUDE_FIND}=~s/ -or $//;
@@ -104,10 +107,30 @@ sub init {
        # If no error handling function was specified, just propigate
        # errors out.
        if (! exists $dh{ERROR_HANDLER} || ! defined $dh{ERROR_HANDLER}) {
-               $dh{ERROR_HANDLER}='exit $?';
+               $dh{ERROR_HANDLER}='exit \$?';
        }
 }
 
+# Run at exit. Add the command to the log files for the packages it acted
+# on, if it's exiting successfully.
+my $write_log=1;
+sub END {
+       if ($? == 0 && $write_log) {
+               my $cmd=basename($0);
+               foreach my $package (@{$dh{DOPACKAGES}}) {
+                       my $ext=pkgext($package);
+                       my $log="debian/${ext}debhelper.log";
+                       open(LOG, ">>", $log) || error("failed to write to ${log}: $!");
+                       print LOG $cmd."\n";
+                       close LOG;
+               }
+       }
+}
+
+sub inhibit_log {
+       $write_log=0;
+}
+
 # Pass it an array containing the arguments of a shell command like would
 # be run by exec(). It turns that into a line like you might enter at the
 # shell, escaping metacharacters and quoting arguments that contain spaces.
@@ -320,18 +343,22 @@ sub pkgfile {
                $filename="$dh{NAME}.$filename";
        }
        
-       if (-f "debian/$package.$filename.".buildarch()) {
-               return "debian/$package.$filename.".buildarch();
+       my @try=("debian/$package.$filename.".buildarch(),
+                "debian/$package.$filename");
+       if ($package eq $dh{MAINPACKAGE}) {
+               push @try, "debian/$filename";
        }
-       elsif (-f "debian/$package.$filename") {
-               return "debian/$package.$filename";
-       }
-       elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
-               return "debian/$filename";
-       }
-       else {
-               return "";
+       
+       foreach my $file (@try) {
+               if (-f $file &&
+                   (! $dh{IGNORE} || ! exists $dh{IGNORE}->{$file})) {
+                       return $file;
+               }
+
        }
+
+       return "";
+
 }
 
 # Pass it a name of a binary package, it returns the name to prefix to files
@@ -405,7 +432,7 @@ sub autoscript {
        my $filename=shift;
        my $sed=shift || "";
 
-       # This is the file we will append to.
+       # This is the file we will modify.
        my $outfile="debian/".pkgext($package)."$script.debhelper";
 
        # Figure out what shell script snippet to use.
@@ -423,9 +450,20 @@ sub autoscript {
                }
        }
 
-       complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
-       complex_doit("sed \"$sed\" $infile >> $outfile");
-       complex_doit("echo '# End automatically added section' >> $outfile");
+       if (-e $outfile && ($script eq 'postrm' || $script eq 'prerm')
+          && !compat(5)) {
+               # Add fragments to top so they run in reverse order when removing.
+               complex_doit("echo \"# Automatically added by ".basename($0)."\"> $outfile.new");
+               complex_doit("sed \"$sed\" $infile >> $outfile.new");
+               complex_doit("echo '# End automatically added section' >> $outfile.new");
+               complex_doit("cat $outfile >> $outfile.new");
+               complex_doit("mv $outfile.new $outfile");
+       }
+       else {
+               complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
+               complex_doit("sed \"$sed\" $infile >> $outfile");
+               complex_doit("echo '# End automatically added section' >> $outfile");
+       }
 }
 
 # Removes a whole substvar line.
@@ -477,7 +515,7 @@ sub addsubstvar {
                else {
                        delete $items{$str};
                }
-               $line=join(", ", keys %items);
+               $line=join(", ", sort keys %items);
        }
        elsif (! $remove) {
                $line=$str;
@@ -553,12 +591,24 @@ sub excludefile {
        sub buildarch {
                return $arch if defined $arch;
 
-               $arch=`dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null` || error($!);
+               $arch=`dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null` || error("dpkg-architecture failed");
                chomp $arch;
                return $arch;
        }
 }
 
+# Passed an arch and a list of arches to match against, returns true if matched
+sub samearch {
+       my $arch=shift;
+       my @archlist=split(/\s+/,shift);
+
+       foreach my $a (@archlist) {
+               system("dpkg-architecture", "-a$arch", "-i$a") == 0 && return 1;
+       }
+
+       return 0;
+}
+
 # Returns a list of packages in the control file.
 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
 # -independant or same arch packages. If nothing is specified, returns all
@@ -604,7 +654,7 @@ sub getpackages {
                if (/^Architecture:\s*(.*)/) {
                        $arch=$1;
                }
-               if (/^X[BC]*-Package-Type:\s*(.*)/) {
+               if (/^(?:X[BC]*-)?Package-Type:\s*(.*)/) {
                        $package_type=$1;
                }
                
@@ -616,7 +666,7 @@ sub getpackages {
                        if ($package &&
                            (($type eq 'indep' && $arch eq 'all') ||
                             ($type eq 'arch' && $arch ne 'all') ||
-                            ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
+                            ($type eq 'same' && ($arch eq 'any' || samearch($buildarch, $arch))) ||
                             ! $type)) {
                                push @list, $package;
                                $package="";
@@ -633,6 +683,10 @@ sub getpackages {
 sub package_arch {
        my $package=shift;
        
+       if (! exists $package_arches{$package}) {
+               warning "package $package is not in control info";
+               return buildarch();
+       }
        return $package_arches{$package} eq 'all' ? "all" : buildarch();
 }
 
@@ -640,6 +694,10 @@ sub package_arch {
 sub is_udeb {
        my $package=shift;
        
+       if (! exists $package_types{$package}) {
+               warning "package $package is not in control info";
+               return 0;
+       }
        return $package_types{$package} eq 'udeb';
 }