]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Buildsystem/python_distutils.pm
Build Python dbg extensions automatically.
[debhelper.git] / Debian / Debhelper / Buildsystem / python_distutils.pm
1 # A debhelper build system class for building Python Distutils based
2 # projects. It prefers out of source tree building.
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 (setup.py)"
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         # Out of source tree building is prefered.
28         $this->prefer_out_of_source_building(@_);
29         return $this;
30 }
31
32 sub check_auto_buildable {
33         my $this=shift;
34         return -e $this->get_sourcepath("setup.py");
35 }
36
37 sub not_our_cfg {
38         my $this=shift;
39         my $ret;
40         if (open(my $cfg, $this->get_buildpath(".pydistutils.cfg"))) {
41                 $ret = not "# Created by dh_auto\n" eq <$cfg>;
42                 close $cfg;
43         }
44         return $ret;
45 }
46
47 sub create_cfg {
48         my $this=shift;
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";
52                 close $cfg;
53                 return 1;
54         }
55         return 0;
56 }
57
58 sub pre_building_step {
59         my $this=shift;
60         my $step=shift;
61
62         return unless grep /$step/, qw(build install clean);
63
64         # --build-base can only be passed to the build command. However,
65         # it is always read from the config file (really weird design).
66         # Therefore create such a cfg config file.
67         # See http://bugs.python.org/issue818201
68         #     http://bugs.python.org/issue1011113
69         if ($this->get_buildpath() ne $this->DEFAULT_BUILD_DIRECTORY()) {
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());
77         }
78
79         $this->SUPER::pre_building_step($step);
80 }
81
82 sub dbg_build_needed {
83         my $this=shift;
84         my $act=shift;
85
86         my @dbg;
87         open (CONTROL,  $this->get_sourcepath('debian/control')) ||
88                 error("cannot read debian/control: $!\n");
89         foreach my $builddeps (join('', <CONTROL>) =~ /^Build-Depends[^:]*:.*\n(?:^[^\w\n].*\n)*/gmi) {
90                 foreach ($builddeps =~ /(python[^, ]*-dbg)/g) {
91                         push @dbg, $1;
92                 }
93         }
94
95         close CONTROL;
96         return @dbg;
97
98 }
99
100 sub setup_py {
101         my $this=shift;
102         my $act=shift;
103
104         my $python_default = `pyversions -d`;
105         $python_default =~ s/^\s+//;
106         $python_default =~ s/\s+$//;
107         my $python_requested = split ' ', `pyversions -r 2>/dev/null`;
108         my @dbg_build_needed = $this->dbg_build_needed();
109
110         # We need to to run setup.py with the default python first
111         # as distutils/setuptools modifies the shebang lines of scripts.
112         # This ensures that #!/usr/bin/python is used and not pythonX.Y
113         if (grep /^$python_default/, $python_requested) {
114                 $this->doit_in_sourcedir("python", "setup.py", $act, @_);
115         }
116         if (grep /^(python-all-dbg|python-dbg)/, $dbg_build_needed) {
117                 $this->doit_in_sourcedir("python-dbg", "setup.py", $act, @_);
118         }
119         for my $python (grep !/^$python_default/, $python_requested) {
120                 if (-x "/usr/bin/" . $python) {
121                         $this->doit_in_sourcedir($python, "setup.py", $act, @_);
122                 }
123                 $python = $python . "-dbg";
124                 if (grep /^(python-all-dbg|$python)/, @dbg_build_needed) {
125                         $this->doit_in_sourcedir($python, "setup.py", $act, @_);
126                 }
127
128         }
129 }
130
131 sub build {
132         my $this=shift;
133         $this->setup_py("build", @_);
134 }
135
136 sub install {
137         my $this=shift;
138         my $destdir=shift;
139         $this->setup_py("install",
140                 "--root=$destdir",
141                 "--no-compile",
142                 "-O0",
143                 "--install-layout=deb",
144                 @_);
145 }
146
147 sub clean {
148         my $this=shift;
149         $this->setup_py("clean", "-a", @_);
150
151         # Config file will remain if it was created by us
152         if (!$this->not_our_cfg()) {
153                 unlink($this->get_buildpath(".pydistutils.cfg"));
154                 $this->rmdir_builddir(1); # only if empty
155         }
156         # The setup.py might import files, leading to python creating pyc
157         # files.
158         $this->doit_in_sourcedir('find', '.', '-name', '*.pyc', '-exec', 'rm', '{}', ';');
159 }
160
161 1