]> git.donarmstrong.com Git - debhelper.git/blob - dh_python
r1586: * dh_python: patch from Josselin to fix generated depends. Closes: #204717
[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>] [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.
31
32 If you use this program, your package should build-depend on python.
33
34 =head1 OPTIONS
35
36 =over 4
37
38 =item I<module dirs>
39
40 If your package installs python modules in non-standard directories, you
41 can make dh_python check those directories by passing their names on the
42 command line. By default, it will check /usr/lib/site-python,
43 /usr/lib/$PACKAGE, /usr/share/$PACKAGE, /usr/lib/games/$PACKAGE,
44 /usr/share/games/$PACKAGE and /usr/lib/python?.?/site-packages.
45
46 =item B<-n>, B<--noscripts>
47
48 Do not modify postinst/postrm scripts.
49
50 =back
51
52 =head1 CONFORMS TO
53
54 Debian policy, version 3.5.7
55
56 Python policy, version 0.3.7
57
58 =cut
59
60 init();
61
62 my $python = 'python';
63
64 # The current python major version
65 my $python_major;
66 my $python_version = `$python -V 2>&1`;
67 if ("$python_version" eq "") {
68         error("Python is not installed, aborting. (Probably forgot to Build-Depend on python.)");
69 }
70 elsif ($python_version =~ m/^Python\s+(\d+)\.(\d+)(\.\d+)*/) {
71         $python_version = "$1.$2" ;
72         $python_major = $1 ;
73 } else { 
74         error("Unable to parse python version out of \"$python_version\".");
75 }
76
77 # The next python version
78 my $python_nextversion = $python_version + 0.1;
79 my $python_nextmajor = $python_major + 1;
80
81 my @python_allversions = ('1.5','2.1','2.2','2.3');
82 foreach (@python_allversions) {
83         s/^/python/;
84 }
85
86 # Cleaning the paths given on the command line
87 foreach (@ARGV) {
88         s#/$##;
89         s#^/##;
90 }
91
92 # dependency types
93 use constant PROGRAM   => 1;
94 use constant PY_MODULE => 2;
95
96 foreach my $package (@{$dh{DOPACKAGES}}) {
97         my $tmp = tmpdir($package);
98
99         delsubstvar($package, "python:Depends");
100
101         my @dirs = ("usr/lib/site-python", "usr/lib/$package", "usr/share/$package", "usr/lib/games/$package", "usr/share/games/$package", @ARGV );
102
103         my $dep_on_python = 0;
104         my $strong_dep = 0;
105         my $look_for_pythonXY = 1;
106
107         # First, the case of python-foo
108         if ($package =~ /^python-/) {
109                 $dep_on_python = 1;
110                 $strong_dep = 1;
111                 my $pack = $package;
112                 $pack =~ s/^python/python$python_version/;
113                 if (grep { "$_" eq "$pack" } GetPackages()) {
114                         addsubstvar($package, "python:Depends", $pack);
115                 }
116                 else {
117                         push @dirs, "usr/lib/python$python_version/site-packages" ;
118                         $look_for_pythonXY = 0;
119                 }
120         }
121
122         @dirs = grep -d, map "$tmp/$_", @dirs;
123
124         my $deps = 0;
125         my %verdeps = ();
126         foreach (@python_allversions) {
127                 $verdeps{$_} = 0;
128         }
129
130         # Find scripts
131         find sub {
132                 return unless -f and (-x or /\.py$/);
133                 local *F;
134                 return unless open F, $_;
135                 if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
136                         if ( "python" eq $2 ) {
137                                 $deps |= PROGRAM;
138                         } elsif(defined $verdeps{$2}) {
139                                 $verdeps{$2} |= PROGRAM;
140                         }
141                 }
142                 close F;
143         }, $tmp;
144
145         # Look for python modules
146         my $dirlist="";
147         if (@dirs) {
148                 foreach my $curdir (@dirs) {
149                         my $has_module = 0;
150                         $curdir =~ s%^$tmp/%%;
151                         find sub {
152                                 return unless -f;
153                                 $has_module = 1 if /\.py$/;
154                         }, "$tmp/$curdir" ;
155                         if ($has_module) {
156                                 $deps |= PY_MODULE;
157                                 $dirlist="$dirlist /$curdir";
158                         }
159                 }
160         }
161
162         # Dependencies on current python
163         $dep_on_python = 1 if $deps;
164         $strong_dep = 1 if($deps & PY_MODULE);
165
166         if ($dep_on_python) {
167                 addsubstvar($package, "python:Depends", $python, ">= $python_version");
168                 if ($strong_dep) {
169                         addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
170                 } else {
171                         addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
172                 }
173         }
174
175         my $need_prerm = 0;
176
177         # Look for specific pythonX.Y modules
178         foreach my $pyver (@python_allversions) {
179                 my $pydir="/usr/lib/$pyver/site-packages";
180                 if ($look_for_pythonXY) {
181                         if (grep -d,"$tmp$pydir") {
182                                 find sub {
183                                         return unless -f;
184                                         $verdeps{$pyver} |= PY_MODULE if /\.py$/;
185                                 }, "$tmp$pydir";
186                         }
187                 }
188         
189                 # Go for the dependencies
190                 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
191
192                 # And now, the postinst and prerm stuff
193                 if ($pyver eq "python$python_version") {
194                         if ($verdeps{$pyver} & PY_MODULE) {
195                                 $pydir = $pydir.$dirlist;
196                         } else {
197                                 $pydir = $dirlist;
198                         }
199                         $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
200                 }
201                 if ($verdeps{$pyver} & PY_MODULE && ! $dh{NOSCRIPTS}) {
202                         autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
203                         $need_prerm = 1;
204                 }
205         }
206         if ($need_prerm && ! $dh{NOSCRIPTS}) {
207                 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
208         }
209 }
210
211 =head1 SEE ALSO
212
213 L<debhelper(7)>
214
215 This program is a part of debhelper.
216
217 =head1 AUTHOR
218
219 Josselin Mouette <joss@debian.org>
220
221 most ideas stolen from Brendan O'Dea <bod@debian.org>
222
223 =cut
224