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