dpkg-scansources.pl 9.8 KB

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