dpkg-scansources.pl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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::Cdata;
  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 %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 version {
  63. printf _g("Debian %s version %s.\n"), $progname, $version;
  64. exit;
  65. }
  66. sub usage {
  67. printf _g(
  68. "Usage: %s [<option> ...] <binarypath> [<overridefile> [<pathprefix>]] > Sources
  69. Options:
  70. -n, --no-sort don't sort by package before outputting.
  71. -s, --source-override <file>
  72. use file for additional source overrides, default
  73. is regular override file with .src appended.
  74. --debug turn debugging on.
  75. --help show this help message.
  76. --version show the version.
  77. See the man page for the full documentation.
  78. "), $progname;
  79. exit;
  80. }
  81. # Getopt::Long has some really awful defaults. This function loads it
  82. # then configures it to use more sane settings.
  83. sub getopt(@);
  84. sub configure_getopt {
  85. Getopt::Long->import(2.11);
  86. *getopt = \&Getopt::Long::GetOptions;
  87. # I'm setting this environment variable lest he sneaks more bad
  88. # defaults into the module.
  89. local $ENV{POSIXLY_CORRECT} = 1;
  90. Getopt::Long::config qw(
  91. default
  92. no_autoabbrev
  93. no_getopt_compat
  94. require_order
  95. bundling
  96. no_ignorecase
  97. );
  98. }
  99. sub close_msg {
  100. my $name = shift;
  101. return sprintf(_g("error closing %s (\$? %d, \$! `%s')"),
  102. $name, $?, $!)."\n";
  103. }
  104. sub init {
  105. configure_getopt;
  106. getopt @Option_spec or usage;
  107. }
  108. sub load_override {
  109. my $file = shift;
  110. local $_;
  111. open OVERRIDE, $file or syserr(_g("can't read override file %s"), $file);
  112. while (<OVERRIDE>) {
  113. s/#.*//;
  114. next if /^\s*$/;
  115. s/\s+$//;
  116. my @data = split ' ', $_, 4;
  117. unless (@data == 3 || @data == 4) {
  118. warning(_g("invalid override entry at line %d (%d fields)"),
  119. $., 0 + @data);
  120. next;
  121. }
  122. my ($package, $priority, $section, $maintainer) = @data;
  123. if (exists $Override{$package}) {
  124. warning(_g("ignoring duplicate override entry for %s at line %d"),
  125. $package, $.);
  126. next;
  127. }
  128. if (!$Priority{$priority}) {
  129. warning(_g("ignoring override entry for %s, invalid priority %s"),
  130. $package, $priority);
  131. next;
  132. }
  133. $Override{$package} = [];
  134. $Override{$package}[O_PRIORITY] = $priority;
  135. $Override{$package}[O_SECTION] = $section;
  136. if (!defined $maintainer) {
  137. # do nothing
  138. }
  139. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  140. $Override{$package}[O_MAINT_FROM] = [split m-\s*//\s*-, $1];
  141. $Override{$package}[O_MAINT_TO] = $2;
  142. }
  143. else {
  144. $Override{$package}[O_MAINT_TO] = $maintainer;
  145. }
  146. }
  147. close OVERRIDE or syserr(_g("error closing override file"));
  148. }
  149. sub load_src_override {
  150. my ($user_file, $regular_file) = @_;
  151. my ($file);
  152. local $_;
  153. if (defined $user_file) {
  154. $file = $user_file;
  155. }
  156. elsif (defined $regular_file) {
  157. $file = "$regular_file.src";
  158. }
  159. else {
  160. return;
  161. }
  162. debug "source override file $file";
  163. unless (open SRC_OVERRIDE, $file) {
  164. return if !defined $user_file;
  165. syserr(_g("can't read source override file %s"), $file);
  166. }
  167. while (<SRC_OVERRIDE>) {
  168. s/#.*//;
  169. next if /^\s*$/;
  170. s/\s+$//;
  171. my @data = split ' ', $_;
  172. unless (@data == 2) {
  173. warning(_g("invalid source override entry at line %d (%d fields)"),
  174. $., 0 + @data);
  175. next;
  176. }
  177. my ($package, $section) = @data;
  178. my $key = "source/$package";
  179. if (exists $Override{$key}) {
  180. warning(_g("ignoring duplicate source override entry for %s at line %d"),
  181. $package, $.);
  182. next;
  183. }
  184. $Override{$key} = [];
  185. $Override{$key}[O_SECTION] = $section;
  186. }
  187. close SRC_OVERRIDE or syserr(_g("error closing source override file"));
  188. }
  189. # Given PREFIX and DSC-FILE, process the file and returns the fields.
  190. sub process_dsc {
  191. my ($prefix, $file) = @_;
  192. # Parse ‘.dsc’ file.
  193. open(CDATA, '<', $file) || syserr(_g("cannot open %s"), $file);
  194. my $fields = parsecdata(\*CDATA,
  195. sprintf(_g("source control file %s"), $file),
  196. allow_pgp => 1);
  197. error(_g("parsing an empty file %s"), $file) unless (defined $fields);
  198. close(CDATA) || syserr(_g("cannot close %s"), $file);
  199. # Get checksums
  200. my $size;
  201. my $sums = {};
  202. getchecksums($file, $sums, \$size);
  203. my $source = $fields->{Source};
  204. my @binary = split /\s*,\s*/, $fields->{Binary};
  205. error(_g("no binary packages specified in %s"), $file) unless (@binary);
  206. # Rename the source field to package.
  207. $fields->{Package} = $fields->{Source};
  208. delete $fields->{Source};
  209. # The priority for the source package is the highest priority of the
  210. # binary packages it produces.
  211. my @binary_by_priority = sort {
  212. ($Override{$a} ? $Priority{$Override{$a}[O_PRIORITY]} : 0)
  213. <=>
  214. ($Override{$b} ? $Priority{$Override{$b}[O_PRIORITY]} : 0)
  215. } @binary;
  216. my $priority_override = $Override{$binary_by_priority[-1]};
  217. my $priority = $priority_override
  218. ? $priority_override->[O_PRIORITY]
  219. : undef;
  220. $fields->{Priority} = $priority if defined $priority;
  221. # For the section override, first check for a record from the source
  222. # override file, else use the regular override file.
  223. my $section_override = $Override{"source/$source"} || $Override{$source};
  224. my $section = $section_override
  225. ? $section_override->[O_SECTION]
  226. : undef;
  227. $fields->{Section} = $section if defined $section;
  228. # For the maintainer override, use the override record for the first
  229. # binary. Modify the maintainer if necessary.
  230. my $maintainer_override = $Override{$binary[0]};
  231. if ($maintainer_override && defined $maintainer_override->[O_MAINT_TO]) {
  232. if (!defined $maintainer_override->[O_MAINT_FROM] ||
  233. grep { $fields->{Maintainer} eq $_ }
  234. @{ $maintainer_override->[O_MAINT_FROM] }) {
  235. $fields->{Maintainer} = $maintainer_override->[O_MAINT_TO];
  236. }
  237. }
  238. # A directory field will be inserted just before the files field.
  239. my $dir;
  240. $dir = ($file =~ s-(.*)/--) ? $1 : '';
  241. $dir = "$prefix$dir";
  242. $dir =~ s-/+$--;
  243. $dir = '.' if $dir eq '';
  244. $fields->{Directory} = $dir;
  245. # The files field will get an entry for the .dsc file itself.
  246. foreach my $alg (@check_supported) {
  247. if ($alg eq "md5") {
  248. $fields->{Files} =~ s/^\n/\n $sums->{$alg} $size $file\n/;
  249. } else {
  250. my $name = "Checksums-" . ucfirst($alg);
  251. $fields->{$name} =~ s/^\n/\n $sums->{$alg} $size $file\n/
  252. if defined $fields->{$name};
  253. }
  254. }
  255. return $fields;
  256. }
  257. # FIXME: Try to reuse from Dpkg::Source::Package
  258. use Dpkg::Deps qw(@src_dep_fields);
  259. my @src_fields = (qw(Format Package Binary Architecture Version Origin
  260. Maintainer Uploaders Dm-Upload-Allowed Homepage
  261. Standards-Version Vcs-Browser Vcs-Arch Vcs-Bzr
  262. Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn Vcs-Svn),
  263. @src_dep_fields,
  264. qw(Directory Files Checksums-Md5 Checksums-Sha1 Checksums-Sha256));
  265. sub main {
  266. my (@out);
  267. init;
  268. @ARGV >= 1 && @ARGV <= 3 or usageerr(_g("1 to 3 args expected\n"));
  269. push @ARGV, undef if @ARGV < 2;
  270. push @ARGV, '' if @ARGV < 3;
  271. my ($dir, $override, $prefix) = @ARGV;
  272. load_override $override if defined $override;
  273. load_src_override $Src_override, $override;
  274. open FIND, "find \Q$dir\E -follow -name '*.dsc' -print |"
  275. or syserr(_g("can't fork"));
  276. while (<FIND>) {
  277. chomp;
  278. s-^\./+--;
  279. my $fields;
  280. # FIXME: Fix it instead to not die on syntax and general errors?
  281. eval {
  282. $fields = process_dsc($prefix, $_);
  283. };
  284. if ($@) {
  285. warn $@;
  286. next;
  287. }
  288. tied(%{$fields})->set_field_importance(@src_fields);
  289. if ($No_sort) {
  290. tied(%{$fields})->output(\*STDOUT);
  291. print "\n";
  292. }
  293. else {
  294. push @out, $fields;
  295. }
  296. }
  297. close FIND or error(close_msg, 'find');
  298. if (@out) {
  299. map {
  300. tied(%{$_})->output(\*STDOUT);
  301. print "\n";
  302. } sort {
  303. $a->{Package} cmp $b->{Package}
  304. } @out;
  305. }
  306. return 0;
  307. }
  308. $Exit = main || $Exit;
  309. $Exit = 1 if $Exit and not $Exit % 256;
  310. exit $Exit;