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