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 B<python_distutils> - Python Distutils (setup.py)
16 B<dh_auto_*> [B<--buildsystem>=I<python_distutils>] ...
20 Python Distribution Utilities (Distutils for short) is a standard Python build
21 system. It is used to package most of the Python modules in the source
22 distribution form. Typically, only two steps (build and install) are needed to
23 finish installation of the Distutils based Python module. This build system can
24 be typically identified by presence of the F<setup.py> in the source directory.
28 Out of source tree building is done by default but in source building is also
29 supported. PLEASE NOTE that B<default build directory> is B<$srcdir/build>
30 where $srcdir is a path to the source directory.
32 Due to design flaws of Distutils, it is not possible to set a B<custom> build
33 directory via command line arguments to F<setup.py>. Therefore, the same effect
34 is achieved by writing appropriate F<.pydistutils.cfg> file to the build
35 directory and pointing $HOME environment variable to the build directory.
43 use Debian::Debhelper::Dh_Lib qw(error);
44 use base 'Debian::Debhelper::Buildsystem';
47 "Python Distutils (setup.py)"
50 sub DEFAULT_BUILD_DIRECTORY {
52 return $this->_canonpath($this->get_sourcepath("build"));
57 my $this=$class->SUPER::new(@_);
58 # Out of source tree building is prefered.
59 $this->enforce_out_of_source_building(@_);
63 sub check_auto_buildable {
65 return -e $this->get_sourcepath("setup.py");
71 if (open(my $cfg, $this->get_buildpath(".pydistutils.cfg"))) {
72 $ret = not "# Created by dh_auto\n" eq <$cfg>;
80 if (open(my $cfg, ">", $this->get_buildpath(".pydistutils.cfg"))) {
81 print $cfg "# Created by dh_auto", "\n";
82 print $cfg "[build]\nbuild-base=", $this->get_build_rel2sourcedir(), "\n";
89 sub pre_building_step {
93 return unless grep /$step/, qw(build install clean);
95 # --build-base can only be passed to the build command. However,
96 # it is always read from the config file (really weird design).
97 # Therefore create such a cfg config file.
98 if ($this->get_buildpath() ne $this->DEFAULT_BUILD_DIRECTORY()) {
99 not $this->not_our_cfg() or
100 error("cannot set custom build directory: .pydistutils.cfg is in use");
101 $this->mkdir_builddir();
102 $this->create_cfg() or
103 error("cannot set custom build directory: unwritable .pydistutils.cfg");
104 # Distutils reads $HOME/.pydistutils.cfg
105 $ENV{HOME} = Cwd::abs_path($this->get_buildpath());
112 $this->doit_in_sourcedir("python", "setup.py", $act, @_);
115 =head2 Configure step
121 Do nothing but stop auto-selection process.
123 =item I<Auto-selection>
125 If neither F<configure>, F<Makefile.PL> exist, but F<setup.py> exists in the
138 Execute C<python setup.py build>.
140 =item I<Auto-selection>
142 If F<Makefile>, F<makefile> F<GNUmakefile> do not exist in the build directory
143 and F<setup.py> file exists in the source directory.
150 $this->setup_py("build", @_);
159 Do nothing but stop auto-selection process.
161 =item I<Auto-selection>
163 F<Makefile>, F<makefile>, F<GNUmakefile> do not exist in the build directory and
164 F<setup.py> file exists in the source directory.
176 Execute C<python setup.py install> passing temporary installation directory via
177 C<--root> parameter. C<--no-compile> and C<-O0> parameters are also passed by
178 default. See L<dh_auto_install(1)> for more information.
180 =item I<Auto-selection>
182 F<Makefile>, F<makefile>, F<GNUmakefile> do not exist in the build directory and
183 F<setup.py> file exists in the source directory.
191 $this->setup_py("install", "--root=$destdir", "--no-compile", "-O0", @_);
200 Execute C<python setup.py clean -a>. Additional parameters (if specified) are
201 passed to the latter command. F<.pydistutils.cfg> is also removed if it was
202 created (together with the build directory if it is ends up empty). Finally,
203 recursively find and delete all *.pyc files from the source directory.
205 =item I<Auto-selection>
207 F<Makefile>, F<makefile>, F<GNUmakefile> do not exist in the build directory and
208 F<setup.py> file exists in the source directory.
215 $this->setup_py("clean", "-a", @_);
217 # Config file will remain if it was created by us
218 if (!$this->not_our_cfg()) {
219 unlink($this->get_buildpath(".pydistutils.cfg"));
220 $this->rmdir_builddir(1); # only if empty
222 # The setup.py might import files, leading to python creating pyc
224 $this->doit_in_sourcedir('find', '.', '-name', '*.pyc', '-exec', 'rm', '{}', ';');
233 Joey Hess <joeyh@debian.org>
234 Modestas Vainius <modestas@vainius.eu>