]> git.donarmstrong.com Git - debhelper.git/blob - dh_python
cmake: Pass CPPFLAGS in CFLAGS. Closes: #668813 Thanks, Simon Ruderich for the patch...
[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 (deprecated)
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 B<dh_python2> instead.
20 This program will do nothing if F<debian/pycompat> or a
21 B<Python-Version> F<control> file field exists.
22
23 B<dh_python> is a debhelper program that is responsible for generating the
24 B<${python:Depends}> substitutions and adding them to substvars files. It
25 will also add a F<postinst> and a F<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 B<python>, with the
29 current major version, or on B<python>I<X>B<.>I<Y> if your scripts or modules need a
30 specific B<python> version. The dependency will be substituted into your
31 package's F<control> file wherever you place the token B<${python:Depends}>.
32
33 If some modules need to be byte-compiled at install time, appropriate
34 F<postinst> and F<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 B<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 F<dh_python> check those directories by passing their names on the
47 command line. By default, it will check F</usr/lib/site-python>,
48 F</usr/lib/$PACKAGE>, F</usr/share/$PACKAGE>, F</usr/lib/games/$PACKAGE>,
49 F</usr/share/games/$PACKAGE> and F</usr/lib/python?.?/site-packages>.
50
51 Note: only F</usr/lib/site-python>, F</usr/lib/python?.?/site-packages> and the
52 extra names on the command line are searched for binary (F<.so>) modules.
53
54 =item B<-V> I<version>
55
56 If the F<.py> files your package ships are meant to be used by a specific
57 B<python>I<X>B<.>I<Y> version, you can use this option to specify the desired version,
58 such as B<2.3>. Do not use if you ship modules in F</usr/lib/site-python>.
59
60 =item B<-n>, B<--noscripts>
61
62 Do not modify F<postinst>/F<prerm> 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_python2 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_python2 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         my @dirs = ("usr/lib/site-python", "usr/lib/$package", "usr/share/$package", "usr/lib/games/$package", "usr/share/games/$package", @ARGV );
139         my @dirs_so = ("usr/lib/site-python", @ARGV );
140
141         my $dep_on_python = 0;
142         my $strong_dep = 0;
143         my $look_for_pythonXY = 1;
144
145         # First, the case of python-foo and pythonX.Y-foo
146         if ($package =~ /^python-/) {
147                 $dep_on_python = 1;
148                 $strong_dep = 1;
149                 my $pack = $package;
150                 $pack =~ s/^python/python$python_version/;
151                 if (grep { "$_" eq "$pack" } getpackages()) {
152                         addsubstvar($package, "python:Depends", $pack);
153                 }
154         }
155         if ($package !~ /^python[0-9].[0-9]-/) {
156                 push @dirs, "usr/lib/$usepython/site-packages";
157                 push @dirs_so, "usr/lib/$usepython/site-packages";
158                 $look_for_pythonXY = 0;
159         }
160
161         @dirs = grep -d, map "$tmp/$_", @dirs;
162         @dirs_so = grep -d, map "$tmp/$_", @dirs_so;
163
164         my $deps = 0;
165         my %verdeps = ();
166         foreach (@python_allversions) {
167                 $verdeps{$_} = 0;
168         }
169
170         # Find scripts
171         find sub {
172                 return unless -f and (-x or /\.py$/);
173                 local *F;
174                 return unless open F, $_;
175                 if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
176                         if ( "python" eq $2 ) {
177                                 $deps |= PROGRAM;
178                         } elsif(defined $verdeps{$2}) {
179                                 $verdeps{$2} |= PROGRAM;
180                         }
181                 }
182                 close F;
183         }, $tmp;
184
185         # Look for python modules
186         my $dirlist="";
187         if (@dirs) {
188                 foreach my $curdir (@dirs) {
189                         my $has_module = 0;
190                         $curdir =~ s%^$tmp/%%;
191                         find sub {
192                                 return unless -f;
193                                 if (/\.py$/) {
194                                         $has_module = 1;
195                                         doit(("rm","-f",$_."c",$_."o"));
196                                 }
197                         }, "$tmp/$curdir" ;
198                         if ($has_module) {
199                                 if ($dh{V_FLAG_SET}) {
200                                         $verdeps{$usepython} |= PY_MODULE_NONSTANDARD;
201                                 } else {
202                                         $deps |= PY_MODULE;
203                                 }
204                                 $dirlist="$dirlist /$curdir";
205                         }
206                 }
207         }
208         if (@dirs_so) {
209                 foreach my $curdir (@dirs_so) {
210                         my $has_module = 0;
211                         $curdir =~ s%^$tmp/%%;
212                         find sub {
213                                 return unless -f;
214                                 $has_module = 1 if /\.so$/;
215                         }, "$tmp/$curdir" ;
216                         if ($has_module) {
217                                 if ($dh{V_FLAG_SET}) {
218                                         $verdeps{$usepython} |= SO_MODULE_NONSTANDARD;
219                                 }
220                                 else {
221                                         $deps |= SO_MODULE;
222                                 }
223                         }
224                 }
225         }
226
227         # Dependencies on current python
228         $dep_on_python = 1 if $deps;
229         $strong_dep = 1 if($deps & (PY_MODULE|SO_MODULE));
230
231         if ($dep_on_python) {
232                 addsubstvar($package, "python:Depends", $python, ">= $python_version");
233                 if ($strong_dep) {
234                         addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
235                 } else {
236                         addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
237                 }
238         }
239
240         my $need_prerm = 0;
241
242         # Look for specific pythonX.Y modules
243         foreach my $pyver (@python_allversions) {
244                 my $pydir="/usr/lib/$pyver/site-packages";
245                 if ($look_for_pythonXY) {
246                         if (grep -d,"$tmp$pydir") {
247                                 find sub {
248                                         return unless -f;
249                                         if (/\.py$/) {
250                                                 $verdeps{$pyver} |= PY_MODULE;
251                                                 doit(("rm","-f",$_."c",$_."o"));
252                                         }
253                                         $verdeps{$pyver} |= SO_MODULE if /\.so$/;
254                                 }, "$tmp$pydir";
255                         }
256                 }
257         
258                 # Go for the dependencies
259                 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
260
261                 # And now, the postinst and prerm stuff
262                 if ($pyver eq "$usepython") {
263                         if ($verdeps{$pyver} & PY_MODULE) {
264                                 $pydir = $pydir.$dirlist;
265                         } else {
266                                 $pydir = $dirlist;
267                         }
268                         $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
269                 }
270                 if ($verdeps{$pyver} & (PY_MODULE|PY_MODULE_NONSTANDARD) && ! $dh{NOSCRIPTS}) {
271                         autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
272                         $need_prerm = 1;
273                 }
274         }
275         if ($need_prerm && ! $dh{NOSCRIPTS}) {
276                 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
277         }
278 }
279
280 =head1 SEE ALSO
281
282 L<debhelper(7)>
283
284 This program is a part of debhelper.
285
286 =head1 AUTHOR
287
288 Josselin Mouette <joss@debian.org>
289
290 most ideas stolen from Brendan O'Dea <bod@debian.org>
291
292 =cut