dpkg-scansources.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #!/usr/bin/perl
  2. #
  3. # Copyright © 1999 Roderick Schertler
  4. # Copyright © 2002 Wichert Akkerman <wakkerma@debian.org>
  5. # Copyright © 2006-2009 Guillem Jover <guillem@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. # Errors with a single package are warned about but don't affect the
  20. # exit code. Only errors which affect everything cause a non-zero exit.
  21. #
  22. # Dependencies are by request non-existant. I used to use the MD5 and
  23. # Proc::WaitStat modules.
  24. use strict;
  25. use warnings;
  26. use Dpkg;
  27. use Dpkg::Gettext;
  28. use Dpkg::ErrorHandling;
  29. use Dpkg::Control;
  30. use Dpkg::Checksums;
  31. textdomain("dpkg-dev");
  32. use Getopt::Long ();
  33. my $Exit = 0;
  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 %Extra_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 $Extra_override_file = undef;
  54. my @Option_spec = (
  55. 'debug!' => \$Debug,
  56. 'help!' => \&usage,
  57. 'no-sort|n' => \$No_sort,
  58. 'source-override|s=s' => \$Src_override,
  59. 'extra-override|e=s' => \$Extra_override_file,
  60. 'version' => \&version,
  61. );
  62. sub debug {
  63. print @_, "\n" if $Debug;
  64. }
  65. sub version {
  66. printf _g("Debian %s version %s.\n"), $progname, $version;
  67. exit;
  68. }
  69. sub usage {
  70. printf _g(
  71. "Usage: %s [<option> ...] <binarypath> [<overridefile> [<pathprefix>]] > Sources
  72. Options:
  73. -n, --no-sort don't sort by package before outputting.
  74. -e, --extra-override <file>
  75. use extra override file.
  76. -s, --source-override <file>
  77. use file for additional source overrides, default
  78. is regular override file with .src appended.
  79. --debug turn debugging on.
  80. --help show this help message.
  81. --version show the version.
  82. See the man page for the full documentation.
  83. "), $progname;
  84. exit;
  85. }
  86. # Getopt::Long has some really awful defaults. This function loads it
  87. # then configures it to use more sane settings.
  88. sub getopt(@);
  89. sub configure_getopt {
  90. Getopt::Long->import(2.11);
  91. *getopt = \&Getopt::Long::GetOptions;
  92. # I'm setting this environment variable lest he sneaks more bad
  93. # defaults into the module.
  94. local $ENV{POSIXLY_CORRECT} = 1;
  95. Getopt::Long::config qw(
  96. default
  97. no_autoabbrev
  98. no_getopt_compat
  99. require_order
  100. bundling
  101. no_ignorecase
  102. );
  103. }
  104. sub close_msg {
  105. my $name = shift;
  106. return sprintf(_g("error closing %s (\$? %d, \$! `%s')"),
  107. $name, $?, $!)."\n";
  108. }
  109. sub init {
  110. configure_getopt;
  111. getopt @Option_spec or usage;
  112. }
  113. sub load_override {
  114. my $file = shift;
  115. local $_;
  116. open OVERRIDE, $file or syserr(_g("can't read override file %s"), $file);
  117. while (<OVERRIDE>) {
  118. s/#.*//;
  119. next if /^\s*$/;
  120. s/\s+$//;
  121. my @data = split ' ', $_, 4;
  122. unless (@data == 3 || @data == 4) {
  123. warning(_g("invalid override entry at line %d (%d fields)"),
  124. $., 0 + @data);
  125. next;
  126. }
  127. my ($package, $priority, $section, $maintainer) = @data;
  128. if (exists $Override{$package}) {
  129. warning(_g("ignoring duplicate override entry for %s at line %d"),
  130. $package, $.);
  131. next;
  132. }
  133. if (!$Priority{$priority}) {
  134. warning(_g("ignoring override entry for %s, invalid priority %s"),
  135. $package, $priority);
  136. next;
  137. }
  138. $Override{$package} = [];
  139. $Override{$package}[O_PRIORITY] = $priority;
  140. $Override{$package}[O_SECTION] = $section;
  141. if (!defined $maintainer) {
  142. # do nothing
  143. }
  144. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  145. $Override{$package}[O_MAINT_FROM] = [split m-\s*//\s*-, $1];
  146. $Override{$package}[O_MAINT_TO] = $2;
  147. }
  148. else {
  149. $Override{$package}[O_MAINT_TO] = $maintainer;
  150. }
  151. }
  152. close OVERRIDE or syserr(_g("error closing override file"));
  153. }
  154. sub load_src_override {
  155. my ($user_file, $regular_file) = @_;
  156. my ($file);
  157. local $_;
  158. if (defined $user_file) {
  159. $file = $user_file;
  160. }
  161. elsif (defined $regular_file) {
  162. $file = "$regular_file.src";
  163. }
  164. else {
  165. return;
  166. }
  167. debug "source override file $file";
  168. unless (open SRC_OVERRIDE, $file) {
  169. return if !defined $user_file;
  170. syserr(_g("can't read source override file %s"), $file);
  171. }
  172. while (<SRC_OVERRIDE>) {
  173. s/#.*//;
  174. next if /^\s*$/;
  175. s/\s+$//;
  176. my @data = split ' ', $_;
  177. unless (@data == 2) {
  178. warning(_g("invalid source override entry at line %d (%d fields)"),
  179. $., 0 + @data);
  180. next;
  181. }
  182. my ($package, $section) = @data;
  183. my $key = "source/$package";
  184. if (exists $Override{$key}) {
  185. warning(_g("ignoring duplicate source override entry for %s at line %d"),
  186. $package, $.);
  187. next;
  188. }
  189. $Override{$key} = [];
  190. $Override{$key}[O_SECTION] = $section;
  191. }
  192. close SRC_OVERRIDE or syserr(_g("error closing source override file"));
  193. }
  194. sub load_override_extra
  195. {
  196. my $extra_override = shift;
  197. open(OVERRIDE, "<", $extra_override) or
  198. syserr(_g("Couldn't open override file %s"), $extra_override);
  199. while (<OVERRIDE>) {
  200. s/\#.*//;
  201. s/\s+$//;
  202. next unless $_;
  203. my ($p, $field, $value) = split(/\s+/, $_, 3);
  204. $Extra_Override{$p}{$field} = $value;
  205. }
  206. close(OVERRIDE);
  207. }
  208. # Given PREFIX and DSC-FILE, process the file and returns the fields.
  209. sub process_dsc {
  210. my ($prefix, $file) = @_;
  211. # Parse ‘.dsc’ file.
  212. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  213. $fields->parse($file);
  214. $fields->set_options(type => CTRL_APT_SRC);
  215. # Get checksums
  216. my $size;
  217. my $sums = {};
  218. getchecksums($file, $sums, \$size);
  219. my $source = $fields->{Source};
  220. my @binary = split /\s*,\s*/, $fields->{Binary};
  221. error(_g("no binary packages specified in %s"), $file) unless (@binary);
  222. # Rename the source field to package.
  223. $fields->{Package} = $fields->{Source};
  224. delete $fields->{Source};
  225. # The priority for the source package is the highest priority of the
  226. # binary packages it produces.
  227. my @binary_by_priority = sort {
  228. ($Override{$a} ? $Priority{$Override{$a}[O_PRIORITY]} : 0)
  229. <=>
  230. ($Override{$b} ? $Priority{$Override{$b}[O_PRIORITY]} : 0)
  231. } @binary;
  232. my $priority_override = $Override{$binary_by_priority[-1]};
  233. my $priority = $priority_override
  234. ? $priority_override->[O_PRIORITY]
  235. : undef;
  236. $fields->{Priority} = $priority if defined $priority;
  237. # For the section override, first check for a record from the source
  238. # override file, else use the regular override file.
  239. my $section_override = $Override{"source/$source"} || $Override{$source};
  240. my $section = $section_override
  241. ? $section_override->[O_SECTION]
  242. : undef;
  243. $fields->{Section} = $section if defined $section;
  244. # For the maintainer override, use the override record for the first
  245. # binary. Modify the maintainer if necessary.
  246. my $maintainer_override = $Override{$binary[0]};
  247. if ($maintainer_override && defined $maintainer_override->[O_MAINT_TO]) {
  248. if (!defined $maintainer_override->[O_MAINT_FROM] ||
  249. grep { $fields->{Maintainer} eq $_ }
  250. @{ $maintainer_override->[O_MAINT_FROM] }) {
  251. $fields->{Maintainer} = $maintainer_override->[O_MAINT_TO];
  252. }
  253. }
  254. # Process extra override
  255. if (exists $Extra_Override{$source}) {
  256. my ($field, $value);
  257. while(($field, $value) = each %{$Extra_Override{$source}}) {
  258. $fields->{$field} = $value;
  259. }
  260. }
  261. # A directory field will be inserted just before the files field.
  262. my $dir;
  263. $dir = ($file =~ s-(.*)/--) ? $1 : '';
  264. $dir = "$prefix$dir";
  265. $dir =~ s-/+$--;
  266. $dir = '.' if $dir eq '';
  267. $fields->{Directory} = $dir;
  268. # The files field will get an entry for the .dsc file itself.
  269. foreach my $alg (@check_supported) {
  270. if ($alg eq "md5") {
  271. $fields->{Files} =~ s/^\n/\n $sums->{$alg} $size $file\n/;
  272. } else {
  273. my $name = "Checksums-" . ucfirst($alg);
  274. $fields->{$name} =~ s/^\n/\n $sums->{$alg} $size $file\n/
  275. if defined $fields->{$name};
  276. }
  277. }
  278. return $fields;
  279. }
  280. sub main {
  281. my (@out);
  282. init;
  283. @ARGV >= 1 && @ARGV <= 3 or usageerr(_g("1 to 3 args expected\n"));
  284. push @ARGV, undef if @ARGV < 2;
  285. push @ARGV, '' if @ARGV < 3;
  286. my ($dir, $override, $prefix) = @ARGV;
  287. load_override $override if defined $override;
  288. load_src_override $Src_override, $override;
  289. load_extra_override $Extra_override_file if defined $Extra_override_file;
  290. open FIND, "find \Q$dir\E -follow -name '*.dsc' -print |"
  291. or syserr(_g("cannot fork for %s"), "find");
  292. while (<FIND>) {
  293. chomp;
  294. s-^\./+--;
  295. my $fields;
  296. # FIXME: Fix it instead to not die on syntax and general errors?
  297. eval {
  298. $fields = process_dsc($prefix, $_);
  299. };
  300. if ($@) {
  301. warn $@;
  302. next;
  303. }
  304. if ($No_sort) {
  305. $fields->output(\*STDOUT);
  306. print "\n";
  307. }
  308. else {
  309. push @out, $fields;
  310. }
  311. }
  312. close FIND or error(close_msg, 'find');
  313. if (@out) {
  314. map {
  315. $_->output(\*STDOUT);
  316. print "\n";
  317. } sort {
  318. $a->{Package} cmp $b->{Package}
  319. } @out;
  320. }
  321. return 0;
  322. }
  323. $Exit = main || $Exit;
  324. $Exit = 1 if $Exit and not $Exit % 256;
  325. exit $Exit;