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