dpkg-scansources.pl 14 KB

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