/[collab-maint]/deb-maint/python-support/tags/1.0.14/debhelper/dh_pysupport
ViewVC logotype

Contents of /deb-maint/python-support/tags/1.0.14/debhelper/dh_pysupport

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18744 - (show annotations) (download)
Mon Jun 27 20:21:15 2011 UTC (22 months, 3 weeks ago) by piotr
File size: 12232 byte(s)
[svn-buildpackage] Tagging python-support 1.0.14
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 warning("This program is deprecated, you should use dh_python2 instead. Migration guide: http://deb.li/dhs2p");
77
78 sub next_minor_version {
79 my $version = shift;
80 # Handles 2.10 -> 2.11 gracefully
81 my @items = split(/\./, $version);
82 $items[1] += 1;
83 $version = join(".", @items);
84 return $version;
85 }
86
87 sub specified_deps_in_package {
88 my $package = shift;
89 my $curpackage = 0;
90 my @deps = ();
91 open (CONTROL, 'debian/control') || error("cannot read debian/control: $!\n");
92 while (<CONTROL>) {
93 chomp;
94 s/\s+$//;
95 if (/^Package:\s*(.*)$/ && $package eq $1) {
96 $curpackage = 1;
97 }
98 if ($curpackage == 2) {
99 if (/^\s+(.*)$/) {
100 push @deps, split ",",$1;
101 if ($1 !~ /,$/) {
102 return @deps;
103 }
104 } else {
105 return @deps;
106 }
107 }
108 if ($curpackage && /^Python-Depends:\s*(.*)$/) {
109 @deps = split ",",$1;
110 if ($1 =~ /,$/) {
111 $curpackage = 2;
112 } else {
113 return @deps;
114 }
115 }
116 }
117 return @deps;
118 }
119
120 sub trim {
121 my $tmp = shift;
122 $tmp =~ s/^\s+//;
123 $tmp =~ s/\s+$//;
124 return $tmp;
125 }
126
127 # The current default python version
128 my $default=`readlink /usr/bin/python`;
129 $default =~ s/^python//;
130 chomp $default;
131
132 # Versions supported by python-defaults
133 my @debian_pysupported = split(/ /, `/usr/bin/pyversions -sv`);
134 chomp @debian_pysupported;
135
136 my $privdir="/usr/share/python-support/private";
137 # All supported versions
138 my $allversions_string=`$privdir/parseversions --all`;
139 chomp $allversions_string;
140 my @allversions=split " ", $allversions_string;
141
142 if (! grep { $_ eq $default } @allversions) {
143 error("Cannot detect default Python version");
144 }
145
146 # Use a specific version for private modules (doesn't affect public modules)
147 my $useversion;
148 if($dh{V_FLAG_SET}) {
149 $useversion = $dh{V_FLAG};
150 if (! grep { $_ eq $useversion } @allversions) {
151 error("Unknown python version $useversion");
152 }
153 }
154
155 foreach my $package (@{$dh{DOPACKAGES}}) {
156 next if ($package =~ /^python3-/); # ignore Python 3 packages
157 my $tmp = tmpdir($package);
158 my $need_pydep=0; # This variable tells whether we need a Python dependency
159 # regardless of the rest
160 my $have_pydep=0; # This variable tells whether we have added some dependency
161 # on python one way or another.
162 my @specified_deps = specified_deps_in_package ($package);
163 my $do_scripts = "";
164
165 # 1) Handle public python modules
166 # Move them to the python-support directories
167 my $verfile = "debian/pyversions";
168 my $versions = "";
169 if (open (VERFILE, $verfile)) {
170 # read first non-empty line
171 local $/ = "";
172 $versions = <VERFILE>;
173 chomp $versions;
174 close (VERFILE);
175 $versions = trim $versions;
176 # TODO: debian/package.pyversions ?
177 } else {
178 my $doko_versions=`$privdir/parseversions --raw --pycentral debian/control`;
179 chomp $doko_versions;
180 if ($doko_versions !~ /not found/) {
181 $versions=$doko_versions;
182 }
183 }
184 if ($versions) {
185 doit (("$privdir/movemodules","-V", $versions, $tmp))
186 } else {
187 doit (("$privdir/movemodules",$tmp));
188 }
189
190 # Then look for what the script found
191 foreach my $list_file (glob("$tmp/usr/share/python-support/*.public")) {
192 if (-f $list_file) {
193 my $supported=`$privdir/parseversions --minmax $list_file`;
194
195 # Add the packages explicitly asked by the maintainer
196 foreach my $dep (@specified_deps) {
197 $dep = trim $dep;
198 addsubstvar($package, "python:Depends", $dep);
199 }
200 my @ar=split "\n",$supported;
201 my @provides=split " ",$ar[0];
202 foreach my $pyversion (@provides) {
203 # Skip the substvars part for versions that might not
204 # be provided by packages depended upon.
205 next if (! grep { $_ eq $pyversion } @debian_pysupported);
206
207 # Generate the useless versions field
208 addsubstvar($package, "python:Versions", $pyversion);
209 # ... and the provides field
210 if ($package =~ /^python-/) {
211 my $virtual = $package;
212 $virtual =~ s/^python-/python$pyversion-/;
213 addsubstvar($package, "python:Provides", $virtual);
214 }
215 # Use the provides fields in packages dependended upon
216 foreach my $dep (@specified_deps) {
217 $dep = trim $dep;
218 # I have no idea why this wouldn't be the case, but well
219 if ($dep =~ /^python-(\S+)/) {
220 addsubstvar($package, "python:Depends", "python$pyversion-$1");
221 }
222 }
223 }
224 my @minmax=split " ",$ar[1];
225 my $minversion=$minmax[0];
226 if ( grep { $_ eq $default } @provides ) {
227 # The default version is in the supported versions
228 if ($minversion ne "None") {
229 addsubstvar($package, "python:Depends", "python (>= $minversion)");
230 $have_pydep=1;
231 }
232 } elsif ($minversion ne "None") {
233 # The default version is less than all supported versions
234 addsubstvar($package, "python:Depends", "python (>= $minversion) | python$minversion");
235 $have_pydep=1;
236 } else {
237 error("The default python version is greater than all supported versions");
238 }
239 my $maxversion=$minmax[1];
240 if ($maxversion ne "None") {
241 $maxversion = next_minor_version($maxversion);
242 addsubstvar($package, "python:Depends", "python (<< $maxversion)");
243 $have_pydep=1;
244 }
245 $list_file =~ s,^.*/,,;
246 $do_scripts = "$do_scripts $list_file";
247
248 $need_pydep = 1;
249 }
250 }
251
252 # 2) Look for private python modules
253 my @dirs = ("/usr/lib/$package", "/usr/share/$package",
254 "/usr/lib/games/$package", "/usr/share/games/$package", @ARGV );
255 @dirs = grep -d, map "$tmp$_", @dirs;
256 my @filelist;
257 my $file;
258 my $has_module = 0;
259 my $has_extension = 0;
260 my $strong_pydep=0;
261 my %need_verdep = ();
262 foreach (@allversions) {
263 $need_verdep{$_} = 0;
264 }
265 if (@dirs) {
266 foreach my $curdir (@dirs) {
267 find sub {
268 return unless -f;
269 return if excludefile($File::Find::name);
270 if (/\.py$/) {
271 $has_module=1;
272 doit(("rm","-f",$_."c",$_."o"));
273 ( $file=$File::Find::name ) =~ s%^$tmp%%;
274 if (! grep { $_ eq $file } @filelist) {
275 push @filelist, $file;
276 }
277 }
278 if (/\.so$/ &&
279 `nm -Du "$_" | grep "U Py_InitModule"` &&
280 ! `objdump -p "$_" | grep "NEEDED *libpython"`) {
281 $has_extension=1;
282 }
283 }, $curdir ;
284 }
285 }
286
287 if ( ($has_module or $has_extension) ) {
288 if ( $useversion ) {
289 $need_verdep{$useversion}=1;
290 } else {
291 $need_pydep=1;
292 $strong_pydep=1 if $has_extension;
293 }
294 }
295
296 if (@filelist) {
297 # We have private python modules
298 # Use python-support to ensure that they are always
299 # byte-compiled for the current version
300 doit("mkdir", "-p", "-m", "755", "$tmp/usr/share/python-support");
301 open(FILELIST, "> $tmp/usr/share/python-support/$package.private") ||
302 error("Can't create $tmp/usr/share/python-support/$package.private: $!");
303 if ( $useversion ) {
304 print FILELIST "pyversion=$useversion\n\n";
305 }
306 print FILELIST map "$_\n", @filelist;
307 close(FILELIST);
308 $do_scripts = "$do_scripts $package.private";
309 }
310
311 # 3) Add python-support dependency depending on what we found
312 if (-d "$tmp/usr/share/python-support") {
313 addsubstvar($package, "python:Depends", "python-support (>= 0.90.0)");
314 }
315
316 # 4) Look for python scripts
317 find sub {
318 return unless -f and -x;
319 return if excludefile($File::Find::name);
320 local *F;
321 return unless open F, $_;
322 if (read F, local $_, 32 and m%^#!\s*/usr/bin/(env\s+)?(python(\d+\.\d+)?)\s%) {
323 if ( "python" eq $2 ) {
324 $need_pydep=1;
325 } elsif (defined $need_verdep{$3}) {
326 $need_verdep{$3}=1;
327 }
328 }
329 close F;
330 }, $tmp;
331
332 # 5) Generate remaining dependencies
333 foreach my $version (@allversions) {
334 if ($need_verdep{$version}) {
335 addsubstvar($package, "python:Depends", "python$version");
336 }
337 }
338 if (not $have_pydep) {
339 if ($strong_pydep) {
340 addsubstvar($package, "python:Depends", "python (>= $default)");
341 my $maxversion = next_minor_version($default);
342 addsubstvar($package, "python:Depends", "python (<< $maxversion)");
343 $have_pydep=1;
344 } elsif ($need_pydep and $versions) {
345 my $supported=`echo $versions | $privdir/parseversions --minmax`;
346 my @ar=split "\n",$supported;
347 my @minmax=split " ",$ar[1];
348 my $minversion=$minmax[0];
349 if ($minversion ne "None") {
350 addsubstvar($package, "python:Depends", "python (>= $minversion)");
351 $have_pydep=1;
352 }
353 my $maxversion=$minmax[1];
354 if ($maxversion ne "None") {
355 $maxversion = next_minor_version($maxversion);
356 addsubstvar($package, "python:Depends", "python (<< $maxversion)");
357 $have_pydep=1;
358 }
359 }
360 }
361 # If nothing has added a python dependency yet, add it
362 if ($need_pydep and not $have_pydep) {
363 addsubstvar($package, "python:Depends", "python");
364 }
365
366 # 6) Generate the scripts
367 if ($do_scripts && ! $dh{NOSCRIPTS}) {
368 autoscript($package, "postinst", "postinst-python-support", "s,#ARGS#,$do_scripts,");
369 autoscript($package, "prerm", "prerm-python-support", "s,#ARGS#,$do_scripts,");
370 }
371 }
372
373 =head1 SEE ALSO
374
375 L<debhelper(7)>
376
377 This program is a part of python-support but is made to work with debhelper.
378
379 =head1 AUTHORS
380
381 Josselin Mouette <joss@debian.org>,
382 Raphael Hertzog <hertzog@debian.org>
383
384 =cut

  ViewVC Help
Powered by ViewVC 1.1.5