dpkg-scansources.pl 14 KB

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