1 # A debhelper build system class for building Python Distutils based
2 # projects. It prefers out of source tree building.
4 # Copyright: © 2008 Joey Hess
5 # © 2008-2009 Modestas Vainius
8 package Debian::Debhelper::Buildsystem::python_distutils;
12 use Debian::Debhelper::Dh_Lib qw(error);
13 use base 'Debian::Debhelper::Buildsystem';
16 "Python Distutils (setup.py)"
19 sub DEFAULT_BUILD_DIRECTORY {
21 return $this->canonpath($this->get_sourcepath("build"));
26 my $this=$class->SUPER::new(@_);
27 # Out of source tree building is prefered.
28 $this->prefer_out_of_source_building(@_);
32 sub check_auto_buildable {
34 return -e $this->get_sourcepath("setup.py");
40 if (open(my $cfg, $this->get_buildpath(".pydistutils.cfg"))) {
41 $ret = not "# Created by dh_auto\n" eq <$cfg>;
49 if (open(my $cfg, ">", $this->get_buildpath(".pydistutils.cfg"))) {
50 print $cfg "# Created by dh_auto", "\n";
51 print $cfg "[build]\nbuild-base=", $this->get_build_rel2sourcedir(), "\n";
58 sub pre_building_step {
62 return unless grep /$step/, qw(build install clean);
64 if ($this->get_buildpath() ne $this->DEFAULT_BUILD_DIRECTORY()) {
65 # --build-base can only be passed to the build command. However,
66 # it is always read from the config file (really weird design).
67 # Therefore create such a cfg config file.
68 # See http://bugs.python.org/issue818201
69 # http://bugs.python.org/issue1011113
70 not $this->not_our_cfg() or
71 error("cannot set custom build directory: .pydistutils.cfg is in use");
72 $this->mkdir_builddir();
73 $this->create_cfg() or
74 error("cannot set custom build directory: unwritable .pydistutils.cfg");
75 # Distutils reads $HOME/.pydistutils.cfg
76 $ENV{HOME} = Cwd::abs_path($this->get_buildpath());
79 $this->SUPER::pre_building_step($step);
82 sub dbg_build_needed {
86 # Return a list of python-dbg package which are listed
87 # in the build-dependencies. This is kinda ugly, but building
88 # dbg extensions without checking if they're supposed to be
89 # built may result in various FTBFS if the package is not
90 # built in a clean chroot.
93 open (CONTROL, 'debian/control') ||
94 error("cannot read debian/control: $!\n");
95 foreach my $builddeps (join('', <CONTROL>) =~
96 /^Build-Depends[^:]*:.*\n(?:^[^\w\n].*\n)*/gmi) {
97 while ($builddeps =~ /(python[^, ]*-dbg)/g) {
111 # We need to to run setup.py with the default python first
112 # as distutils/setuptools modifies the shebang lines of scripts.
113 # This ensures that #!/usr/bin/python is used and not pythonX.Y
114 # Take into account that the default Python must not be in
115 # the requested Python versions.
116 # Then, run setup.py with each available python, to build
117 # extensions for each.
119 my $python_default = `pyversions -d`;
120 $python_default =~ s/^\s+//;
121 $python_default =~ s/\s+$//;
122 my @python_requested = split ' ', `pyversions -r 2>/dev/null`;
123 if (grep /^\Q$python_default\E/, @python_requested) {
124 @python_requested = ("python", grep(!/^\Q$python_default\E/,
129 my @dbg_build_needed = $this->dbg_build_needed();
130 foreach my $python (map { $_."-dbg" } @python_requested) {
131 if (grep /^(python-all-dbg|\Q$python\E)/, @dbg_build_needed) {
132 push @python_dbg, $python;
134 elsif (($python eq "python-dbg")
135 and (grep /^\Q$python_default\E/, @dbg_build_needed)) {
136 push @python_dbg, $python_default."-dbg";
140 foreach my $python (@python_requested, @python_dbg) {
141 if (-x "/usr/bin/".$python) {
142 # To allow backports of debhelper we don't pass
143 # --install-layout=deb to 'setup.py install` for
144 # those Python versions where the option is
145 # ignored by distutils/setuptools.
146 if ( $act eq "install" and not
147 ( ($python =~ /^python(?:-dbg)?$/
148 and $python_default =~ /^python2\.[2345]$/)
149 or $python =~ /^python2\.[2345](?:-dbg)?$/ )) {
150 $this->doit_in_sourcedir($python, "setup.py",
151 $act, @_, "--install-layout=deb");
154 $this->doit_in_sourcedir($python, "setup.py",
163 $this->setup_py("build", @_);
169 $this->setup_py("install",
179 $this->setup_py("clean", "-a", @_);
181 # Config file will remain if it was created by us
182 if (!$this->not_our_cfg()) {
183 unlink($this->get_buildpath(".pydistutils.cfg"));
184 $this->rmdir_builddir(1); # only if empty
186 # The setup.py might import files, leading to python creating pyc
188 $this->doit_in_sourcedir('find', '.', '-name', '*.pyc', '-exec', 'rm', '{}', ';');