]> git.donarmstrong.com Git - debhelper.git/blobdiff - Debian/Debhelper/Buildsystem/python_distutils.pm
python distutils buildsystem: When checking if a version of python is installed,...
[debhelper.git] / Debian / Debhelper / Buildsystem / python_distutils.pm
index 8266fa0b5a3ac4a04014b9f8c25cdc7e40332eab..81cff3c9fc8fe15689605f800964f585a0725dbd 100644 (file)
@@ -7,37 +7,6 @@
 
 package Debian::Debhelper::Buildsystem::python_distutils;
 
-=head1 NAME
-
-B<python_distutils> - Python Distutils (setup.py)
-
-=head1 SYNOPSIS
-
-B<dh_auto_*> [B<--buildsystem>=I<python_distutils>] ...
-
-=head1 DESCRIPTION
-
-Python Distribution Utilities (Distutils for short) is a standard Python build
-system. It is used to package most of the Python modules in the source
-distribution form. Typically, only two steps (build and install) are needed to
-finish installation of the Distutils based Python module. This build system can
-be typically identified by presence of the F<setup.py> in the source directory.
-
-=head1 DH_AUTO NOTES
-
-Out of source tree building is done by default but in source building is also
-supported. PLEASE NOTE that B<default build directory> is B<$srcdir/build>
-where $srcdir is a path to the source directory.
-
-Due to design flaws of Distutils, it is not possible to set a B<custom> build
-directory via command line arguments to F<setup.py>. Therefore, the same effect
-is achieved by writing appropriate F<.pydistutils.cfg> file to the build
-directory and pointing $HOME environment variable to the build directory.
-
-=head1 BUILD PROCESS
-
-=cut
-
 use strict;
 use Cwd ();
 use Debian::Debhelper::Dh_Lib qw(error);
@@ -49,20 +18,20 @@ sub DESCRIPTION {
 
 sub DEFAULT_BUILD_DIRECTORY {
        my $this=shift;
-       return $this->_canonpath($this->get_sourcepath("build"));
+       return $this->canonpath($this->get_sourcepath("build"));
 }
 
 sub new {
        my $class=shift;
        my $this=$class->SUPER::new(@_);
-       # Out of source tree building is prefered.
-       $this->enforce_out_of_source_building(@_);
+       # Out of source tree building is preferred.
+       $this->prefer_out_of_source_building(@_);
        return $this;
 }
 
 sub check_auto_buildable {
        my $this=shift;
-       return -e $this->get_sourcepath("setup.py");
+       return -e $this->get_sourcepath("setup.py") ? 1 : 0;
 }
 
 sub not_our_cfg {
@@ -70,7 +39,7 @@ sub not_our_cfg {
        my $ret;
        if (open(my $cfg, $this->get_buildpath(".pydistutils.cfg"))) {
                $ret = not "# Created by dh_auto\n" eq <$cfg>;
-               close DISTUTILSCFG;
+               close $cfg;
        }
        return $ret;
 }
@@ -92,10 +61,12 @@ sub pre_building_step {
 
        return unless grep /$step/, qw(build install clean);
 
-       # --build-base can only be passed to the build command. However,
-       # it is always read from the config file (really weird design).
-       # Therefore create such a cfg config file.
        if ($this->get_buildpath() ne $this->DEFAULT_BUILD_DIRECTORY()) {
+               # --build-base can only be passed to the build command. However,
+               # it is always read from the config file (really weird design).
+               # Therefore create such a cfg config file.
+               # See http://bugs.python.org/issue818201
+               #     http://bugs.python.org/issue1011113
                not $this->not_our_cfg() or
                    error("cannot set custom build directory: .pydistutils.cfg is in use");
                $this->mkdir_builddir();
@@ -104,112 +75,124 @@ sub pre_building_step {
                # Distutils reads $HOME/.pydistutils.cfg
                $ENV{HOME} = Cwd::abs_path($this->get_buildpath());
        }
+
+       $this->SUPER::pre_building_step($step);
 }
 
-sub setup_py {
+sub dbg_build_needed {
        my $this=shift;
        my $act=shift;
-       $this->doit_in_sourcedir("python", "setup.py", $act, @_);
-}
-
-=head2 Configure step
 
-=over 4
-
-=item I<Behaviour>
-
-Do nothing but stop auto-selection process.
-
-=item I<Auto-selection>
-
-If neither F<configure>, F<Makefile.PL> exist, but F<setup.py> exists in the
-source directory.
-
-=back
-
-=cut
-
-=head2 Build step
+       # Return a list of python-dbg package which are listed
+       # in the build-dependencies. This is kinda ugly, but building
+       # dbg extensions without checking if they're supposed to be
+       # built may result in various FTBFS if the package is not
+       # built in a clean chroot.
+
+       my @dbg;
+       open (CONTROL, 'debian/control') ||
+               error("cannot read debian/control: $!\n");
+       foreach my $builddeps (join('', <CONTROL>) =~ 
+                       /^Build-Depends[^:]*:.*\n(?:^[^\w\n].*\n)*/gmi) {
+               while ($builddeps =~ /(python[^, ]*-dbg)/g) {
+                       push @dbg, $1;
+               }
+       }
 
-=over 4
+       close CONTROL;
+       return @dbg;
 
-=item I<Behaviour>
+}
 
-Execute C<python setup.py build>.
+sub setup_py {
+       my $this=shift;
+       my $act=shift;
 
-=item I<Auto-selection>
+       # We need to to run setup.py with the default python last
+       # as distutils/setuptools modifies the shebang lines of scripts.
+       # This ensures that #!/usr/bin/python is installed last and
+       # not pythonX.Y
+       # Take into account that the default Python must not be in
+       # the requested Python versions.
+       # Then, run setup.py with each available python, to build
+       # extensions for each.
+
+       my $python_default = `pyversions -d`;
+       if ($? == -1) {
+               error("failed to run pyversions")
+       }
+       my $ecode = $? >> 8;
+       if ($ecode != 0) {
+               error("pyversions -d failed [$ecode]")
+       }
+       $python_default =~ s/^\s+//;
+       $python_default =~ s/\s+$//;
+       my @python_requested = split ' ', `pyversions -r`;
+       if ($? == -1) {
+               error("failed to run pyversions")
+       }
+       $ecode = $? >> 8;
+       if ($ecode != 0) {
+               error("pyversions -r failed [$ecode]")
+       }
+       if (grep /^\Q$python_default\E/, @python_requested) {
+               @python_requested = (
+                       grep(!/^\Q$python_default\E/, @python_requested),
+                       "python",
+               );
+       }
 
-If F<Makefile>, F<makefile> F<GNUmakefile> do not exist in the build directory
-and F<setup.py> file exists in the source directory.
+       my @python_dbg;
+       my @dbg_build_needed = $this->dbg_build_needed();
+       foreach my $python (map { $_."-dbg" } @python_requested) {
+               if (grep /^(python-all-dbg|\Q$python\E)/, @dbg_build_needed) {
+                       push @python_dbg, $python;
+               }
+               elsif (($python eq "python-dbg")
+                      and (grep /^\Q$python_default\E/, @dbg_build_needed)) {
+                       push @python_dbg, $python_default."-dbg";
+               }
+       }
 
-=back
+       foreach my $python (@python_dbg, @python_requested) {
+               if (-f "/usr/lib/$python/distutils/__init__.py") {
+                       # To allow backports of debhelper we don't pass
+                       # --install-layout=deb to 'setup.py install` for
+                       # those Python versions where the option is
+                       # ignored by distutils/setuptools.
+                       if ( $act eq "install" and not
+                            ( ($python =~ /^python(?:-dbg)?$/
+                                and $python_default =~ /^python2\.[2345]$/)
+                             or $python =~ /^python2\.[2345](?:-dbg)?$/ )) {
+                               $this->doit_in_sourcedir($python, "setup.py",
+                                               $act, @_, "--install-layout=deb");
+                       }
+                       else {
+                               $this->doit_in_sourcedir($python, "setup.py",
+                                               $act, @_);
+                       }
+               }
+       }
+}
 
-=cut
 sub build {
        my $this=shift;
-       $this->setup_py("build", @_);
+       $this->setup_py("build",
+               "--force",
+               @_);
 }
 
-=head2 Test step
-
-=over 4
-
-=item I<Behaviour>
-
-Do nothing but stop auto-selection process.
-
-=item I<Auto-selection>
-
-F<Makefile>, F<makefile>, F<GNUmakefile> do not exist in the build directory and
-F<setup.py> file exists in the source directory.
-
-=back
-
-=cut
-
-=head2 Install step
-
-=over 4
-
-=item I<Behaviour>
-
-Execute C<python setup.py install> passing temporary installation directory via
-C<--root> parameter. C<--no-compile> and C<-O0> parameters are also passed by
-default. See L<dh_auto_install(1)> for more information.
-
-=item I<Auto-selection>
-
-F<Makefile>, F<makefile>, F<GNUmakefile> do not exist in the build directory and
-F<setup.py> file exists in the source directory.
-
-=back
-
-=cut
 sub install {
        my $this=shift;
        my $destdir=shift;
-       $this->setup_py("install", "--root=$destdir", "--no-compile", "-O0", @_);
+       $this->setup_py("install",
+               "--force",
+               "--root=$destdir",
+               "--no-compile",
+               "-O0",
+               @_);
 }
 
-=head2 Clean step
-
-=over 4
-
-=item I<Behaviour>
-
-Execute C<python setup.py clean -a>. Additional parameters (if specified) are
-passed to the latter command. F<.pydistutils.cfg> is also removed if it was
-created (together with the build directory if it is ends up empty). Finally,
-recursively find and delete all *.pyc files from the source directory.
-
-=item I<Auto-selection>
-
-F<Makefile>, F<makefile>, F<GNUmakefile> do not exist in the build directory and
-F<setup.py> file exists in the source directory.
-
-=back
-
-=cut
 sub clean {
        my $this=shift;
        $this->setup_py("clean", "-a", @_);
@@ -224,15 +207,4 @@ sub clean {
        $this->doit_in_sourcedir('find', '.', '-name', '*.pyc', '-exec', 'rm', '{}', ';');
 }
 
-=head1 SEE ALSO
-
-L<dh_auto(7)>
-
-=head1 AUTHORS
-
- Joey Hess <joeyh@debian.org>
- Modestas Vainius <modestas@vainius.eu>
-
-=cut
-
-1;
+1