File: dh_fixpythonscripts

package info (click to toggle)
gadfly 1.0.0-16
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,308 kB
  • ctags: 2,139
  • sloc: python: 12,216; ansic: 5,172; makefile: 89; perl: 35
file content (44 lines) | stat: -rw-r--r-- 961 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl -w
# Ensure that Python scripts have appropriately (un)versioned shebangs, and
# make them executable.

use strict;
use File::Find;
use Debian::Debhelper::Dh_Lib;

init();

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmpdir=tmpdir($package);

	my $python='/usr/bin/';
	if ($package=~/^(python[\d.]*)-/) {
		$python.=$1;
	} elsif ($_=(glob "$tmpdir/usr/lib/python*")[0]) {
		s:.*/::;
		$python.=$_;
	} else {
		next;
	}

	find sub {
		return unless -f and /\.py$/;

		open IN, $_ or die "$_: $!";
		my $line=<IN>;
		if (defined $line &&
		    $line=~m,^#!\s*(\S+/)(?:env\s+)?(python[\d.]*)(\s.*),s) {
			if ($1.$2 ne $python) {
				open OUT, ">$_.tmp" or die "$_: $!";
				print OUT "#!$python$3" or die "$_: $!";
				while ($line=<IN>) {
					print OUT $line or die "$_: $!";
				}
				close OUT or die "$_: $!";
				rename "$_.tmp", $_ or die "$_: $!";
			}
			chmod 0755, $_ or die "$_: $!";
		}
		close IN or die "$_: $!";
	}, $tmpdir;
}