]> git.donarmstrong.com Git - debhelper.git/blob - Dh_Lib.pm
r157: 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             %dh);
16
17 sub init {
18         # If DH_OPTIONS is set, prepend it @ARGV.
19         if (defined($ENV{DH_OPTIONS})) {
20                 unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
21         }
22
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.
26         my $parseopt=undef;
27         my $arg;
28         foreach $arg (@ARGV) {
29                 if ($arg=~m/^-/) {
30                         $parseopt=1;
31                         last;
32                 }       
33         }
34         if ($parseopt) {
35                 eval "use Dh_Getopt";
36                 error($!) if $@;
37                 %dh=Dh_Getopt::parseopts();
38         }
39
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 "") {
43                 $dh{VERBOSE}=1;
44         }
45
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 "") {
49                 $dh{NO_ACT}=1;
50         }
51
52         # Get the name of the main binary package (first one listed in
53         # debian/control).
54         my @allpackages=GetPackages();
55         $dh{MAINPACKAGE}=$allpackages[0];
56
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");
64                 }
65                 push @{$dh{DOPACKAGES}},@allpackages;
66         }
67
68         # Check to see if -P was specified. If so, we can only act on a single
69         # package.
70         if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
71                 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
72         }
73
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];
78
79         # Split the U_PARAMS up into an array.
80         my $u=$dh{U_PARAMS};
81         undef $dh{U_PARAMS};
82         if (defined $u) {
83                 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
84         }
85 }
86
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;
91         return $word;
92 }
93
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 
96 # function.
97 #
98 # Note that this cannot handle complex commands, especially anything
99 # involving redirection. Use complex_doit instead.
100 sub doit {
101         verbose_print(join(" ",map { escape_shell($_) } @_));
102         
103         if (! $dh{NO_ACT}) {
104                 system(@_) == 0
105                         || error("command returned error code");
106                 
107         }
108 }
109
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
113 # like redirection.
114 sub complex_doit {
115         verbose_print(join(" ",@_));
116         
117         if (! $dh{NO_ACT}) {
118                 # The join makes system get a scalar so it forks off a shell.
119                 system(join(" ",@_)) == 0
120                         || error("command returned error code");
121         }                       
122 }
123
124 # Print something if the verbose flag is on.
125 sub verbose_print { my $message=shift;
126         if ($dh{VERBOSE}) {
127                 print "\t$message\n";
128         }
129 }
130
131 # Output an error message and exit.
132 sub error { my $message=shift;
133         warning($message);
134         exit 1;
135 }
136
137 # Output a warning.
138 sub warning { my $message=shift;
139         print STDERR basename($0).": $message\n";
140 }
141
142 # Returns the basename of the argument passed to it.
143 sub basename { my $fn=shift;
144         $fn=~s:^.*/(.*?)$:$1:;
145         return $fn;
146 }
147
148 # Returns the directory name of the argument passed to it.
149 sub dirname { my $fn=shift;
150         $fn=~s:^(.*)/.*?$:$1:;
151         return $fn;
152 }
153
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;
158         if ($dh{TMPDIR}) {
159                 return $dh{TMPDIR};
160         }
161         elsif ($package eq $dh{MAINPACKAGE}) {
162                 return "debian/tmp";
163         }
164         else {
165                 return "debian/$package";
166         }
167 }
168
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";
178         }
179         elsif ($package eq $dh{MAINPACKAGE} && -e "debian/$filename") {
180                 return "debian/$filename";
181         }
182         return "";
183 }
184
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}) {
189                 return "$package.";
190         }
191         return "";
192 }
193
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.
196 {
197         # Caches return code so it only needs to run dpkg-parsechangelog once.
198         my %isnative_cache;
199         
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";
206                         }
207
208                         # Get the package version.
209                         my $version=`dpkg-parsechangelog -l$isnative_changelog`;
210                         ($dh{VERSION})=$version=~m/Version: (.*)/m;
211
212                         # Is this a native Debian package?
213                         if ($dh{VERSION}=~m/.*-/) {
214                                 $isnative_cache{$package}=0;
215                         }
216                         else {
217                                 $isnative_cache{$package}=1;
218                         }
219                 }
220         
221                 return $isnative_cache{$package};
222         }
223 }
224
225 # Automatically add a shell script snippet to a debian script.
226 # Only works if the script has #DEBHELPER# in it.
227 #
228 # Parameters:
229 # 1: package
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";
236
237         # Figure out what shell script snippet to use.
238         my $infile;
239         if (defined($ENV{DH_AUTOSCRIPTDIR}) && 
240             -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
241                 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
242         }
243         else {
244                 if (-e "/usr/lib/debhelper/autoscripts/$filename") {
245                         $infile="/usr/lib/debhelper/autoscripts/$filename";
246                 }
247                 else {
248                         error("/usr/lib/debhelper/autoscripts/$filename does not exist");
249                 }
250         }
251
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");
256 }
257
258 # Reads in the specified file, one word at a time, and returns an array of
259 # the result.
260 sub filearray { my $file=shift;
261         my @ret;
262         open (DH_FARRAY_IN,"<$file") || error("cannot read $file: $1");
263         while (<DH_FARRAY_IN>) {
264                 push @ret,split(/\s/,$_);
265         }
266         close DH_FARRAY_IN;
267         
268         return @ret;
269 }
270
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
274 # packages.
275 sub GetPackages { my $type=shift;
276         $type="" if ! defined $type;
277         
278         # Look up the build arch if we need to.
279         my$buildarch='';
280         if ($type eq 'same') {
281                 $buildarch=`dpkg --print-architecture` || error($!);
282                 chomp $buildarch;
283         }
284
285         my $package="";
286         my $arch="";
287         my @list=();
288         open (CONTROL,"<debian/control") ||
289                 error("cannot read debian/control: $!\n");
290         while (<CONTROL>) {
291                 chomp;
292                 s/\s+$//;
293                 if (/^Package:\s+(.*)/) {
294                         $package=$1;
295                 }
296                 if (/^Architecture:\s+(.*)/) {
297                         $arch=$1;
298                 }
299                 if (!$_ or eof) { # end of stanza.
300                         if ($package &&
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/)) ||
304                              ! $type)) {
305                                 push @list, $package;
306                                 $package="";
307                                 $arch="";
308                         }
309                 }
310         }
311         close CONTROL;
312
313         return @list;
314 }
315
316 1