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>] [B<-V> I<version>] [S<I<module dirs ...>>]
19 Note: This program is deprecated. You should use dh_pysupport or
20 dh_pycentral instead. This program will do nothing if debian/pycompat
23 dh_python is a debhelper program that is responsible for generating the
24 ${python:Depends} substitutions and adding them to substvars files. It
25 will also add a postinst and a 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 python, with the
29 current major version, or on pythonX.Y if your scripts or modules need a
30 specific python version. The dependency will be substituted into your
31 package's control file wherever you place the token "${python:Depends}".
33 If some modules need to be byte-compiled at install time, appropriate
34 postinst and 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 python.
45 If your package installs python modules in non-standard directories, you
46 can make dh_python check those directories by passing their names on the
47 command line. By default, it will check /usr/lib/site-python,
48 /usr/lib/$PACKAGE, /usr/share/$PACKAGE, /usr/lib/games/$PACKAGE,
49 /usr/share/games/$PACKAGE and /usr/lib/python?.?/site-packages.
51 Note: only /usr/lib/site-python, /usr/lib/python?.?/site-packages and the
52 extra names on the command line are searched for binary (.so) modules.
54 =item B<-V> I<version>
56 If the .py files your package ships are meant to be used by a specific
57 pythonX.Y version, you can use this option to specify the desired version,
58 such as 2.3. Do not use if you ship modules in /usr/lib/site-python.
60 =item B<-n>, B<--noscripts>
62 Do not modify postinst/postrm 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.");
81 warning("This program is deprecated, you should use dh_pysupport or dh_pycentral instead.");
83 my $python = 'python';
85 # The current python major version
87 my $python_version = `$python -V 2>&1`;
88 if (! defined $python_version || $python_version eq "") {
89 error("Python is not installed, aborting. (Probably forgot to Build-Depend on python.)");
91 elsif ($python_version =~ m/^Python\s+(\d+)\.(\d+)(\.\d+)*/) {
92 $python_version = "$1.$2" ;
95 error("Unable to parse python version out of \"$python_version\".");
98 # The next python version
99 my $python_nextversion = $python_version + 0.1;
100 my $python_nextmajor = $python_major + 1;
102 my @python_allversions = ('1.5','2.1','2.2','2.3','2.4');
103 foreach (@python_allversions) {
108 my $usepython = "python$python_version";
109 if($dh{V_FLAG_SET}) {
110 $usepython = $dh{V_FLAG};
111 $usepython =~ s/^/python/;
112 if (! grep { $_ eq $usepython } @python_allversions) {
113 error("Unknown python version $dh{V_FLAG}");
117 # Cleaning the paths given on the command line
124 use constant PROGRAM => 1;
125 use constant PY_MODULE => 2;
126 use constant PY_MODULE_NONSTANDARD => 4;
127 use constant SO_MODULE => 8;
128 use constant SO_MODULE_NONSTANDARD => 16;
130 foreach my $package (@{$dh{DOPACKAGES}}) {
131 my $tmp = tmpdir($package);
133 delsubstvar($package, "python:Depends");
135 my @dirs = ("usr/lib/site-python", "usr/lib/$package", "usr/share/$package", "usr/lib/games/$package", "usr/share/games/$package", @ARGV );
136 my @dirs_so = ("usr/lib/site-python", @ARGV );
138 my $dep_on_python = 0;
140 my $look_for_pythonXY = 1;
142 # First, the case of python-foo and pythonX.Y-foo
143 if ($package =~ /^python-/) {
147 $pack =~ s/^python/python$python_version/;
148 if (grep { "$_" eq "$pack" } getpackages()) {
149 addsubstvar($package, "python:Depends", $pack);
152 if ($package !~ /^python[0-9].[0-9]-/) {
153 push @dirs, "usr/lib/$usepython/site-packages";
154 push @dirs_so, "usr/lib/$usepython/site-packages";
155 $look_for_pythonXY = 0;
158 @dirs = grep -d, map "$tmp/$_", @dirs;
159 @dirs_so = grep -d, map "$tmp/$_", @dirs_so;
163 foreach (@python_allversions) {
169 return unless -f and (-x or /\.py$/);
171 return unless open F, $_;
172 if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
173 if ( "python" eq $2 ) {
175 } elsif(defined $verdeps{$2}) {
176 $verdeps{$2} |= PROGRAM;
182 # Look for python modules
185 foreach my $curdir (@dirs) {
187 $curdir =~ s%^$tmp/%%;
192 doit(("rm","-f",$_."c",$_."o"));
196 if ($dh{V_FLAG_SET}) {
197 $verdeps{$usepython} |= PY_MODULE_NONSTANDARD;
201 $dirlist="$dirlist /$curdir";
206 foreach my $curdir (@dirs_so) {
208 $curdir =~ s%^$tmp/%%;
211 $has_module = 1 if /\.so$/;
214 if ($dh{V_FLAG_SET}) {
215 $verdeps{$usepython} |= SO_MODULE_NONSTANDARD;
224 # Dependencies on current python
225 $dep_on_python = 1 if $deps;
226 $strong_dep = 1 if($deps & (PY_MODULE|SO_MODULE));
228 if ($dep_on_python) {
229 addsubstvar($package, "python:Depends", $python, ">= $python_version");
231 addsubstvar($package, "python:Depends", $python, "<< $python_nextversion");
233 addsubstvar($package, "python:Depends", $python, "<< $python_nextmajor");
239 # Look for specific pythonX.Y modules
240 foreach my $pyver (@python_allversions) {
241 my $pydir="/usr/lib/$pyver/site-packages";
242 if ($look_for_pythonXY) {
243 if (grep -d,"$tmp$pydir") {
247 $verdeps{$pyver} |= PY_MODULE;
248 doit(("rm","-f",$_."c",$_."o"));
250 $verdeps{$pyver} |= SO_MODULE if /\.so$/;
255 # Go for the dependencies
256 addsubstvar($package, "python:Depends", $pyver) if $verdeps{$pyver};
258 # And now, the postinst and prerm stuff
259 if ($pyver eq "$usepython") {
260 if ($verdeps{$pyver} & PY_MODULE) {
261 $pydir = $pydir.$dirlist;
265 $verdeps{$pyver} |= PY_MODULE if($deps & PY_MODULE);
267 if ($verdeps{$pyver} & (PY_MODULE|PY_MODULE_NONSTANDARD) && ! $dh{NOSCRIPTS}) {
268 autoscript($package,"postinst","postinst-python","s%#PYVER#%$pyver%;s%#DIRLIST#%$pydir%");
272 if ($need_prerm && ! $dh{NOSCRIPTS}) {
273 autoscript($package,"prerm","prerm-python","s%#PACKAGE#%$package%");
281 This program is a part of debhelper.
285 Josselin Mouette <joss@debian.org>
287 most ideas stolen from Brendan O'Dea <bod@debian.org>