dpkg-scansources.pl 14 KB

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