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