| 1 |
joss |
12895 |
#!/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 |
joss |
12980 |
B<dh_pysupport> [I<debhelper options>] [-V I<X.Y>] [-X I<item> [...]] [-n] [I<module dirs ...>] |
| 16 |
joss |
12895 |
|
| 17 |
|
|
=head1 DESCRIPTION |
| 18 |
|
|
|
| 19 |
|
|
dh_pysupport is a debhelper program that will scan your package, detect |
| 20 |
joss |
12980 |
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 |
joss |
12895 |
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 |
joss |
12980 |
If a file named I<debian/pyversions> exists, it is used to determine the |
| 30 |
|
|
python versions with which the package can work. |
| 31 |
joss |
12895 |
|
| 32 |
joss |
12930 |
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 |
joss |
12895 |
=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 |
joss |
12930 |
This option is deprecated. |
| 54 |
joss |
12895 |
|
| 55 |
joss |
12916 |
=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 |
joss |
12918 |
=item B<-X> I<item>, B<--exclude=>I<item> |
| 60 |
|
|
|
| 61 |
|
|
Exclude files that contain "item" anywhere in their filename from being |
| 62 |
joss |
12980 |
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 |
joss |
12918 |
|
| 66 |
joss |
12895 |
=back |
| 67 |
|
|
|
| 68 |
|
|
=head1 CONFORMS TO |
| 69 |
|
|
|
| 70 |
joss |
12918 |
Python policy as of 2006-08-10 |
| 71 |
joss |
12895 |
|
| 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 |
joss |
12938 |
sub specified_deps_in_package { |
| 86 |
|
|
my $package = shift; |
| 87 |
|
|
my $curpackage = 0; |
| 88 |
joss |
12960 |
my @deps = (); |
| 89 |
joss |
12938 |
open (CONTROL, 'debian/control') || error("cannot read debian/control: $!\n"); |
| 90 |
|
|
while (<CONTROL>) { |
| 91 |
|
|
chomp; |
| 92 |
|
|
s/\s+$//; |
| 93 |
joss |
12960 |
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 |
joss |
12938 |
} else { |
| 103 |
joss |
12960 |
return @deps; |
| 104 |
joss |
12938 |
} |
| 105 |
|
|
} |
| 106 |
|
|
if ($curpackage && /^Python-Depends:\s*(.*)$/) { |
| 107 |
joss |
12960 |
@deps = split ",",$1; |
| 108 |
|
|
if ($1 =~ /,$/) { |
| 109 |
|
|
$curpackage = 2; |
| 110 |
|
|
} else { |
| 111 |
|
|
return @deps; |
| 112 |
|
|
} |
| 113 |
joss |
12938 |
} |
| 114 |
|
|
} |
| 115 |
joss |
12960 |
return @deps; |
| 116 |
joss |
12938 |
} |
| 117 |
|
|
|
| 118 |
joss |
12944 |
sub trim { |
| 119 |
|
|
my $tmp = shift; |
| 120 |
|
|
$tmp =~ s/^\s+//; |
| 121 |
|
|
$tmp =~ s/\s+$//; |
| 122 |
|
|
return $tmp; |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
joss |
12916 |
# The current default python version |
| 126 |
joss |
12936 |
my $default=`readlink /usr/bin/python`; |
| 127 |
|
|
$default =~ s/^python//; |
| 128 |
joss |
12915 |
chomp $default; |
| 129 |
|
|
|
| 130 |
joss |
12980 |
my $privdir="/usr/share/python-support/private"; |
| 131 |
joss |
12916 |
# All supported versions |
| 132 |
joss |
12980 |
my $allversions_string=`$privdir/parseversions --all`; |
| 133 |
joss |
12916 |
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 |
joss |
12895 |
foreach my $package (@{$dh{DOPACKAGES}}) { |
| 146 |
|
|
my $tmp = tmpdir($package); |
| 147 |
joss |
12916 |
my $have_pydep=0; # This variable tells whether we have added some dependency |
| 148 |
|
|
# on python one way or another. |
| 149 |
joss |
12938 |
my @specified_deps = specified_deps_in_package ($package); |
| 150 |
joss |
12958 |
my $do_scripts = ""; |
| 151 |
joss |
12916 |
|
| 152 |
|
|
# 1) Handle public python modules |
| 153 |
|
|
# Move them to the python-support directories |
| 154 |
joss |
12980 |
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 |
joss |
12916 |
# Then look for what the script found |
| 175 |
joss |
12980 |
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 |
joss |
12958 |
# 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 |
joss |
12938 |
foreach my $dep (@specified_deps) { |
| 197 |
joss |
12958 |
$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 |
joss |
12938 |
} |
| 203 |
joss |
12958 |
} |
| 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 |
joss |
12932 |
$have_pydep=1; |
| 211 |
joss |
12958 |
} |
| 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 |
joss |
12916 |
} |
| 225 |
joss |
12980 |
$list_file =~ s,^$tmp/usr/share/python-support/,,; |
| 226 |
|
|
$do_scripts = "$do_scripts $list_file"; |
| 227 |
joss |
12916 |
} |
| 228 |
|
|
} |
| 229 |
joss |
12895 |
|
| 230 |
joss |
12916 |
# 2) Look for private python modules |
| 231 |
joss |
12915 |
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 |
joss |
12980 |
my @filelist; |
| 235 |
|
|
my $file; |
| 236 |
|
|
my $has_module = 0; |
| 237 |
|
|
my $has_extension = 0; |
| 238 |
joss |
12916 |
my $need_pydep=0; |
| 239 |
joss |
12918 |
my $strong_pydep=0; |
| 240 |
joss |
12916 |
my %need_verdep = (); |
| 241 |
|
|
foreach (@allversions) { |
| 242 |
|
|
$need_verdep{$_} = 0; |
| 243 |
|
|
} |
| 244 |
joss |
12895 |
if (@dirs) { |
| 245 |
|
|
foreach my $curdir (@dirs) { |
| 246 |
|
|
find sub { |
| 247 |
|
|
return unless -f; |
| 248 |
joss |
12918 |
return if excludefile($File::Find::name); |
| 249 |
joss |
12916 |
if (/.py$/) { |
| 250 |
|
|
$has_module=1; |
| 251 |
|
|
doit(("rm","-f",$_."c",$_."o")); |
| 252 |
joss |
12980 |
( $file=$File::Find::name ) =~ s%^$tmp%%; |
| 253 |
|
|
push @filelist, $file; |
| 254 |
joss |
12916 |
} |
| 255 |
joss |
12956 |
if (/.so$/ && |
| 256 |
|
|
`nm -Du "$_" | grep "U Py_InitModule"` && |
| 257 |
|
|
! `objdump -p "$_" | grep "NEEDED *libpython"`) { |
| 258 |
joss |
12918 |
$has_extension=1; |
| 259 |
|
|
} |
| 260 |
joss |
12915 |
}, $curdir ; |
| 261 |
joss |
12895 |
} |
| 262 |
|
|
} |
| 263 |
joss |
12980 |
|
| 264 |
|
|
if ( ($has_module or $has_extension) ) { |
| 265 |
|
|
if ( $useversion ) { |
| 266 |
|
|
$need_verdep{$useversion}=1; |
| 267 |
|
|
} else { |
| 268 |
|
|
$need_pydep=1; |
| 269 |
|
|
$strong_pydep=1 if $has_extension; |
| 270 |
|
|
} |
| 271 |
|
|
} |
| 272 |
|
|
|
| 273 |
|
|
if (@filelist) { |
| 274 |
joss |
12895 |
# We have private python modules |
| 275 |
|
|
# Use python-support to ensure that they are always |
| 276 |
|
|
# byte-compiled for the current version |
| 277 |
|
|
mkdir("$tmp/usr/share/python-support"); |
| 278 |
joss |
12980 |
open(FILELIST, "> $tmp/usr/share/python-support/$package.private") || |
| 279 |
|
|
error("Can't create $tmp/usr/share/python-support/$package.private: $!"); |
| 280 |
|
|
if ( $useversion ) { |
| 281 |
|
|
print FILELIST "pyversion=$useversion\n\n"; |
| 282 |
|
|
} |
| 283 |
|
|
print FILELIST map "$_\n", @filelist; |
| 284 |
|
|
close(FILELIST); |
| 285 |
|
|
$do_scripts = "$do_scripts $package.private"; |
| 286 |
joss |
12895 |
} |
| 287 |
|
|
|
| 288 |
joss |
12916 |
# 3) Add python-support dependency depending on what we found |
| 289 |
joss |
12974 |
if (-d "$tmp/usr/share/python-support") { |
| 290 |
joss |
12980 |
addsubstvar($package, "python:Depends", "python-support (>= 0.90.0)"); |
| 291 |
joss |
12974 |
} |
| 292 |
|
|
|
| 293 |
joss |
12916 |
# 4) Look for python scripts |
| 294 |
joss |
12958 |
find sub { |
| 295 |
|
|
return unless -f and -x; |
| 296 |
|
|
return if excludefile($File::Find::name); |
| 297 |
|
|
local *F; |
| 298 |
|
|
return unless open F, $_; |
| 299 |
|
|
if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) { |
| 300 |
|
|
if ( "python" eq $2 ) { |
| 301 |
|
|
$need_pydep=1; |
| 302 |
|
|
} elsif (defined $need_verdep{$3}) { |
| 303 |
|
|
$need_verdep{$3}=1; |
| 304 |
|
|
} |
| 305 |
|
|
} |
| 306 |
|
|
close F; |
| 307 |
|
|
}, $tmp; |
| 308 |
joss |
12916 |
|
| 309 |
|
|
# 5) Generate remaining dependencies |
| 310 |
joss |
12958 |
foreach (@allversions) { |
| 311 |
|
|
if ($need_verdep{$_}) { |
| 312 |
|
|
addsubstvar($package, "python:Depends", "python$_"); |
| 313 |
|
|
} |
| 314 |
|
|
} |
| 315 |
|
|
if (not $have_pydep) { |
| 316 |
|
|
if ($strong_pydep) { |
| 317 |
|
|
addsubstvar($package, "python:Depends", "python (>= $default)"); |
| 318 |
|
|
my $maxversion = next_minor_version($default); |
| 319 |
|
|
addsubstvar($package, "python:Depends", "python (<< $maxversion)"); |
| 320 |
|
|
$have_pydep=1; |
| 321 |
joss |
12980 |
} elsif ($need_pydep and $versions) { |
| 322 |
|
|
my $supported=`echo $versions | $privdir/parseversions --minmax`; |
| 323 |
joss |
12958 |
my @ar=split "\n",$supported; |
| 324 |
|
|
my @minmax=split " ",$ar[1]; |
| 325 |
|
|
my $minversion=$minmax[0]; |
| 326 |
|
|
if ($minversion ne "None") { |
| 327 |
|
|
addsubstvar($package, "python:Depends", "python (>= $minversion)"); |
| 328 |
|
|
$have_pydep=1; |
| 329 |
|
|
} |
| 330 |
|
|
my $maxversion=$minmax[1]; |
| 331 |
|
|
if ($maxversion ne "None") { |
| 332 |
|
|
$maxversion = next_minor_version($maxversion); |
| 333 |
|
|
addsubstvar($package, "python:Depends", "python (<< $maxversion)"); |
| 334 |
|
|
$have_pydep=1; |
| 335 |
|
|
} |
| 336 |
|
|
} |
| 337 |
|
|
} |
| 338 |
|
|
# If nothing has added a python dependency yet, add it |
| 339 |
|
|
if ($need_pydep and not $have_pydep) { |
| 340 |
|
|
addsubstvar($package, "python:Depends", "python"); |
| 341 |
joss |
12916 |
} |
| 342 |
joss |
12958 |
|
| 343 |
|
|
# 6) Generate the scripts |
| 344 |
|
|
if ($do_scripts && ! $dh{NOSCRIPTS}) { |
| 345 |
|
|
autoscript($package, "postinst", "postinst-python-support", "s,#ARGS#,$do_scripts,"); |
| 346 |
|
|
autoscript($package, "prerm", "prerm-python-support", "s,#ARGS#,$do_scripts,"); |
| 347 |
|
|
} |
| 348 |
joss |
12895 |
} |
| 349 |
|
|
|
| 350 |
|
|
=head1 SEE ALSO |
| 351 |
|
|
|
| 352 |
|
|
L<debhelper(7)> |
| 353 |
|
|
|
| 354 |
|
|
This program is a part of python-support but is made to work with debhelper. |
| 355 |
|
|
|
| 356 |
|
|
=head1 AUTHORS |
| 357 |
|
|
|
| 358 |
|
|
Josselin Mouette <joss@debian.org>, |
| 359 |
|
|
Raphael Hertzog <hertzog@debian.org> |
| 360 |
|
|
|
| 361 |
|
|
=cut |