| 1 |
#!/usr/bin/perl -w
|
| 2 |
|
| 3 |
=head1 NAME
|
| 4 |
|
| 5 |
dh_pysupport - use the python-support framework to handle Python modules
|
| 6 |
|
| 7 |
=cut
|
| 8 |
|
| 9 |
use strict;
|
| 10 |
use File::Find;
|
| 11 |
use Debian::Debhelper::Dh_Lib;
|
| 12 |
|
| 13 |
=head1 SYNOPSIS
|
| 14 |
|
| 15 |
B<dh_pysupport> [I<debhelper options>] [-V I<X.Y>] [-X I<item> [...]] [-n] [I<module dirs ...>]
|
| 16 |
|
| 17 |
=head1 DESCRIPTION
|
| 18 |
|
| 19 |
dh_pysupport is a debhelper program that will scan your package, detect
|
| 20 |
public modules in I</usr/lib/pythonX.Y/site-packages>, and move them to
|
| 21 |
the shared Python modules location. It will generate appropriate
|
| 22 |
postinst/prerm scripts to byte-compile modules installed there for all
|
| 23 |
available python versions.
|
| 24 |
|
| 25 |
It will also look for private Python modules and will byte-compile them
|
| 26 |
with the current Python version. You may have to list the directories
|
| 27 |
containing private Python modules.
|
| 28 |
|
| 29 |
If a file named I<debian/pyversions> exists, it is used to determine the
|
| 30 |
python versions with which the package can work.
|
| 31 |
|
| 32 |
Appropriate dependencies on python-support, python and pythonI<X.Y> are
|
| 33 |
put in ${python:Depends}. The ${python:Versions} and ${python:Provides}
|
| 34 |
optional substitution variables are made available as well.
|
| 35 |
|
| 36 |
=head1 OPTIONS
|
| 37 |
|
| 38 |
=over 4
|
| 39 |
|
| 40 |
=item I<module dirs>
|
| 41 |
|
| 42 |
If your package installs private python modules in non-standard directories, you
|
| 43 |
can make dh_pysupport check those directories by passing their names on the
|
| 44 |
command line. By default, it will check /usr/lib/$PACKAGE,
|
| 45 |
/usr/share/$PACKAGE, /usr/lib/games/$PACKAGE and /usr/share/games/$PACKAGE
|
| 46 |
|
| 47 |
=item B<-n>, B<--noscripts>
|
| 48 |
|
| 49 |
Do not modify postinst/postrm scripts.
|
| 50 |
|
| 51 |
=item B<-d>
|
| 52 |
|
| 53 |
This option is deprecated.
|
| 54 |
|
| 55 |
=item B<-V> I<X.Y>
|
| 56 |
|
| 57 |
Force private modules to be bytecompiled with the specific I<X.Y> python version, regardless of the default python version on the system.
|
| 58 |
|
| 59 |
=item B<-X> I<item>, B<--exclude=>I<item>
|
| 60 |
|
| 61 |
Exclude files that contain "item" anywhere in their filename from being
|
| 62 |
taken into account to generate the python dependency. It also excludes
|
| 63 |
them from byte-compilation. You may use this option multiple times to
|
| 64 |
build up a list of things to exclude.
|
| 65 |
|
| 66 |
=back
|
| 67 |
|
| 68 |
=head1 CONFORMS TO
|
| 69 |
|
| 70 |
Python policy as of 2006-08-10
|
| 71 |
|
| 72 |
=cut
|
| 73 |
|
| 74 |
init();
|
| 75 |
|
| 76 |
sub next_minor_version {
|
| 77 |
my $version = shift;
|
| 78 |
# Handles 2.10 -> 2.11 gracefully
|
| 79 |
my @items = split(/\./, $version);
|
| 80 |
$items[1] += 1;
|
| 81 |
$version = join(".", @items);
|
| 82 |
return $version;
|
| 83 |
}
|
| 84 |
|
| 85 |
sub specified_deps_in_package {
|
| 86 |
my $package = shift;
|
| 87 |
my $curpackage = 0;
|
| 88 |
my @deps = ();
|
| 89 |
open (CONTROL, 'debian/control') || error("cannot read debian/control: $!\n");
|
| 90 |
while (<CONTROL>) {
|
| 91 |
chomp;
|
| 92 |
s/\s+$//;
|
| 93 |
if (/^Package:\s*(.*)$/ && $package eq $1) {
|
| 94 |
$curpackage = 1;
|
| 95 |
}
|
| 96 |
if ($curpackage == 2) {
|
| 97 |
if (/^\s+(.*)$/) {
|
| 98 |
push @deps, split ",",$1;
|
| 99 |
if ($1 !~ /,$/) {
|
| 100 |
return @deps;
|
| 101 |
}
|
| 102 |
} else {
|
| 103 |
return @deps;
|
| 104 |
}
|
| 105 |
}
|
| 106 |
if ($curpackage && /^Python-Depends:\s*(.*)$/) {
|
| 107 |
@deps = split ",",$1;
|
| 108 |
if ($1 =~ /,$/) {
|
| 109 |
$curpackage = 2;
|
| 110 |
} else {
|
| 111 |
return @deps;
|
| 112 |
}
|
| 113 |
}
|
| 114 |
}
|
| 115 |
return @deps;
|
| 116 |
}
|
| 117 |
|
| 118 |
sub trim {
|
| 119 |
my $tmp = shift;
|
| 120 |
$tmp =~ s/^\s+//;
|
| 121 |
$tmp =~ s/\s+$//;
|
| 122 |
return $tmp;
|
| 123 |
}
|
| 124 |
|
| 125 |
# The current default python version
|
| 126 |
my $default=`readlink /usr/bin/python`;
|
| 127 |
$default =~ s/^python//;
|
| 128 |
chomp $default;
|
| 129 |
|
| 130 |
my $privdir="/usr/share/python-support/private";
|
| 131 |
# All supported versions
|
| 132 |
my $allversions_string=`$privdir/parseversions --all`;
|
| 133 |
chomp $allversions_string;
|
| 134 |
my @allversions=split " ", $allversions_string;
|
| 135 |
|
| 136 |
# Use a specific version for private modules (doesn't affect public modules)
|
| 137 |
my $useversion;
|
| 138 |
if($dh{V_FLAG_SET}) {
|
| 139 |
$useversion = $dh{V_FLAG};
|
| 140 |
if (! grep { $_ eq $useversion } @allversions) {
|
| 141 |
error("Unknown python version $useversion");
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
foreach my $package (@{$dh{DOPACKAGES}}) {
|
| 146 |
my $tmp = tmpdir($package);
|
| 147 |
my $have_pydep=0; # This variable tells whether we have added some dependency
|
| 148 |
# on python one way or another.
|
| 149 |
my @specified_deps = specified_deps_in_package ($package);
|
| 150 |
my $do_scripts = "";
|
| 151 |
|
| 152 |
# 1) Handle public python modules
|
| 153 |
# Move them to the python-support directories
|
| 154 |
my $verfile = "debian/pyversions";
|
| 155 |
my $versions = "";
|
| 156 |
if (-f $verfile) {
|
| 157 |
# TODO: debian/package.pyversions ?
|
| 158 |
$versions=`cat $verfile`;
|
| 159 |
chomp $versions;
|
| 160 |
} else {
|
| 161 |
my $doko_versions=`$privdir/parseversions --raw --pycentral debian/control`;
|
| 162 |
chomp $doko_versions;
|
| 163 |
if ($doko_versions !~ /not found/) {
|
| 164 |
print "Compatibility mode: using detected XS-Python-Version.\n";
|
| 165 |
$versions=$doko_versions;
|
| 166 |
}
|
| 167 |
}
|
| 168 |
if ($versions) {
|
| 169 |
doit (("$privdir/movemodules","-V", $versions, $tmp))
|
| 170 |
} else {
|
| 171 |
doit (("$privdir/movemodules",$tmp));
|
| 172 |
}
|
| 173 |
|
| 174 |
# Then look for what the script found
|
| 175 |
foreach my $list_file (glob("$tmp/usr/share/python-support/*.public")) {
|
| 176 |
if (-f $list_file) {
|
| 177 |
my $supported=`$privdir/parseversions --minmax $list_file`;
|
| 178 |
|
| 179 |
# Add the packages explicitly asked by the maintainer
|
| 180 |
foreach my $dep (@specified_deps) {
|
| 181 |
$dep = trim $dep;
|
| 182 |
addsubstvar($package, "python:Depends", $dep);
|
| 183 |
}
|
| 184 |
my @ar=split "\n",$supported;
|
| 185 |
my @provides=split " ",$ar[0];
|
| 186 |
foreach my $pyversion (@provides) {
|
| 187 |
# Generate the useless versions field
|
| 188 |
addsubstvar($package, "python:Versions", $pyversion);
|
| 189 |
# ... and the provides field
|
| 190 |
if ($package =~ /^python-/) {
|
| 191 |
my $virtual = $package;
|
| 192 |
$virtual =~ s/^python-/python$pyversion-/;
|
| 193 |
addsubstvar($package, "python:Provides", $virtual);
|
| 194 |
}
|
| 195 |
# Use the provides fields in packages dependended upon
|
| 196 |
foreach my $dep (@specified_deps) {
|
| 197 |
$dep = trim $dep;
|
| 198 |
# I have no idea why this wouldn't be the case, but well
|
| 199 |
if ($dep =~ /^python-(\S+)/) {
|
| 200 |
addsubstvar($package, "python:Depends", "python$pyversion-$1");
|
| 201 |
}
|
| 202 |
}
|
| 203 |
}
|
| 204 |
my @minmax=split " ",$ar[1];
|
| 205 |
my $minversion=$minmax[0];
|
| 206 |
if ( grep { $_ eq $default } @provides ) {
|
| 207 |
# The default version is in the supported versions
|
| 208 |
if ($minversion ne "None") {
|
| 209 |
addsubstvar($package, "python:Depends", "python (>= $minversion)");
|
| 210 |
$have_pydep=1;
|
| 211 |
}
|
| 212 |
} elsif ($minversion ne "None") {
|
| 213 |
# The default version is less than all supported versions
|
| 214 |
addsubstvar($package, "python:Depends", "python (>= $minversion) | python$minversion");
|
| 215 |
$have_pydep=1;
|
| 216 |
} else {
|
| 217 |
error("The default python version is greater than all supported versions");
|
| 218 |
}
|
| 219 |
my $maxversion=$minmax[1];
|
| 220 |
if ($maxversion ne "None") {
|
| 221 |
$maxversion = next_minor_version($maxversion);
|
| 222 |
addsubstvar($package, "python:Depends", "python (<< $maxversion)");
|
| 223 |
$have_pydep=1;
|
| 224 |
}
|
| 225 |
$list_file =~ s,^$tmp/usr/share/python-support/,,;
|
| 226 |
$do_scripts = "$do_scripts $list_file";
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
| 230 |
# 2) Look for private python modules
|
| 231 |
my @dirs = ("/usr/lib/$package", "/usr/share/$package",
|
| 232 |
"/usr/lib/games/$package", "/usr/share/games/$package", @ARGV );
|
| 233 |
@dirs = grep -d, map "$tmp$_", @dirs;
|
| 234 |
my @filelist;
|
| 235 |
my $file;
|
| 236 |
my $has_module = 0;
|
| 237 |
my $has_extension = 0;
|
| 238 |
my $need_pydep=0;
|
| 239 |
my $strong_pydep=0;
|
| 240 |
my %need_verdep = ();
|
| 241 |
foreach (@allversions) {
|
| 242 |
$need_verdep{$_} = 0;
|
| 243 |
}
|
| 244 |
if (@dirs) {
|
| 245 |
foreach my $curdir (@dirs) {
|
| 246 |
find sub {
|
| 247 |
return unless -f;
|
| 248 |
return if excludefile($File::Find::name);
|
| 249 |
if (/.py$/) {
|
| 250 |
$has_module=1;
|
| 251 |
doit(("rm","-f",$_."c",$_."o"));
|
| 252 |
( $file=$File::Find::name ) =~ s%^$tmp%%;
|
| 253 |
if (! grep { $_ eq $file } @filelist) {
|
| 254 |
push @filelist, $file;
|
| 255 |
}
|
| 256 |
}
|
| 257 |
if (/.so$/ &&
|
| 258 |
`nm -Du "$_" | grep "U Py_InitModule"` &&
|
| 259 |
! `objdump -p "$_" | grep "NEEDED *libpython"`) {
|
| 260 |
$has_extension=1;
|
| 261 |
}
|
| 262 |
}, $curdir ;
|
| 263 |
}
|
| 264 |
}
|
| 265 |
|
| 266 |
if ( ($has_module or $has_extension) ) {
|
| 267 |
if ( $useversion ) {
|
| 268 |
$need_verdep{$useversion}=1;
|
| 269 |
} else {
|
| 270 |
$need_pydep=1;
|
| 271 |
$strong_pydep=1 if $has_extension;
|
| 272 |
}
|
| 273 |
}
|
| 274 |
|
| 275 |
if (@filelist) {
|
| 276 |
# We have private python modules
|
| 277 |
# Use python-support to ensure that they are always
|
| 278 |
# byte-compiled for the current version
|
| 279 |
mkdir("$tmp/usr/share/python-support");
|
| 280 |
open(FILELIST, "> $tmp/usr/share/python-support/$package.private") ||
|
| 281 |
error("Can't create $tmp/usr/share/python-support/$package.private: $!");
|
| 282 |
if ( $useversion ) {
|
| 283 |
print FILELIST "pyversion=$useversion\n\n";
|
| 284 |
}
|
| 285 |
print FILELIST map "$_\n", @filelist;
|
| 286 |
close(FILELIST);
|
| 287 |
$do_scripts = "$do_scripts $package.private";
|
| 288 |
}
|
| 289 |
|
| 290 |
# 3) Add python-support dependency depending on what we found
|
| 291 |
if (-d "$tmp/usr/share/python-support") {
|
| 292 |
addsubstvar($package, "python:Depends", "python-support (>= 0.90.0)");
|
| 293 |
}
|
| 294 |
|
| 295 |
# 4) Look for python scripts
|
| 296 |
find sub {
|
| 297 |
return unless -f and -x;
|
| 298 |
return if excludefile($File::Find::name);
|
| 299 |
local *F;
|
| 300 |
return unless open F, $_;
|
| 301 |
if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
|
| 302 |
if ( "python" eq $2 ) {
|
| 303 |
$need_pydep=1;
|
| 304 |
} elsif (defined $need_verdep{$3}) {
|
| 305 |
$need_verdep{$3}=1;
|
| 306 |
}
|
| 307 |
}
|
| 308 |
close F;
|
| 309 |
}, $tmp;
|
| 310 |
|
| 311 |
# 5) Generate remaining dependencies
|
| 312 |
foreach (@allversions) {
|
| 313 |
if ($need_verdep{$_}) {
|
| 314 |
addsubstvar($package, "python:Depends", "python$_");
|
| 315 |
}
|
| 316 |
}
|
| 317 |
if (not $have_pydep) {
|
| 318 |
if ($strong_pydep) {
|
| 319 |
addsubstvar($package, "python:Depends", "python (>= $default)");
|
| 320 |
my $maxversion = next_minor_version($default);
|
| 321 |
addsubstvar($package, "python:Depends", "python (<< $maxversion)");
|
| 322 |
$have_pydep=1;
|
| 323 |
} elsif ($need_pydep and $versions) {
|
| 324 |
my $supported=`echo $versions | $privdir/parseversions --minmax`;
|
| 325 |
my @ar=split "\n",$supported;
|
| 326 |
my @minmax=split " ",$ar[1];
|
| 327 |
my $minversion=$minmax[0];
|
| 328 |
if ($minversion ne "None") {
|
| 329 |
addsubstvar($package, "python:Depends", "python (>= $minversion)");
|
| 330 |
$have_pydep=1;
|
| 331 |
}
|
| 332 |
my $maxversion=$minmax[1];
|
| 333 |
if ($maxversion ne "None") {
|
| 334 |
$maxversion = next_minor_version($maxversion);
|
| 335 |
addsubstvar($package, "python:Depends", "python (<< $maxversion)");
|
| 336 |
$have_pydep=1;
|
| 337 |
}
|
| 338 |
}
|
| 339 |
}
|
| 340 |
# If nothing has added a python dependency yet, add it
|
| 341 |
if ($need_pydep and not $have_pydep) {
|
| 342 |
addsubstvar($package, "python:Depends", "python");
|
| 343 |
}
|
| 344 |
|
| 345 |
# 6) Generate the scripts
|
| 346 |
if ($do_scripts && ! $dh{NOSCRIPTS}) {
|
| 347 |
autoscript($package, "postinst", "postinst-python-support", "s,#ARGS#,$do_scripts,");
|
| 348 |
autoscript($package, "prerm", "prerm-python-support", "s,#ARGS#,$do_scripts,");
|
| 349 |
}
|
| 350 |
}
|
| 351 |
|
| 352 |
=head1 SEE ALSO
|
| 353 |
|
| 354 |
L<debhelper(7)>
|
| 355 |
|
| 356 |
This program is a part of python-support but is made to work with debhelper.
|
| 357 |
|
| 358 |
=head1 AUTHORS
|
| 359 |
|
| 360 |
Josselin Mouette <joss@debian.org>,
|
| 361 |
Raphael Hertzog <hertzog@debian.org>
|
| 362 |
|
| 363 |
=cut
|