5 dh_python - calculates python dependencies and adds postinst and prerm python scripts
11 use Debian::Debhelper::Dh_Lib;
15 B<dh_python> [S<I<debhelper options>>] [B<-n>] [S<I<module dirs ...>>]
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.
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}".
29 If some modules need to be byte-compiled at build time, appropriate
30 postinst and prerm scripts will be generated.
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.
44 =item B<-n>, B<--noscripts>
46 Do not modify postinst/postrm scripts.
52 Debian policy, version 3.5.7
54 Python policy, version 0.3.7
60 my $python = 'python';
62 # The current python major version
65 if(`$python -V 2>&1` =~ m/^Python\s+(\d+)\.(\d+)\.\d+\n$/) {
66 $python_version = "$1.$2" ;
69 error("Cannot obtain python version.");
72 # The next python version
73 my $python_nextversion = $python_version + 0.1;
74 my $python_nextmajor = $python_major + 1;
76 my @python_allversions = ('1.5','2.1','2.2','2.3');
77 foreach (@python_allversions) {
81 # Cleaning the paths given on the command line
88 use constant PROGRAM => 1;
89 use constant PY_MODULE => 2;
91 foreach my $package (@{$dh{DOPACKAGES}}) {
92 my $tmp = tmpdir($package);
94 delsubstvar($package, "python:Depends");
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;
102 foreach (@python_allversions) {
108 return unless -f and (-x or /\.py$/);
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 ) {
114 } elsif(defined $verdeps{$2}) {
115 $verdeps{$2} |= PROGRAM;
121 # Look for python modules
124 foreach my $curdir (@dirs) {
126 $curdir =~ s%^$tmp/%%;
129 $has_module = 1 if /\.py$/;
133 $dirlist="$dirlist /$curdir";
138 # Dependencies on current python
139 my $dep_on_python = 0;
141 $dep_on_python = 1 if $deps;
142 $strong_dep = 1 if($deps & PY_MODULE);
143 if($package =~ /^python-/) {
145 $pack =~ s/^python/python$python_version/;
146 if(grep { "$_" eq "$pack" } @{$dh{DOPACKAGES}}) {
147 addsubstvar($package, "python:Depends", $pack);
153 addsubstvar($package, "python:Depends", $python, ">= $python_version");
155 addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
157 addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
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") {
169 $verdeps{$pyver} |= PY_MODULE if /\.py$/;
173 # Go for the dependencies
174 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
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;
183 $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
185 if($verdeps{$pyver} & PY_MODULE) {
186 autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
191 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
199 This program is a part of debhelper.
203 Josselin Mouette <joss@debian.org>
205 most ideas stolen from Brendan O'Dea <bod@debian.org>