5 dh_python - calculates Python dependencies and adds postinst and prerm Python scripts (deprecated)
11 use Debian::Debhelper::Dh_Lib;
15 B<dh_python> [S<I<debhelper options>>] [B<-n>] [B<-V> I<version>] [S<I<module dirs> ...>]
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.
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.
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}>.
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.
37 If you use this program, your package should build-depend on B<python>.
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>.
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.
54 =item B<-V> I<version>
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>.
60 =item B<-n>, B<--noscripts>
62 Do not modify F<postinst>/F<prerm> scripts.
68 Debian policy, version 3.5.7
70 Python policy, version 0.3.7
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.");
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.");
85 warning("This program is deprecated, you should use dh_python2 instead.");
88 my $python = 'python';
90 # The current python major version
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.)");
96 elsif ($python_version =~ m/^Python\s+(\d+)\.(\d+)(\.\d+)*/) {
97 $python_version = "$1.$2" ;
100 error("Unable to parse python version out of \"$python_version\".");
103 # The next python version
104 my $python_nextversion = $python_version + 0.1;
105 my $python_nextmajor = $python_major + 1;
107 my @python_allversions = ('1.5','2.1','2.2','2.3','2.4');
108 foreach (@python_allversions) {
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}");
122 # Cleaning the paths given on the command line
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;
135 foreach my $package (@{$dh{DOPACKAGES}}) {
136 my $tmp = tmpdir($package);
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 );
141 my $dep_on_python = 0;
143 my $look_for_pythonXY = 1;
145 # First, the case of python-foo and pythonX.Y-foo
146 if ($package =~ /^python-/) {
150 $pack =~ s/^python/python$python_version/;
151 if (grep { "$_" eq "$pack" } getpackages()) {
152 addsubstvar($package, "python:Depends", $pack);
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;
161 @dirs = grep -d, map "$tmp/$_", @dirs;
162 @dirs_so = grep -d, map "$tmp/$_", @dirs_so;
166 foreach (@python_allversions) {
172 return unless -f and (-x or /\.py$/);
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 ) {
178 } elsif(defined $verdeps{$2}) {
179 $verdeps{$2} |= PROGRAM;
185 # Look for python modules
188 foreach my $curdir (@dirs) {
190 $curdir =~ s%^$tmp/%%;
195 doit(("rm","-f",$_."c",$_."o"));
199 if ($dh{V_FLAG_SET}) {
200 $verdeps{$usepython} |= PY_MODULE_NONSTANDARD;
204 $dirlist="$dirlist /$curdir";
209 foreach my $curdir (@dirs_so) {
211 $curdir =~ s%^$tmp/%%;
214 $has_module = 1 if /\.so$/;
217 if ($dh{V_FLAG_SET}) {
218 $verdeps{$usepython} |= SO_MODULE_NONSTANDARD;
227 # Dependencies on current python
228 $dep_on_python = 1 if $deps;
229 $strong_dep = 1 if($deps & (PY_MODULE|SO_MODULE));
231 if ($dep_on_python) {
232 addsubstvar($package, "python:Depends", $python, ">= $python_version");
234 addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
236 addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
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") {
250 $verdeps{$pyver} |= PY_MODULE;
251 doit(("rm","-f",$_."c",$_."o"));
253 $verdeps{$pyver} |= SO_MODULE if /\.so$/;
258 # Go for the dependencies
259 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
261 # And now, the postinst and prerm stuff
262 if ($pyver eq "$usepython") {
263 if ($verdeps{$pyver} & PY_MODULE) {
264 $pydir = $pydir.$dirlist;
268 $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
270 if ($verdeps{$pyver} & (PY_MODULE|PY_MODULE_NONSTANDARD) && ! $dh{NOSCRIPTS}) {
271 autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
275 if ($need_prerm && ! $dh{NOSCRIPTS}) {
276 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
284 This program is a part of debhelper.
288 Josselin Mouette <joss@debian.org>
290 most ideas stolen from Brendan O'Dea <bod@debian.org>