]> git.donarmstrong.com Git - debhelper.git/blob - dh_python
r1611: * dh_install: add --fail-missing option. Closes: #120026
[debhelper.git] / dh_python
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_python - calculates python dependencies and adds postinst and prerm python scripts
6
7 =cut
8
9 use strict;
10 use File::Find;
11 use Debian::Debhelper::Dh_Lib;
12
13 =head1 SYNOPSIS
14
15 B<dh_python> [S<I<debhelper options>>] [B<-n>] [B<-V> I<version>] [S<I<module dirs ...>>]
16
17 =head1 DESCRIPTION
18
19 dh_python is a debhelper program that is responsible for generating the
20 ${python:Depends} substitutions and adding them to substvars files. It
21 will also add a postinst and a prerm script if required.
22
23 The program will look at python scripts and modules in your package, and
24 will use this information to generate a dependency on python, with the
25 current major version, or on pythonX.Y if your scripts or modules need a
26 specific python version. The dependency will be substituted into your
27 package's control file wherever you place the token "${python:Depends}".
28
29 If some modules need to be byte-compiled at install time, appropriate
30 postinst and prerm scripts will be generated. If already byte-compiled
31 modules are found, they are removed.
32
33 If you use this program, your package should build-depend on python.
34
35 =head1 OPTIONS
36
37 =over 4
38
39 =item I<module dirs>
40
41 If your package installs python modules in non-standard directories, you
42 can make dh_python check those directories by passing their names on the
43 command line. By default, it will check /usr/lib/site-python,
44 /usr/lib/$PACKAGE, /usr/share/$PACKAGE, /usr/lib/games/$PACKAGE,
45 /usr/share/games/$PACKAGE and /usr/lib/python?.?/site-packages.
46
47 Note: only /usr/lib/site-python, /usr/lib/python?.?/site-packages and the
48 extra names on the command line are searched for binary (.so) modules.
49
50 =item B<-V> I<version>
51
52 If the .py files your package ships are meant to be used by a specific
53 pythonX.Y version, you can set this option with the desired X.Y python
54 version.
55 Do not use if you ship modules in /usr/lib/site-python.
56
57 =item B<-n>, B<--noscripts>
58
59 Do not modify postinst/postrm scripts.
60
61 =back
62
63 =head1 CONFORMS TO
64
65 Debian policy, version 3.5.7
66
67 Python policy, version 0.3.7
68
69 =cut
70
71 init();
72
73 my $python = 'python';
74
75 # The current python major version
76 my $python_major;
77 my $python_version = `$python -V 2>&1`;
78 if (! defined $python_version || $python_version eq "") {
79         error("Python is not installed, aborting. (Probably forgot to Build-Depend on python.)");
80 }
81 elsif ($python_version =~ m/^Python\s+(\d+)\.(\d+)(\.\d+)*/) {
82         $python_version = "$1.$2" ;
83         $python_major = $1 ;
84 } else { 
85         error("Unable to parse python version out of \"$python_version\".");
86 }
87
88 # The next python version
89 my $python_nextversion = $python_version + 0.1;
90 my $python_nextmajor = $python_major + 1;
91
92 my @python_allversions = ('1.5','2.1','2.2','2.3');
93 foreach (@python_allversions) {
94         s/^/python/;
95 }
96
97 # Check for -V
98 my $usepython = "python$python_version";
99 if($dh{V_FLAG_SET}) {
100         $usepython = $dh{V_FLAG};
101         $usepython =~ s/^/python/;
102 }
103
104 # Cleaning the paths given on the command line
105 foreach (@ARGV) {
106         s#/$##;
107         s#^/##;
108 }
109
110 # dependency types
111 use constant PROGRAM   => 1;
112 use constant PY_MODULE => 2;
113 use constant PY_MODULE_NONSTANDARD => 4;
114 use constant SO_MODULE => 8;
115 use constant SO_MODULE_NONSTANDARD => 16;
116
117 foreach my $package (@{$dh{DOPACKAGES}}) {
118         my $tmp = tmpdir($package);
119
120         delsubstvar($package, "python:Depends");
121
122         my @dirs = ("usr/lib/site-python", "usr/lib/$package", "usr/share/$package", "usr/lib/games/$package", "usr/share/games/$package", @ARGV );
123         my @dirs_so = ("usr/lib/site-python", @ARGV );
124
125         my $dep_on_python = 0;
126         my $strong_dep = 0;
127         my $look_for_pythonXY = 1;
128
129         # First, the case of python-foo and pythonX.Y-foo
130         if ($package =~ /^python-/) {
131                 $dep_on_python = 1;
132                 $strong_dep = 1;
133                 my $pack = $package;
134                 $pack =~ s/^python/python$python_version/;
135                 if (grep { "$_" eq "$pack" } GetPackages()) {
136                         addsubstvar($package, "python:Depends", $pack);
137                 }
138         }
139         if ($package !~ /^python[0-9].[0-9]-/) {
140                 push @dirs, "usr/lib/python$python_version/site-packages" ;
141                 push @dirs_so, "usr/lib/python$python_version/site-packages" ;
142                 $look_for_pythonXY = 0;
143         }
144
145         @dirs = grep -d, map "$tmp/$_", @dirs;
146         @dirs_so = grep -d, map "$tmp/$_", @dirs_so;
147
148         my $deps = 0;
149         my %verdeps = ();
150         foreach (@python_allversions) {
151                 $verdeps{$_} = 0;
152         }
153
154         # Find scripts
155         find sub {
156                 return unless -f and (-x or /\.py$/);
157                 local *F;
158                 return unless open F, $_;
159                 if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
160                         if ( "python" eq $2 ) {
161                                 $deps |= PROGRAM;
162                         } elsif(defined $verdeps{$2}) {
163                                 $verdeps{$2} |= PROGRAM;
164                         }
165                 }
166                 close F;
167         }, $tmp;
168
169         # Look for python modules
170         my $dirlist="";
171         if (@dirs) {
172                 foreach my $curdir (@dirs) {
173                         my $has_module = 0;
174                         $curdir =~ s%^$tmp/%%;
175                         find sub {
176                                 return unless -f;
177                                 if (/\.py$/) {
178                                         $has_module = 1;
179                                         doit(("rm","-f",$_."c",$_."o"));
180                                 }
181                         }, "$tmp/$curdir" ;
182                         if ($has_module) {
183                                 if ($dh{V_FLAG_SET}) {
184                                         $verdeps{$usepython} |= PY_MODULE_NONSTANDARD;
185                                 } else {
186                                         $deps |= PY_MODULE;
187                                 }
188                                 $dirlist="$dirlist /$curdir";
189                         }
190                 }
191         }
192         if (@dirs_so) {
193                 foreach my $curdir (@dirs_so) {
194                         my $has_module = 0;
195                         $curdir =~ s%^$tmp/%%;
196                         find sub {
197                                 return unless -f;
198                                 $has_module = 1 if /\.so$/;
199                         }, "$tmp/$curdir" ;
200                         if ($has_module) {
201                                 if ($dh{V_FLAG_SET}) {
202                                         $verdeps{$usepython} |= SO_MODULE_NONSTANDARD;
203                                 }
204                                 else {
205                                         $deps |= SO_MODULE;
206                                 }
207                         }
208                 }
209         }
210
211         # Dependencies on current python
212         $dep_on_python = 1 if $deps;
213         $strong_dep = 1 if($deps & (PY_MODULE|SO_MODULE));
214
215         if ($dep_on_python) {
216                 addsubstvar($package, "python:Depends", $python, ">= $python_version");
217                 if ($strong_dep) {
218                         addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
219                 } else {
220                         addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
221                 }
222         }
223
224         my $need_prerm = 0;
225
226         # Look for specific pythonX.Y modules
227         foreach my $pyver (@python_allversions) {
228                 my $pydir="/usr/lib/$pyver/site-packages";
229                 if ($look_for_pythonXY) {
230                         if (grep -d,"$tmp$pydir") {
231                                 find sub {
232                                         return unless -f;
233                                         if (/\.py$/) {
234                                                 $verdeps{$pyver} |= PY_MODULE;
235                                                 doit(("rm","-f",$_."c",$_."o"));
236                                         }
237                                         $verdeps{$pyver} |= SO_MODULE if /\.so$/;
238                                 }, "$tmp$pydir";
239                         }
240                 }
241         
242                 # Go for the dependencies
243                 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
244
245                 # And now, the postinst and prerm stuff
246                 if ($pyver eq "$usepython") {
247                         if ($verdeps{$pyver} & PY_MODULE) {
248                                 $pydir = $pydir.$dirlist;
249                         } else {
250                                 $pydir = $dirlist;
251                         }
252                         $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
253                 }
254                 if ($verdeps{$pyver} & (PY_MODULE|PY_MODULE_NONSTANDARD) && ! $dh{NOSCRIPTS}) {
255                         autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
256                         $need_prerm = 1;
257                 }
258         }
259         if ($need_prerm && ! $dh{NOSCRIPTS}) {
260                 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
261         }
262 }
263
264 =head1 SEE ALSO
265
266 L<debhelper(7)>
267
268 This program is a part of debhelper.
269
270 =head1 AUTHOR
271
272 Josselin Mouette <joss@debian.org>
273
274 most ideas stolen from Brendan O'Dea <bod@debian.org>
275
276 =cut