dpkg-scansources.pl 14 KB

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