Debian Codegraph @

lib/Debian/Debhelper/Buildsystem/dh_virtualenv.pm

raw
1 package Debian::Debhelper::Buildsystem::dh_virtualenv;
2
3 use strict;
4 use base 'Debian::Debhelper::Buildsystem';
5 use Debian::Debhelper::Dh_Lib;
6 use File::Spec;
7 use Cwd;
8
9 sub DESCRIPTION {
10 'Python Virtualenv';
11 }
12
13 sub DEFAULT_BUILD_DIRECTORY {
14 my $this=shift;
15 return $this->canonpath($this->get_sourcepath("build"));
16 }
17
18 sub check_auto_buildable {
19 my $this=shift;
20 return -e $this->get_sourcepath("setup.py") ? 1 : 0;
21 }
22
23 sub new {
24 my $class = shift;
25 my $this = $class->SUPER::new(@_);
26 $this->prefer_out_of_source_building();
27 return $this;
28 }
29
30 sub get_install_root {
31 my $prefix = "/usr/share/python";
32 if (defined $ENV{DH_VIRTUALENV_INSTALL_ROOT}) {
33 $prefix = $ENV{DH_VIRTUALENV_INSTALL_ROOT};
34 }
35 return $prefix;
36 }
37
38 sub get_venv_builddir {
39 my $this = shift;
40 my $builddir = $this->get_builddir();
41 my $virtualenv_name = $ENV{DH_VIRTUALENV_INSTALL_SUFFIX} || $this->sourcepackage();
42 my $prefix = $this->get_install_root();
43 return "$builddir$prefix/$virtualenv_name";
44 }
45
46 sub get_exec {
47 my $this = shift;
48 my $executable = shift;
49 my $builddir = $this->get_venv_builddir();
50 return Cwd::abs_path("$builddir/bin/$executable");
51 }
52
53 sub get_python {
54 my $this = shift;
55 return $this->get_exec("python");
56 }
57
58 sub get_pip {
59 my $this = shift;
60 return ($this->get_exec("python"), "-m", "pip");
61 }
62
63 sub configure {
64 my $this = shift;
65 doit('mkdir', '-p', $this->get_venv_builddir());
66 }
67
68 sub build {
69 my $this = shift;
70 my $sourcedir = $this->get_sourcedir();
71 my $builddir = $this->get_venv_builddir();
72 my @params = ('--no-site-packages');
73 my $reqfile = ('requirements.txt');
74 my @pipargs = ();
75
76 if (defined $ENV{DH_VIRTUALENV_ARGUMENTS}) {
77 @params = split(' ', $ENV{DH_VIRTUALENV_ARGUMENTS});
78 }
79
80 if (defined $ENV{DH_REQUIREMENTS_FILE}) {
81 $reqfile = $ENV{DH_REQUIREMENTS_FILE};
82 }
83
84 if (defined $ENV{DH_PIP_EXTRA_ARGS}) {
85 @pipargs = split(' ', $ENV{DH_PIP_EXTRA_ARGS});
86 }
87
88 $this->doit_in_builddir(
89 'virtualenv', @params, Cwd::abs_path($builddir));
90
91 my $python = $this->get_python();
92 my @pip = $this->get_pip();
93
94 if (defined $ENV{DH_UPGRADE_PIP}) {
95 my $version = length $ENV{DH_UPGRADE_PIP} && '=='.$ENV{DH_UPGRADE_PIP} || '';
96 $this->doit_in_sourcedir(
97 @pip, 'install', '-U', 'pip' . $version);
98 }
99 if (defined $ENV{DH_UPGRADE_SETUPTOOLS}) {
100 my $version = length $ENV{DH_UPGRADE_SETUPTOOLS} && '=='.$ENV{DH_UPGRADE_SETUPTOOLS} || '';
101 $this->doit_in_sourcedir(
102 @pip, 'install', '-U', 'setuptools' . $version);
103 }
104 if (defined $ENV{DH_UPGRADE_WHEEL}) {
105 my $version = length $ENV{DH_UPGRADE_WHEEL} && '=='.$ENV{DH_UPGRADE_WHEEL} || '';
106 $this->doit_in_sourcedir(
107 @pip, 'install', '-U', 'wheel' . $version);
108 }
109 $this->doit_in_sourcedir(
110 @pip, 'install', '-r', $reqfile, @pipargs);
111 }
112
113 sub test {
114 my $this = shift;
115 my $python = $this->get_python();
116 $this->doit_in_sourcedir(
117 $python, 'setup.py', 'test');
118 }
119
120 sub install {
121 my $this = shift;
122 my $destdir = shift;
123 my @pip = $this->get_pip();
124 my $python = $this->get_python();
125 my $sourcepackage = $this->sourcepackage();
126 my $venv = $this->get_venv_builddir();
127 my $prefix = $this->get_install_root();
128
129 $this->doit_in_sourcedir(
130 @pip, 'install', '.');
131
132 # Before we copy files, let's make the symlinks in the 'usr/local'
133 # relative to the build path.
134 my @files_in_local = <"$venv/local/*">;
135 foreach (@files_in_local) {
136 if ( -l $_ ) {
137 my $target = readlink;
138 my $relpath = File::Spec->abs2rel($target, "$venv/local");
139 my $basename = Debian::Debhelper::Dh_Lib->basename($_);
140 unlink;
141 symlink($relpath, $_);
142 }
143 }
144
145 $this->doit_in_builddir('mkdir', '-p', $destdir);
146 $this->doit_in_builddir('cp', '-r', '-T', '.', $destdir);
147
148 my $new_python = undef;
149 my @binaries = undef;
150
151 if (defined $ENV{DH_VIRTUALENV_INSTALL_SUFFIX}) {
152 $new_python = "$prefix/" . $ENV{DH_VIRTUALENV_INSTALL_SUFFIX} . "/bin/python";
153 my $curdir = "$destdir$prefix" . $ENV{DH_VIRTUALENV_INSTALL_SUFFIX} . "/bin/*";
154 @binaries = glob($curdir);
155 } else {
156 $new_python = "$prefix/$sourcepackage/bin/python";
157 @binaries = <"$destdir$prefix/$sourcepackage/bin/*">;
158 }
159
160 # Fix shebangs so that we use the Python in the final location
161 # instead of the Python in the build directory
162 {
163 local $^I = q{};
164 local @ARGV = grep { -T } @binaries;
165 while ( <> ) {
166 s|^#!.*bin/(env )?python|#!$new_python|;
167 print;
168 }
169 }
170 }
171
172 sub clean {
173 my $this = shift;
174 $this->rmdir_builddir();
175 }
176
177 1