dpkg-scansources.pl 14 KB

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