dpkg-scansources.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. #!/usr/bin/perl -w
  2. use strict;
  3. # $Id$
  4. # Copyright (C) 1999 Roderick Schertler
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or (at
  9. # your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # General Public License for more details.
  15. #
  16. # For a copy of the GNU General Public License write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. # User documentation is at the __END__.
  19. #
  20. # Errors with a single package are warned about but don't affect the
  21. # exit code. Only errors which affect everything cause a non-zero exit.
  22. #
  23. # Dependencies are by request non-existant. I used to use the MD5 and
  24. # Proc::WaitStat modules.
  25. use Getopt::Long ();
  26. my $Exit = 0;
  27. (my $Me = $0) =~ s-.*/--;
  28. my $Version = q$Revision$ =~ /(\d\S+)/ ? $1 : '?';
  29. # %Override is a hash of lists. The subs following describe what's in
  30. # the lists.
  31. my %Override;
  32. sub O_PRIORITY () { 0 }
  33. sub O_SECTION () { 1 }
  34. sub O_MAINT_FROM () { 2 } # undef for non-specific, else listref
  35. sub O_MAINT_TO () { 3 } # undef if there's no maint override
  36. my %Priority = (
  37. 'extra' => 1,
  38. 'optional' => 2,
  39. 'standard' => 3,
  40. 'important' => 4,
  41. 'required' => 5,
  42. );
  43. # Switches
  44. my $Debug = 0;
  45. my $No_sort = 0;
  46. my $Src_override = undef;
  47. my @Option_spec = (
  48. 'debug!' => \$Debug,
  49. 'help!' => sub { usage() },
  50. 'no-sort|n!' => \$No_sort,
  51. 'source-override|s=s' => \$Src_override,
  52. 'version' => sub { print "$Me version $Version\n"; exit },
  53. );
  54. my $Usage = <<EOF;
  55. usage: $Me [switch]... binary-dir [override-file [path-prefix]] > Sources
  56. switches:
  57. --debug turn debugging on
  58. --help show this and then die
  59. -n, --no-sort don\'t sort by package before outputting
  60. -s, --source-override file
  61. use file for additional source overrides, default
  62. is regular override file with .src appended
  63. --version show the version and exit
  64. See the man page or \`perldoc $Me\' for the full documentation.
  65. EOF
  66. sub debug {
  67. print @_, "\n" if $Debug;
  68. }
  69. sub xwarndie_mess {
  70. my @mess = ("$Me: ", @_);
  71. $mess[$#mess] =~ s/:$/: $!\n/; # XXX loses if it's really /:\n/
  72. return @mess;
  73. }
  74. sub xdie {
  75. die xwarndie_mess @_;
  76. }
  77. sub xwarn {
  78. warn xwarndie_mess @_;
  79. $Exit ||= 1;
  80. }
  81. sub xwarn_noerror {
  82. warn xwarndie_mess @_;
  83. }
  84. sub usage {
  85. xwarn @_ if @_;
  86. die $Usage;
  87. }
  88. # Getopt::Long has some really awful defaults. This function loads it
  89. # then configures it to use more sane settings.
  90. sub getopt;
  91. sub configure_getopt {
  92. Getopt::Long->import(2.11);
  93. *getopt = \&Getopt::Long::GetOptions;
  94. # I'm setting this environment variable lest he sneaks more bad
  95. # defaults into the module.
  96. local $ENV{POSIXLY_CORRECT} = 1;
  97. Getopt::Long::config qw(
  98. default
  99. no_autoabbrev
  100. no_getopt_compat
  101. require_order
  102. bundling
  103. no_ignorecase
  104. );
  105. }
  106. sub close_msg {
  107. my $name = shift;
  108. return "error closing $name (\$? $?, \$! `$!')\n";
  109. }
  110. sub init {
  111. configure_getopt;
  112. getopt @Option_spec or usage;
  113. }
  114. sub load_override {
  115. my $file = shift;
  116. local $_;
  117. open OVERRIDE, $file or xdie "can't read override file $file:";
  118. while (<OVERRIDE>) {
  119. s/#.*//;
  120. next if /^\s*$/;
  121. s/\s+$//;
  122. my @data = split ' ', $_, 4;
  123. unless (@data == 3 || @data == 4) {
  124. xwarn_noerror "invalid override entry at line $. (",
  125. 0+@data, " fields)\n";
  126. next;
  127. }
  128. my ($package, $priority, $section, $maintainer) = @data;
  129. if (exists $Override{$package}) {
  130. xwarn_noerror "ignoring duplicate override entry for $package",
  131. " at line $.\n";
  132. next;
  133. }
  134. if (!$Priority{$priority}) {
  135. xwarn_noerror "ignoring override entry for $package,",
  136. " invalid priority $priority\n";
  137. next;
  138. }
  139. $Override{$package} = [];
  140. $Override{$package}[O_PRIORITY] = $priority;
  141. $Override{$package}[O_SECTION] = $section;
  142. if (!defined $maintainer) {
  143. # do nothing
  144. }
  145. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  146. $Override{$package}[O_MAINT_FROM] = [split m-\s*//\s*-, $1];
  147. $Override{$package}[O_MAINT_TO] = $2;
  148. }
  149. else {
  150. $Override{$package}[O_MAINT_TO] = $maintainer;
  151. }
  152. }
  153. close OVERRIDE or xdie "error closing override file:";
  154. }
  155. sub load_src_override {
  156. my ($user_file, $regular_file) = @_;
  157. my ($file);
  158. local $_;
  159. if (defined $user_file) {
  160. $file = $user_file;
  161. }
  162. elsif (defined $regular_file) {
  163. $file = "$regular_file.src";
  164. }
  165. else {
  166. return;
  167. }
  168. debug "source override file $file";
  169. unless (open SRC_OVERRIDE, $file) {
  170. return if !defined $user_file;
  171. xdie "can't read source override file $file:";
  172. }
  173. while (<SRC_OVERRIDE>) {
  174. s/#.*//;
  175. next if /^\s*$/;
  176. s/\s+$//;
  177. my @data = split ' ', $_;
  178. unless (@data == 2) {
  179. xwarn_noerror "invalid source override entry at line $. (",
  180. 0+@data, " fields)\n";
  181. next;
  182. }
  183. my ($package, $section) = @data;
  184. my $key = "source/$package";
  185. if (exists $Override{$key}) {
  186. xwarn_noerror "ignoring duplicate source override entry",
  187. " for $package at line $.\n";
  188. next;
  189. }
  190. $Override{$key} = [];
  191. $Override{$key}[O_SECTION] = $section;
  192. }
  193. close SRC_OVERRIDE or xdie "error closing source override file:";
  194. }
  195. # Given FILENAME (for error reporting) and STRING, drop the PGP info
  196. # from the string and undo the encoding (if present) and return it.
  197. sub de_pgp {
  198. my ($file, $s) = @_;
  199. if ($s =~ s/^-----BEGIN PGP SIGNED MESSAGE-----.*?\n\n//s) {
  200. unless ($s =~ s/\n
  201. -----BEGIN\040PGP\040SIGNATURE-----\n
  202. .*?\n
  203. -----END\040PGP\040SIGNATURE-----\n
  204. //xs) {
  205. xwarn_noerror "$file has PGP start token but not end token\n";
  206. return;
  207. }
  208. $s =~ s/^- //mg;
  209. }
  210. return $s;
  211. }
  212. # Load DSC-FILE and return its size, MD5 and translated (de-PGPed)
  213. # contents.
  214. sub read_dsc {
  215. my $file = shift;
  216. my ($size, $md5, $nread, $contents);
  217. unless (open FILE, $file) {
  218. xwarn_noerror "can't read $file:";
  219. return;
  220. }
  221. $size = -s FILE;
  222. unless (defined $size) {
  223. xwarn_noerror "error doing fstat on $file:";
  224. return;
  225. }
  226. $contents = '';
  227. do {
  228. $nread = read FILE, $contents, 16*1024, length $contents;
  229. unless (defined $nread) {
  230. xwarn_noerror "error reading from $file:";
  231. return;
  232. }
  233. } while $nread > 0;
  234. # Rewind the .dsc file and feed it to md5sum as stdin.
  235. my $pid = open MD5, '-|';
  236. unless (defined $pid) {
  237. xwarn_noerror "can't fork:";
  238. return;
  239. }
  240. if (!$pid) {
  241. open STDIN, '<&FILE' or xdie "can't dup $file:";
  242. seek STDIN, 0, 0 or xdie "can't rewind $file:";
  243. exec 'md5sum' or xdie "can't exec md5sum:";
  244. }
  245. chomp($md5 = join '', <MD5>);
  246. unless (close MD5) {
  247. xwarn_noerror close_msg 'md5sum';
  248. return;
  249. }
  250. unless (length($md5) == 32 && $md5 !~ /[^\da-f]/i) {
  251. xwarn_noerror "invalid md5 output for $file ($md5)\n";
  252. return;
  253. }
  254. unless (close FILE) {
  255. xwarn_noerror "error closing $file:";
  256. return;
  257. }
  258. $contents = de_pgp $file, $contents;
  259. return unless defined $contents;
  260. return $size, $md5, $contents;
  261. }
  262. # Given PREFIX and DSC-FILE, process the file and returning the source
  263. # package name and index record.
  264. sub process_dsc {
  265. my ($prefix, $file) = @_;
  266. my ($source, @binary, $priority, $section, $maintainer_override,
  267. $dir, $dir_field, $dsc_field_start);
  268. my ($size, $md5, $contents) = read_dsc $file or return;
  269. # Allow blank lines at the end of a file, because the other programs
  270. # do.
  271. $contents =~ s/\n\n+\Z/\n/;
  272. if ($contents =~ /^\n/ || $contents =~ /\n\n/) {
  273. xwarn_noerror "$file invalid (contains blank line)\n";
  274. return;
  275. }
  276. # Take the $contents and create a list of (possibly multi-line)
  277. # fields. Fields can be continued by starting the next line with
  278. # white space. The tricky part is I don't want to modify the data
  279. # at all, so I can't just collapse continued fields.
  280. #
  281. # Implementation is to start from the last line and work backwards
  282. # to the second. If this line starts with space, append it to the
  283. # previous line and undef it. When done drop the undef entries.
  284. my @line = split /\n/, $contents;
  285. for (my $i = $#line; $i > 0; $i--) {
  286. if ($line[$i] =~ /^\s/) {
  287. $line[$i-1] .= "\n$line[$i]";
  288. $line[$i] = undef;
  289. }
  290. }
  291. my @field = map { "$_\n" } grep { defined } @line;
  292. # Extract information from the record.
  293. for my $orig_field (@field) {
  294. my $s = $orig_field;
  295. $s =~ s/\s+$//;
  296. $s =~ s/\n\s+/ /g;
  297. unless ($s =~ s/^([^:\s]+):\s*//) {
  298. xwarn_noerror "invalid field in $file: $orig_field";
  299. return;
  300. }
  301. my ($key, $val) = (lc $1, $s);
  302. # $source
  303. if ($key eq 'source') {
  304. if (defined $source) {
  305. xwarn_noerror "duplicate source field in $file\n";
  306. return;
  307. }
  308. if ($val =~ /\s/) {
  309. xwarn_noerror "invalid source field in $file\n";
  310. return;
  311. }
  312. $source = $val;
  313. next;
  314. }
  315. # @binary
  316. if ($key eq 'binary') {
  317. if (@binary) {
  318. xwarn_noerror "duplicate binary field in $file\n";
  319. return;
  320. }
  321. @binary = split /\s*,\s*/, $val;
  322. unless (@binary) {
  323. xwarn_noerror "no binary packages specified in $file\n";
  324. return;
  325. }
  326. }
  327. }
  328. # The priority for the source package is the highest priority of the
  329. # binary packages it produces.
  330. my @binary_by_priority = sort {
  331. ($Override{$a} ? $Priority{$Override{$a}[O_PRIORITY]} : 0)
  332. <=>
  333. ($Override{$b} ? $Priority{$Override{$b}[O_PRIORITY]} : 0)
  334. } @binary;
  335. my $priority_override = $Override{$binary_by_priority[-1]};
  336. $priority = $priority_override
  337. ? $priority_override->[O_PRIORITY]
  338. : undef;
  339. # For the section override, first check for a record from the source
  340. # override file, else use the regular override file.
  341. my $section_override = $Override{"source/$source"} || $Override{$source};
  342. $section = $section_override
  343. ? $section_override->[O_SECTION]
  344. : undef;
  345. # For the maintainer override, use the override record for the first
  346. # binary.
  347. $maintainer_override = $Override{$binary[0]};
  348. # A directory field will be inserted just before the files field.
  349. $dir = ($file =~ s-(.*)/--) ? $1 : '';
  350. $dir = "$prefix$dir";
  351. $dir =~ s-/+$--;
  352. $dir = '.' if $dir eq '';
  353. $dir_field .= "Directory: $dir\n";
  354. # The files field will get an entry for the .dsc file itself.
  355. $dsc_field_start = "Files:\n $md5 $size $file\n";
  356. # Loop through @field, doing nececessary processing and building up
  357. # @new_field.
  358. my @new_field;
  359. for (@field) {
  360. # Rename the source field to package.
  361. s/^Source:/Package:/i;
  362. # Override the user's priority field.
  363. if (/^Priority:/i && defined $priority) {
  364. $_ = "Priority: $priority\n";
  365. undef $priority;
  366. }
  367. # Override the user's section field.
  368. if (/^Section:/i && defined $section) {
  369. $_ = "Section: $section\n";
  370. undef $section;
  371. }
  372. # Insert the directory line just before the files entry, and add
  373. # the dsc file to the files list.
  374. if (defined $dir_field && s/^Files:\s*//i) {
  375. push @new_field, $dir_field;
  376. $dir_field = undef;
  377. $_ = " $_" if length;
  378. $_ = "$dsc_field_start$_";
  379. }
  380. # Modify the maintainer if necessary.
  381. if ($maintainer_override
  382. && defined $maintainer_override->[O_MAINT_TO]
  383. && /^Maintainer:\s*(.*)\n/is) {
  384. my $maintainer = $1;
  385. $maintainer =~ s/\n\s+/ /g;
  386. if (!defined $maintainer_override->[O_MAINT_FROM]
  387. || grep { $maintainer eq $_ }
  388. @{ $maintainer_override->[O_MAINT_FROM] }){
  389. $_ = "Maintainer: $maintainer_override->[O_MAINT_TO]\n";
  390. }
  391. }
  392. }
  393. continue {
  394. push @new_field, $_ if defined $_;
  395. }
  396. # If there was no files entry, add one.
  397. if (defined $dir_field) {
  398. push @new_field, $dir_field;
  399. push @new_field, $dsc_field_start;
  400. }
  401. # Add the section field if it didn't override one the user supplied.
  402. if (defined $section) {
  403. # If the record starts with a package field put it after that,
  404. # otherwise put it first.
  405. my $pos = $new_field[0] =~ /^Package:/i ? 1 : 0;
  406. splice @new_field, $pos, 0, "Section: $section\n";
  407. }
  408. # Add the priority field if it didn't override one the user supplied.
  409. if (defined $priority) {
  410. # If the record starts with a package field put it after that,
  411. # otherwise put it first.
  412. my $pos = $new_field[0] =~ /^Package:/i ? 1 : 0;
  413. splice @new_field, $pos, 0, "Priority: $priority\n";
  414. }
  415. return $source, join '', @new_field, "\n";
  416. }
  417. sub main {
  418. my (@out);
  419. init;
  420. @ARGV >= 1 && @ARGV <= 3 or usage "1 to 3 args expected\n";
  421. push @ARGV, undef if @ARGV < 2;
  422. push @ARGV, '' if @ARGV < 3;
  423. my ($dir, $override, $prefix) = @ARGV;
  424. load_override $override if defined $override;
  425. load_src_override $Src_override, $override;
  426. open FIND, "find \Q$dir\E -follow -name '*.dsc' -print |"
  427. or xdie "can't fork:";
  428. while (<FIND>) {
  429. chomp;
  430. s-^\./+--;
  431. my ($source, $out) = process_dsc $prefix, $_ or next;
  432. if ($No_sort) {
  433. print $out;
  434. }
  435. else {
  436. push @out, [$source, $out];
  437. }
  438. }
  439. close FIND or xdie close_msg 'find';
  440. if (@out) {
  441. print map { $_->[1] } sort { $a->[0] cmp $b->[0] } @out;
  442. }
  443. return 0;
  444. }
  445. $Exit = main || $Exit;
  446. $Exit = 1 if $Exit and not $Exit % 256;
  447. exit $Exit;
  448. __END__
  449. =head1 NAME
  450. dpkg-scansources - prog
  451. =head1 SYNOPSIS
  452. B<dpkg-scansources> [switch]... I<binary-dir> [I<override-file>
  453. [I<path-prefix>]] > Sources
  454. =head1 DESCRIPTION
  455. B<dpkg-scansources> scans the given I<binary-dir> for F<.dsc> files.
  456. These are used to create a Debian source index, which is output to
  457. stdout.
  458. The I<override-file>, if given, is used to set priorities in the resulting
  459. index records and to override the maintainer field given in the F<.dsc>
  460. files. See L<dpkg-scanpackages> for the format of this file. NB: Since
  461. the override file is indexed by binary, not source, packages, there's a bit
  462. of a problem here. The current implementation uses the highest priority of
  463. all the binary packages produced by a F<.dsc> file for the priority of the
  464. source package, and the override entry for the first binary package listed
  465. in the F<.dsc> file to modify maintainer information. This might change.
  466. The I<path-prefix>, if given, is prepended to the directory field in the
  467. generated source index. You generally use this to make the directory
  468. fields contain the path from the top of the Debian archive hierarchy.
  469. =head1 OPTIONS
  470. =over 4
  471. =item B<--debug>
  472. Turn debugging on.
  473. =item B<--help>
  474. Show the usage message and die.
  475. =item B<-n>, B<--no-sort>
  476. Don't sort the index records. Normally they are sorted by source package
  477. name.
  478. =item B<-s>, B<--source-override> I<file>
  479. Use I<file> as the source override file. The default is the name of the
  480. override file you specified with I<.src> appended.
  481. The source override file is in a different format from the binary override
  482. file. It contains only two whitespace separated fields, the first is the
  483. source package name and the second is the section. Blank lines and comment
  484. lines are ignored in the normal manner. If a package appears in both files
  485. the source override takes precedence for setting the section.
  486. =item B<--version>
  487. Print the version number and exit.
  488. =back
  489. =head1 SEE ALSO
  490. dpkg-scanpackages(8)
  491. =head1 AUTHOR
  492. Roderick Schertler <roderick@argon.org>
  493. =cut