dpkg-scansources.pl 13 KB

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