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
18 # If DH_OPTIONS is set, prepend it @ARGV.
19 if (defined($ENV{DH_OPTIONS})) {
20 unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
23 # Check to see if an argument on the command line starts with a dash.
24 # if so, we need to pass this off to the resource intensive
25 # Getopt::Long, which I'd prefer to avoid loading at all if possible.
28 foreach $arg (@ARGV) {
37 %dh=Dh_Getopt::parseopts();
40 # Check to see if DH_VERBOSE environment variable was set, if so,
41 # make sure verbose is on.
42 if (defined $ENV{DH_VERBOSE} && $ENV{DH_VERBOSE} ne "") {
46 # Check to see if DH_NO_ACT environment variable was set, if so,
47 # make sure no act mode is on.
48 if (defined $ENV{DH_NO_ACT} && $ENV{DH_NO_ACT} ne "") {
52 # Get the name of the main binary package (first one listed in
54 my @allpackages=GetPackages();
55 $dh{MAINPACKAGE}=$allpackages[0];
57 # Check if packages to build have been specified, if not, fall back to
58 # the default, doing them all.
59 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
60 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
61 # User specified that all arch (in)dep package be
62 # built, and there are none of that type.
63 error("I have no package to act on");
65 push @{$dh{DOPACKAGES}},@allpackages;
68 # Check to see if -P was specified. If so, we can only act on a single
70 if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
71 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
74 # Figure out which package is the first one we were instructed to build.
75 # This package gets special treatement: files and directories specified on
76 # the command line may affect it.
77 $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
79 # Split the U_PARAMS up into an array.
83 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
87 # Escapes out shell metacharacters in a word of shell script.
88 sub escape_shell { my $word=shift;
89 # This list is from _Unix in a Nutshell_. (except '#')
90 $word=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
94 # Run a command, and display the command to stdout if verbose mode is on.
95 # All commands that modifiy files in $TMP should be ran via this
98 # Note that this cannot handle complex commands, especially anything
99 # involving redirection. Use complex_doit instead.
101 verbose_print(join(" ",map { escape_shell($_) } @_));
105 || error("command returned error code");
110 # Run a command and display the command to stdout if verbose mode is on.
111 # Use doit() if you can, instead of this function, because this function
112 # forks a shell. However, this function can handle more complicated stuff
115 verbose_print(join(" ",@_));
118 # The join makes system get a scalar so it forks off a shell.
119 system(join(" ",@_)) == 0
120 || error("command returned error code");
124 # Print something if the verbose flag is on.
125 sub verbose_print { my $message=shift;
127 print "\t$message\n";
131 # Output an error message and exit.
132 sub error { my $message=shift;
138 sub warning { my $message=shift;
139 print STDERR basename($0).": $message\n";
142 # Returns the basename of the argument passed to it.
143 sub basename { my $fn=shift;
144 $fn=~s:^.*/(.*?)$:$1:;
148 # Returns the directory name of the argument passed to it.
149 sub dirname { my $fn=shift;
150 $fn=~s:^(.*)/.*?$:$1:;
154 # Pass it a name of a binary package, it returns the name of the tmp dir to
155 # use, for that package.
156 # This is for back-compatability with the debian/tmp tradition.
157 sub tmpdir { my $package=shift;
161 elsif ($package eq $dh{MAINPACKAGE}) {
165 return "debian/$package";
169 # Pass this the name of a binary package, and the name of the file wanted
170 # for the package, and it will return the actual filename to use. For
171 # example if the package is foo, and the file is somefile, it will look for
172 # debian/somefile, and if found return that, otherwise, if the package is
173 # the main package, it will look for debian/foo, and if found, return that.
174 # Failing that, it will return nothing.
175 sub pkgfile { my $package=shift; my $filename=shift;
176 if (-e "debian/$package.$filename") {
177 return "debian/$package.$filename";
179 elsif ($package eq $dh{MAINPACKAGE} && -e "debian/$filename") {
180 return "debian/$filename";
185 # Pass it a name of a binary package, it returns the name to prefix to files
186 # in debian for this package.
187 sub pkgext { my $package=shift;
188 if ($package ne $dh{MAINPACKAGE}) {
194 # Returns 1 if the package is a native debian package, null otherwise.
195 # As a side effect, sets $dh{VERSION} to the version of this package.
197 # Caches return code so it only needs to run dpkg-parsechangelog once.
200 sub isnative { my $package=shift;
201 if (! defined $isnative_cache{$package}) {
202 # Make sure we look at the correct changelog.
203 my $isnative_changelog=pkgfile($package,"changelog");
204 if (! $isnative_changelog) {
205 $isnative_changelog="debian/changelog";
208 # Get the package version.
209 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
210 ($dh{VERSION})=$version=~m/Version: (.*)/m;
212 # Is this a native Debian package?
213 if ($dh{VERSION}=~m/.*-/) {
214 $isnative_cache{$package}=0;
217 $isnative_cache{$package}=1;
221 return $isnative_cache{$package};
225 # Automatically add a shell script snippet to a debian script.
226 # Only works if the script has #DEBHELPER# in it.
230 # 2: script to add to
231 # 3: filename of snippet
232 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
233 sub autoscript { my $package=shift; my $script=shift; my $filename=shift; my $sed=shift || "";
234 # This is the file we will append to.
235 my $outfile="debian/".pkgext($package)."$script.debhelper";
237 # Figure out what shell script snippet to use.
239 if (defined($ENV{DH_AUTOSCRIPTDIR}) &&
240 -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
241 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
244 if (-e "/usr/lib/debhelper/autoscripts/$filename") {
245 $infile="/usr/lib/debhelper/autoscripts/$filename";
248 error("/usr/lib/debhelper/autoscripts/$filename does not exist");
252 # TODO: do this in perl, perhaps?
253 complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
254 complex_doit("sed \"$sed\" $infile >> $outfile");
255 complex_doit("echo '# End automatically added section' >> $outfile");
258 # Reads in the specified file, one word at a time, and returns an array of
260 sub filearray { my $file=shift;
262 open (DH_FARRAY_IN,"<$file") || error("cannot read $file: $1");
263 while (<DH_FARRAY_IN>) {
264 push @ret,split(/\s/,$_);
271 # Returns a list of packages in the control file.
272 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
273 # -independant or same arch packages. If nothing is specified, returns all
275 sub GetPackages { my $type=shift;
276 $type="" if ! defined $type;
278 # Look up the build arch if we need to.
280 if ($type eq 'same') {
281 $buildarch=`dpkg --print-architecture` || error($!);
288 open (CONTROL,"<debian/control") ||
289 error("cannot read debian/control: $!\n");
293 if (/^Package:\s+(.*)/) {
296 if (/^Architecture:\s+(.*)/) {
299 if (!$_ or eof) { # end of stanza.
301 (($type eq 'indep' && $arch eq 'all') ||
302 ($type eq 'arch' && $arch ne 'all') ||
303 ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
305 push @list, $package;