]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/python_distutils.pm
Fix build directory handling in python_distutils build system.
[debhelper.git] / Debian / Debhelper / Buildsystem / python_distutils.pm
1 # A buildsystem plugin for building Python Distutils based
2 # projects.
3 #
4 # Copyright: © 2008 Joey Hess
5 #            © 2008-2009 Modestas Vainius
6 # License: GPL-2+
7
8 package Debian::Debhelper::Buildsystem::python_distutils;
9
10 use strict;
11 use Cwd ();
12 use Debian::Debhelper::Dh_Lib qw(error);
13 use base 'Debian::Debhelper::Buildsystem';
14
15 sub DESCRIPTION {
16         "Python distutils"
17 }
18
19 sub DEFAULT_BUILD_DIRECTORY {
20         my $this=shift;
21         return $this->_canonpath($this->get_sourcepath("build"));
22 }
23
24 sub new {
25         my $class=shift;
26         my $this=$class->SUPER::new(@_);
27         my %args=@_;
28         # Out of source tree building is prefered.
29         $this->enforce_out_of_source_building($args{builddir});
30         return $this;
31 }
32
33 sub check_auto_buildable {
34         my $this=shift;
35         return -e $this->get_sourcepath("setup.py");
36 }
37
38 sub not_our_cfg {
39         my $this=shift;
40         my $ret;
41         if (open(my $cfg, $this->get_buildpath(".pydistutils.cfg"))) {
42                 $ret = not "# Created by dh_auto\n" eq <$cfg>;
43                 close DISTUTILSCFG;
44         }
45         return $ret;
46 }
47
48 sub create_cfg {
49         my $this=shift;
50         if (open(my $cfg, ">", $this->get_buildpath(".pydistutils.cfg"))) {
51                 print $cfg "# Created by dh_auto", "\n";
52                 print $cfg "[build]\nbuild-base=", $this->get_build_rel2sourcedir(), "\n";
53                 close $cfg;
54                 return 1;
55         }
56         return 0;
57 }
58
59 sub pre_building_step {
60         my $this=shift;
61         my $step=shift;
62
63         return unless grep /$step/, qw(build install clean);
64
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         if ($this->get_buildpath() ne $this->DEFAULT_BUILD_DIRECTORY()) {
69                 not $this->not_our_cfg() or
70                     error("cannot set custom build directory: .pydistutils.cfg is in use");
71                 $this->mkdir_builddir();
72                 $this->create_cfg() or
73                     error("cannot set custom build directory: unwritable .pydistutils.cfg");
74                 # Distutils reads $HOME/.pydistutils.cfg
75                 $ENV{HOME} = Cwd::abs_path($this->get_buildpath());
76         }
77 }
78
79 sub setup_py {
80         my $this=shift;
81         my $act=shift;
82         $this->doit_in_sourcedir("python", "setup.py", $act, @_);
83 }
84
85 sub build {
86         my $this=shift;
87         $this->setup_py("build", @_);
88 }
89
90 sub install {
91         my $this=shift;
92         my $destdir=shift;
93         $this->setup_py("install", "--root=$destdir", "--no-compile", "-O0", @_);
94 }
95
96 sub clean {
97         my $this=shift;
98         $this->setup_py("clean", "-a", @_);
99
100         # Config file will remain if it was created by us
101         if (!$this->not_our_cfg()) {
102                 unlink($this->get_buildpath(".pydistutils.cfg"));
103                 $this->rmdir_builddir(1); # only if empty
104         }
105         # The setup.py might import files, leading to python creating pyc
106         # files.
107         $this->doit_in_sourcedir('find', '.', '-name', '*.pyc', '-exec', 'rm', '{}', ';');
108 }
109
110 1;