]> git.donarmstrong.com Git - debhelper.git/blob - dh_python
r1762: * Add another test-case for dh_link.
[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 use this option to specify the desired version,
54 such as 2.3. Do not use if you ship modules in /usr/lib/site-python.
55
56 =item B<-n>, B<--noscripts>
57
58 Do not modify postinst/postrm scripts.
59
60 =back
61
62 =head1 CONFORMS TO
63
64 Debian policy, version 3.5.7
65
66 Python policy, version 0.3.7
67
68 =cut
69
70 init();
71
72 my $python = 'python';
73
74 # The current python major version
75 my $python_major;
76 my $python_version = `$python -V 2>&1`;
77 if (! defined $python_version || $python_version eq "") {
78         error("Python is not installed, aborting. (Probably forgot to Build-Depend on python.)");
79 }
80 elsif ($python_version =~ m/^Python\s+(\d+)\.(\d+)(\.\d+)*/) {
81         $python_version = "$1.$2" ;
82         $python_major = $1 ;
83 } else { 
84         error("Unable to parse python version out of \"$python_version\".");
85 }
86
87 # The next python version
88 my $python_nextversion = $python_version + 0.1;
89 my $python_nextmajor = $python_major + 1;
90
91 my @python_allversions = ('1.5','2.1','2.2','2.3','2.4');
92 foreach (@python_allversions) {
93         s/^/python/;
94 }
95
96 # Check for -V
97 my $usepython = "python$python_version";
98 if($dh{V_FLAG_SET}) {
99         $usepython = $dh{V_FLAG};
100         $usepython =~ s/^/python/;
101         if (! grep { $_ eq $usepython } @python_allversions) {
102                 error("Unknown python version $dh{V_FLAG}");
103         }
104 }
105
106 # Cleaning the paths given on the command line
107 foreach (@ARGV) {
108         s#/$##;
109         s#^/##;
110 }
111
112 # dependency types
113 use constant PROGRAM   => 1;
114 use constant PY_MODULE => 2;
115 use constant PY_MODULE_NONSTANDARD => 4;
116 use constant SO_MODULE => 8;
117 use constant SO_MODULE_NONSTANDARD => 16;
118
119 foreach my $package (@{$dh{DOPACKAGES}}) {
120         my $tmp = tmpdir($package);
121
122         delsubstvar($package, "python:Depends");
123
124         my @dirs = ("usr/lib/site-python", "usr/lib/$package", "usr/share/$package", "usr/lib/games/$package", "usr/share/games/$package", @ARGV );
125         my @dirs_so = ("usr/lib/site-python", @ARGV );
126
127         my $dep_on_python = 0;
128         my $strong_dep = 0;
129         my $look_for_pythonXY = 1;
130
131         # First, the case of python-foo and pythonX.Y-foo
132         if ($package =~ /^python-/) {
133                 $dep_on_python = 1;
134                 $strong_dep = 1;
135                 my $pack = $package;
136                 $pack =~ s/^python/python$python_version/;
137                 if (grep { "$_" eq "$pack" } getpackages()) {
138                         addsubstvar($package, "python:Depends", $pack);
139                 }
140         }
141         if ($package !~ /^python[0-9].[0-9]-/) {
142                 push @dirs, "usr/lib/$usepython/site-packages";
143                 push @dirs_so, "usr/lib/$usepython/site-packages";
144                 $look_for_pythonXY = 0;
145         }
146
147         @dirs = grep -d, map "$tmp/$_", @dirs;
148         @dirs_so = grep -d, map "$tmp/$_", @dirs_so;
149
150         my $deps = 0;
151         my %verdeps = ();
152         foreach (@python_allversions) {
153                 $verdeps{$_} = 0;
154         }
155
156         # Find scripts
157         find sub {
158                 return unless -f and (-x or /\.py$/);
159                 local *F;
160                 return unless open F, $_;
161                 if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
162                         if ( "python" eq $2 ) {
163                                 $deps |= PROGRAM;
164                         } elsif(defined $verdeps{$2}) {
165                                 $verdeps{$2} |= PROGRAM;
166                         }
167                 }
168                 close F;
169         }, $tmp;
170
171         # Look for python modules
172         my $dirlist="";
173         if (@dirs) {
174                 foreach my $curdir (@dirs) {
175                         my $has_module = 0;
176                         $curdir =~ s%^$tmp/%%;
177                         find sub {
178                                 return unless -f;
179                                 if (/\.py$/) {
180                                         $has_module = 1;
181                                         doit(("rm","-f",$_."c",$_."o"));
182                                 }
183                         }, "$tmp/$curdir" ;
184                         if ($has_module) {
185                                 if ($dh{V_FLAG_SET}) {
186                                         $verdeps{$usepython} |= PY_MODULE_NONSTANDARD;
187                                 } else {
188                                         $deps |= PY_MODULE;
189                                 }
190                                 $dirlist="$dirlist /$curdir";
191                         }
192                 }
193         }
194         if (@dirs_so) {
195                 foreach my $curdir (@dirs_so) {
196                         my $has_module = 0;
197                         $curdir =~ s%^$tmp/%%;
198                         find sub {
199                                 return unless -f;
200                                 $has_module = 1 if /\.so$/;
201                         }, "$tmp/$curdir" ;
202                         if ($has_module) {
203                                 if ($dh{V_FLAG_SET}) {
204                                         $verdeps{$usepython} |= SO_MODULE_NONSTANDARD;
205                                 }
206                                 else {
207                                         $deps |= SO_MODULE;
208                                 }
209                         }
210                 }
211         }
212
213         # Dependencies on current python
214         $dep_on_python = 1 if $deps;
215         $strong_dep = 1 if($deps & (PY_MODULE|SO_MODULE));
216
217         if ($dep_on_python) {
218                 addsubstvar($package, "python:Depends", $python, ">= $python_version");
219                 if ($strong_dep) {
220                         addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
221                 } else {
222                         addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
223                 }
224         }
225
226         my $need_prerm = 0;
227
228         # Look for specific pythonX.Y modules
229         foreach my $pyver (@python_allversions) {
230                 my $pydir="/usr/lib/$pyver/site-packages";
231                 if ($look_for_pythonXY) {
232                         if (grep -d,"$tmp$pydir") {
233                                 find sub {
234                                         return unless -f;
235                                         if (/\.py$/) {
236                                                 $verdeps{$pyver} |= PY_MODULE;
237                                                 doit(("rm","-f",$_."c",$_."o"));
238                                         }
239                                         $verdeps{$pyver} |= SO_MODULE if /\.so$/;
240                                 }, "$tmp$pydir";
241                         }
242                 }
243         
244                 # Go for the dependencies
245                 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
246
247                 # And now, the postinst and prerm stuff
248                 if ($pyver eq "$usepython") {
249                         if ($verdeps{$pyver} & PY_MODULE) {
250                                 $pydir = $pydir.$dirlist;
251                         } else {
252                                 $pydir = $dirlist;
253                         }
254                         $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
255                 }
256                 if ($verdeps{$pyver} & (PY_MODULE|PY_MODULE_NONSTANDARD) && ! $dh{NOSCRIPTS}) {
257                         autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
258                         $need_prerm = 1;
259                 }
260         }
261         if ($need_prerm && ! $dh{NOSCRIPTS}) {
262                 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
263         }
264 }
265
266 =head1 SEE ALSO
267
268 L<debhelper(7)>
269
270 This program is a part of debhelper.
271
272 =head1 AUTHOR
273
274 Josselin Mouette <joss@debian.org>
275
276 most ideas stolen from Brendan O'Dea <bod@debian.org>
277
278 =cut