Debian Codegraph @

tools/which_deb

raw
1 #!/usr/bin/perl -w
2 #
3 # which_deb
4 #
5 # Simple helper tool to find the appropriate version of a package in
6 # the archive to meet a requirement in the debian-cd build
7
8 use strict;
9 use List::Util qw{first};
10
11 my ($mirror, $codename, $pkg, $pth, $output, $text_out);
12
13 $mirror = shift;
14 $codename = shift;
15 $pkg = shift;
16 $output = shift;
17 $pth = "$mirror/dists/$codename";
18 $text_out = "";
19
20 my @components = qw(main);
21 push @components, 'contrib' if $ENV{CONTRIB};
22 if ($ENV{NONFREE}) {
23 push @components, split /\ /,$ENV{NONFREE_COMPONENTS};
24 }
25 push @components, 'local' if $ENV{LOCAL};
26
27 if (!defined ($output)) {
28 $output = "binary";
29 }
30
31 # Give preference to i386 and amd64, if specified
32 my @ARCHES;
33 if ( $ENV{ARCHES} ) {
34 push @ARCHES, 'i386' if $ENV{ARCHES} =~ /(^|\s)i386(\s|$)/;
35 push @ARCHES, 'amd64' if $ENV{ARCHES} =~ /(^|\s)amd64(\s|$)/;
36 push @ARCHES, grep { !/^(source|i386|amd64)$/ } split /\s+/, $ENV{ARCHES};
37 }
38
39 # We seem to be building a source-only CD. Check for whatever binary
40 # arches exist in the archive; #695244
41 if (!@ARCHES) {
42 my %found_arches;
43 my $dh;
44 for my $component(@components) {
45 opendir ($dh, "$pth/$component");
46 if ($dh) {
47 while (my $entry = readdir $dh) {
48 $entry =~ /^binary-(.*)/ and $1 !~ /all/ and $found_arches{$1} = 1;
49 }
50 }
51 close $dh;
52 }
53 if ($found_arches{"i386"}) {
54 push @ARCHES, 'i386';
55 }
56 if ($found_arches{"amd64"}) {
57 push @ARCHES, 'amd64';
58 }
59 for my $arch (sort keys %found_arches) {
60 push @ARCHES, $arch;
61 }
62 }
63
64 sub grab_bin_info {
65 my $pth = shift;
66 my $arch = shift;
67 my $pkgname = shift;
68 my $old_split = $/;
69 my $match;
70 my $result = "";
71
72 $/ = ''; # Browse by paragraph
73
74 for my $component ( @components ) {
75 my $pgz = "$pth/$component/binary-$arch/Packages.gz";
76 if ( $component eq 'local' and $ENV{LOCALDEBS} ) {
77 $pgz = "$ENV{LOCALDEBS}/dists/$codename/local/binary-$arch/Packages.gz";
78 }
79 my $pxz = "$pth/$component/binary-$arch/Packages.xz";
80 if ( $component eq 'local' and $ENV{LOCALDEBS} ) {
81 $pxz = "$ENV{LOCALDEBS}/dists/$codename/local/binary-$arch/Packages.xz";
82 }
83 if (-e $pgz) {
84 open(PFILE, "zcat $pgz |") or
85 die "Failed to read Packages file $pgz";
86 } elsif (-e $pxz) {
87 open(PFILE, "xzcat $pxz |") or
88 die "Failed to read Packages file $pxz";
89 } else {
90 die "Can't find a working Packages file, tried $pgz and $pxz\n";
91 }
92 while (defined($match = <PFILE>)) {
93 if (($match =~ /^Package: \Q$pkgname\E$/m)) {
94 $result = $match;
95 close PFILE;
96 return $result;
97 }
98 }
99 close PFILE;
100 }
101 return "";
102 }
103
104 sub grab_src_info {
105 my $pth = shift;
106 my $pkgname = shift;
107 my $old_split = $/;
108 my $match;
109 my $result = "";
110
111 $/ = ''; # Browse by paragraph
112
113 for my $component ( @components ) {
114 my $pgz = "$pth/$component/source/Sources.gz";
115 my $pxz = "$pth/$component/source/Sources.xz";
116
117 if (-e $pgz) {
118 open(PFILE, "zcat $pgz |") or
119 die "Failed to read Sources file $pgz";
120 } elsif (-e $pxz) {
121 open(PFILE, "xzcat $pxz |") or
122 die "Failed to read Sources file $pxz";
123 } else {
124 die "Can't find a working Sources file, tried $pgz and $pxz\n";
125 }
126 while (defined($match = <PFILE>)) {
127 if (($match =~ /^Package: \Q$pkgname\E$/m)) {
128 $result = "$result" . $match;
129 }
130 }
131 close PFILE;
132 }
133 return $result;
134 }
135
136 my $bin_deb = "";
137 my $pkgdata = "";
138 my $srcname = "";
139
140 if ($pkg eq "syslinux") {
141 first { $pkgdata = grab_bin_info($pth, $_, "syslinux-common") } @ARCHES;
142 if (length($pkgdata) < 3) {
143 first { $pkgdata = grab_bin_info($pth, $_, "syslinux") } @ARCHES;
144 }
145 } elsif ($pkg eq "delo") {
146 $pkgdata = grab_bin_info($pth, "mipsel", $pkg);
147 } else { # Fallthrough for all other packages
148 first { $pkgdata = grab_bin_info($pth, $_, $pkg) } @ARCHES;
149 }
150
151 if (length($pkgdata) > 2) {
152 if ($output eq "binary") {
153 $pkgdata =~ m/^Filename: (\S+)/m and $bin_deb = $1;
154 $text_out = "$bin_deb\n";
155 }
156 elsif ($output eq "source") {
157 $srcname = $pkg;
158 $pkgdata =~ m/^Source: (\S+)/m and $srcname = $1;
159 $pkgdata = grab_src_info($pth, $srcname);
160 if (length($pkgdata) > 2) {
161 my $dir;
162 $pkgdata =~ m/^Directory: (\S+)/m and $dir = $1;
163 # Explicitly use the md5 lines in the Sources stanza, hence the xdigit(32) here
164 while ($pkgdata =~ m/^ ([[:xdigit:]]{32}) (\d+) (\S+)/msg) {
165 $text_out = $text_out . "$dir/$3\n";
166 }
167 }
168 }
169 }
170
171 if (length($text_out) < 2) {
172 die "which_deb: can't find $output file(s) for $pkg in $codename\n";
173 }
174
175 print $text_out;