]> git.donarmstrong.com Git - debhelper.git/blobdiff - Dh_Lib.pm
r132: Initial Import
[debhelper.git] / Dh_Lib.pm
index c58d5c083d9ff8e25ddbf396401cf83930ffc393..61d807afc06fb798df705fef285c95d116d7b1d3 100644 (file)
--- a/Dh_Lib.pm
+++ b/Dh_Lib.pm
@@ -15,9 +15,14 @@ use vars qw(@ISA @EXPORT %dh);
            %dh);
 
 sub init {
+       # If DH_OPTIONS is set, prepend it @ARGV.
+       if (defined($ENV{DH_OPTIONS})) {
+               unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
+       }
+
        # Check to see if an argument on the command line starts with a dash.
-       # if so, we need to pass this off to the resource intensive Getopt::Long,
-       # which I'd prefer to avoid loading at all if possible.
+       # if so, we need to pass this off to the resource intensive 
+       # Getopt::Long, which I'd prefer to avoid loading at all if possible.
        my $parseopt=undef;
        my $arg;
        foreach $arg (@ARGV) {
@@ -49,10 +54,10 @@ sub init {
        my @allpackages=GetPackages();
        $dh{MAINPACKAGE}=$allpackages[0];
 
-       # Check if packages to build have been specified, if not, fall back to 
+       # Check if packages to build have been specified, if not, fall back to
        # the default, doing them all.
        if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
-               if ($dh{DOINDEP} || $dh{DOARCH}) {
+               if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
                        # User specified that all arch (in)dep package be 
                        # built, and there are none of that type.
                        error("I have no package to build");
@@ -70,6 +75,20 @@ sub init {
        # This package gets special treatement: files and directories specified on
        # the command line may affect it.
        $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
+
+       # Split the U_PARAMS up into an array.
+       my $u=$dh{U_PARAMS};
+       undef $dh{U_PARAMS};
+       if (defined $u) {
+               push @{$dh{U_PARAMS}}, split(/\s+/,$u);
+       }
+}
+
+# Escapes out shell metacharacters in a word of shell script.
+sub escape_shell { my $word=shift;
+       # This list is from _Unix in a Nutshell_. (except '#')
+       $word=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
+       return $word;
 }
 
 # Run a command, and display the command to stdout if verbose mode is on.
@@ -79,7 +98,7 @@ sub init {
 # Note that this cannot handle complex commands, especially anything
 # involving redirection. Use complex_doit instead.
 sub doit {
-       verbose_print(join(" ",@_));
+       verbose_print(join(" ",map { escape_shell($_) } @_));
        
        if (! $dh{NO_ACT}) {
                system(@_) == 0
@@ -117,13 +136,18 @@ sub error { my $message=shift;
 
 # Output a warning.
 sub warning { my $message=shift;
-       print STDERR basename().": $message\n";
+       print STDERR basename($0).": $message\n";
 }
 
-# Returns the basename of the program.
-sub basename {
-       my $fn=$0;
-       $fn=~s:.*/(.*?):$1:;
+# Returns the basename of the argument passed to it.
+sub basename { my $fn=shift;
+       $fn=~s:^.*/(.*?)$:$1:;
+       return $fn;
+}
+
+# Returns the directory name of the argument passed to it.
+sub dirname { my $fn=shift;
+       $fn=~s:^(.*)/.*?$:$1:;
        return $fn;
 }
 
@@ -171,30 +195,30 @@ sub pkgext { my $package=shift;
 # As a side effect, sets $dh{VERSION} to the version of this package.
 {
        # Caches return code so it only needs to run dpkg-parsechangelog once.
-       my $isnative_cache;
+       my %isnative_cache;
        
        sub isnative { my $package=shift;
-               if ($isnative_cache eq undef) {
+               if (! defined $isnative_cache{$package}) {
                        # Make sure we look at the correct changelog.
                        my $isnative_changelog=pkgfile($package,"changelog");
                        if (! $isnative_changelog) {
                                $isnative_changelog="debian/changelog";
                        }
-                       
+
                        # Get the package version.
                        my $version=`dpkg-parsechangelog -l$isnative_changelog`;
-                       ($dh{VERSION})=$version=~s/[^|\n]Version: \(.*\)\n//m;
-       
+                       ($dh{VERSION})=$version=~m/Version: (.*)/m;
+
                        # Is this a native Debian package?
                        if ($dh{VERSION}=~m/.*-/) {
-                               $isnative_cache=1;
+                               $isnative_cache{$package}=0;
                        }
                        else {
-                               $isnative_cache=0;
+                               $isnative_cache{$package}=1;
                        }
                }
        
-               return $isnative_cache;
+               return $isnative_cache{$package};
        }
 }
 
@@ -206,19 +230,18 @@ sub pkgext { my $package=shift;
 # 2: script to add to
 # 3: filename of snippet
 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
-sub autoscript { my $package=shift; my $script=shift; my $filename=shift; my $sed=shift;
-       error "autoscript() not yet implemented (lazy, lazy!)";
-
+sub autoscript { my $package=shift; my $script=shift; my $filename=shift; my $sed=shift || "";
        # This is the file we will append to.
        my $outfile="debian/".pkgext($package)."$script.debhelper";
 
        # Figure out what shell script snippet to use.
        my $infile;
-       if ( -e "$main::ENV{DH_AUTOSCRIPTDIR}/$filename" ) {
-               $infile="$main::ENV{DH_AUTOSCRIPTDIR}/$filename";
+       if (defined($ENV{DH_AUTOSCRIPTDIR}) && 
+           -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
+               $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
        }
        else {
-               if ( -e "/usr/lib/debhelper/autoscripts/$filename" ) {
+               if (-e "/usr/lib/debhelper/autoscripts/$filename") {
                        $infile="/usr/lib/debhelper/autoscripts/$filename";
                }
                else {
@@ -227,7 +250,7 @@ sub autoscript { my $package=shift; my $script=shift; my $filename=shift; my $se
        }
 
        # TODO: do this in perl, perhaps?
-       complex_doit("echo \"# Automatically added by ".basename().">> $outfile");
+       complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
        complex_doit("sed \"$sed\" $infile >> $outfile");
        complex_doit("echo '# End automatically added section' >> $outfile");
 }
@@ -246,14 +269,23 @@ sub filearray { my $file=shift;
 }
 
 # Returns a list of packages in the control file.
-# Must pass "arch" or "indep" to specify arch-dependant or -independant
-# packages. If nothing is specified, returns all packages.
+# Must pass "arch" or "indep" or "same" to specify arch-dependant or
+# -independant or same arch packages. If nothing is specified, returns all
+# packages.
 sub GetPackages { my $type=shift;
        $type="" if ! defined $type;
+       
+       # Look up the build arch if we need to.
+       my$buildarch='';
+       if ($type eq 'same') {
+               $buildarch=`dpkg --print-architecture` || error($!);
+               chomp $buildarch;
+       }
+
        my $package="";
        my $arch="";
        my @list=();
-       open (CONTROL,"<debian/control") || 
+       open (CONTROL,"<debian/control") ||
                error("cannot read debian/control: $!\n");
        while (<CONTROL>) {
                chomp;
@@ -268,6 +300,7 @@ sub GetPackages { my $type=shift;
                        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)) {
                                push @list, $package;
                                $package="";