]> git.donarmstrong.com Git - debhelper.git/blob - dh_python
3bd2ac7aff427fed19a25257b99076c98fdac591
[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         $python_version = $dh{V_FLAG};
100         $usepython = $dh{V_FLAG};
101         $usepython =~ s/^/python/;
102         if (! grep { $_ eq $usepython } @python_allversions) {
103                 error("Unknown python version $dh{V_FLAG}");
104         }
105 }
106
107 # Cleaning the paths given on the command line
108 foreach (@ARGV) {
109         s#/$##;
110         s#^/##;
111 }
112
113 # dependency types
114 use constant PROGRAM   => 1;
115 use constant PY_MODULE => 2;
116 use constant PY_MODULE_NONSTANDARD => 4;
117 use constant SO_MODULE => 8;
118 use constant SO_MODULE_NONSTANDARD => 16;
119
120 foreach my $package (@{$dh{DOPACKAGES}}) {
121         my $tmp = tmpdir($package);
122
123         delsubstvar($package, "python:Depends");
124
125         my @dirs = ("usr/lib/site-python", "usr/lib/$package", "usr/share/$package", "usr/lib/games/$package", "usr/share/games/$package", @ARGV );
126         my @dirs_so = ("usr/lib/site-python", @ARGV );
127
128         my $dep_on_python = 0;
129         my $strong_dep = 0;
130         my $look_for_pythonXY = 1;
131
132         # First, the case of python-foo and pythonX.Y-foo
133         if ($package =~ /^python-/) {
134                 $dep_on_python = 1;
135                 $strong_dep = 1;
136                 my $pack = $package;
137                 $pack =~ s/^python/python$python_version/;
138                 if (grep { "$_" eq "$pack" } getpackages()) {
139                         addsubstvar($package, "python:Depends", $pack);
140                 }
141         }
142         if ($package !~ /^python[0-9].[0-9]-/) {
143                 push @dirs, "usr/lib/python$python_version/site-packages" ;
144                 push @dirs_so, "usr/lib/python$python_version/site-packages" ;
145                 $look_for_pythonXY = 0;
146         }
147
148         @dirs = grep -d, map "$tmp/$_", @dirs;
149         @dirs_so = grep -d, map "$tmp/$_", @dirs_so;
150
151         my $deps = 0;
152         my %verdeps = ();
153         foreach (@python_allversions) {
154                 $verdeps{$_} = 0;
155         }
156
157         # Find scripts
158         find sub {
159                 return unless -f and (-x or /\.py$/);
160                 local *F;
161                 return unless open F, $_;
162                 if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
163                         if ( "python" eq $2 ) {
164                                 $deps |= PROGRAM;
165                         } elsif(defined $verdeps{$2}) {
166                                 $verdeps{$2} |= PROGRAM;
167                         }
168                 }
169                 close F;
170         }, $tmp;
171
172         # Look for python modules
173         my $dirlist="";
174         if (@dirs) {
175                 foreach my $curdir (@dirs) {
176                         my $has_module = 0;
177                         $curdir =~ s%^$tmp/%%;
178                         find sub {
179                                 return unless -f;
180                                 if (/\.py$/) {
181                                         $has_module = 1;
182                                         doit(("rm","-f",$_."c",$_."o"));
183                                 }
184                         }, "$tmp/$curdir" ;
185                         if ($has_module) {
186                                 if ($dh{V_FLAG_SET}) {
187                                         $verdeps{$usepython} |= PY_MODULE_NONSTANDARD;
188                                 } else {
189                                         $deps |= PY_MODULE;
190                                 }
191                                 $dirlist="$dirlist /$curdir";
192                         }
193                 }
194         }
195         if (@dirs_so) {
196                 foreach my $curdir (@dirs_so) {
197                         my $has_module = 0;
198                         $curdir =~ s%^$tmp/%%;
199                         find sub {
200                                 return unless -f;
201                                 $has_module = 1 if /\.so$/;
202                         }, "$tmp/$curdir" ;
203                         if ($has_module) {
204                                 if ($dh{V_FLAG_SET}) {
205                                         $verdeps{$usepython} |= SO_MODULE_NONSTANDARD;
206                                 }
207                                 else {
208                                         $deps |= SO_MODULE;
209                                 }
210                         }
211                 }
212         }
213
214         # Dependencies on current python
215         $dep_on_python = 1 if $deps;
216         $strong_dep = 1 if($deps & (PY_MODULE|SO_MODULE));
217
218         if ($dep_on_python) {
219                 addsubstvar($package, "python:Depends", $python, ">= $python_version");
220                 if ($strong_dep) {
221                         addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
222                 } else {
223                         addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
224                 }
225         }
226
227         my $need_prerm = 0;
228
229         # Look for specific pythonX.Y modules
230         foreach my $pyver (@python_allversions) {
231                 my $pydir="/usr/lib/$pyver/site-packages";
232                 if ($look_for_pythonXY) {
233                         if (grep -d,"$tmp$pydir") {
234                                 find sub {
235                                         return unless -f;
236                                         if (/\.py$/) {
237                                                 $verdeps{$pyver} |= PY_MODULE;
238                                                 doit(("rm","-f",$_."c",$_."o"));
239                                         }
240                                         $verdeps{$pyver} |= SO_MODULE if /\.so$/;
241                                 }, "$tmp$pydir";
242                         }
243                 }
244         
245                 # Go for the dependencies
246                 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
247
248                 # And now, the postinst and prerm stuff
249                 if ($pyver eq "$usepython") {
250                         if ($verdeps{$pyver} & PY_MODULE) {
251                                 $pydir = $pydir.$dirlist;
252                         } else {
253                                 $pydir = $dirlist;
254                         }
255                         $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
256                 }
257                 if ($verdeps{$pyver} & (PY_MODULE|PY_MODULE_NONSTANDARD) && ! $dh{NOSCRIPTS}) {
258                         autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
259                         $need_prerm = 1;
260                 }
261         }
262         if ($need_prerm && ! $dh{NOSCRIPTS}) {
263                 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
264         }
265 }
266
267 =head1 SEE ALSO
268
269 L<debhelper(7)>
270
271 This program is a part of debhelper.
272
273 =head1 AUTHOR
274
275 Josselin Mouette <joss@debian.org>
276
277 most ideas stolen from Brendan O'Dea <bod@debian.org>
278
279 =cut