dpkg-scansources.pl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #!/usr/bin/perl
  2. #
  3. # Copyright © 1999 Roderick Schertler
  4. # Copyright © 2002 Wichert Akkerman <wakkerma@debian.org>
  5. # Copyright © 2006-2009,2011-2012 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 qw();
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Control;
  26. use Dpkg::Checksums;
  27. use Dpkg::Compression::FileHandle;
  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|?' => sub { usage(); exit 0; },
  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"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  66. exit;
  67. }
  68. sub usage {
  69. printf _g(
  70. "Usage: %s [<option>...] <binary-path> [<override-file> [<path-prefix>]] > 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. "), $Dpkg::PROGNAME;
  83. }
  84. sub close_msg {
  85. my $name = shift;
  86. return sprintf(_g("error closing %s (\$? %d, \$! `%s')"),
  87. $name, $?, $!)."\n";
  88. }
  89. sub load_override {
  90. my $file = shift;
  91. local $_;
  92. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
  93. while (<$comp_file>) {
  94. s/#.*//;
  95. next if /^\s*$/;
  96. s/\s+$//;
  97. my @data = split ' ', $_, 4;
  98. unless (@data == 3 || @data == 4) {
  99. warning(_g('invalid override entry at line %d (%d fields)'),
  100. $., 0 + @data);
  101. next;
  102. }
  103. my ($package, $priority, $section, $maintainer) = @data;
  104. if (exists $override{$package}) {
  105. warning(_g('ignoring duplicate override entry for %s at line %d'),
  106. $package, $.);
  107. next;
  108. }
  109. if (!$priority{$priority}) {
  110. warning(_g('ignoring override entry for %s, invalid priority %s'),
  111. $package, $priority);
  112. next;
  113. }
  114. $override{$package} = [];
  115. $override{$package}[O_PRIORITY] = $priority;
  116. $override{$package}[O_SECTION] = $section;
  117. if (!defined $maintainer) {
  118. # do nothing
  119. }
  120. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  121. $override{$package}[O_MAINT_FROM] = [split m{\s*//\s*}, $1];
  122. $override{$package}[O_MAINT_TO] = $2;
  123. }
  124. else {
  125. $override{$package}[O_MAINT_TO] = $maintainer;
  126. }
  127. }
  128. close($comp_file);
  129. }
  130. sub load_src_override {
  131. my ($user_file, $regular_file) = @_;
  132. my ($file);
  133. local $_;
  134. if (defined $user_file) {
  135. $file = $user_file;
  136. }
  137. elsif (defined $regular_file) {
  138. my $comp = compression_guess_from_filename($regular_file);
  139. if (defined($comp)) {
  140. $file = $regular_file;
  141. my $ext = compression_get_property($comp, 'file_ext');
  142. $file =~ s/\.$ext$/.src.$ext/;
  143. } else {
  144. $file = "$regular_file.src";
  145. }
  146. return unless -e $file;
  147. }
  148. else {
  149. return;
  150. }
  151. debug "source override file $file";
  152. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
  153. while (<$comp_file>) {
  154. s/#.*//;
  155. next if /^\s*$/;
  156. s/\s+$//;
  157. my @data = split ' ', $_;
  158. unless (@data == 2) {
  159. warning(_g('invalid source override entry at line %d (%d fields)'),
  160. $., 0 + @data);
  161. next;
  162. }
  163. my ($package, $section) = @data;
  164. my $key = "source/$package";
  165. if (exists $override{$key}) {
  166. warning(_g('ignoring duplicate source override entry for %s at line %d'),
  167. $package, $.);
  168. next;
  169. }
  170. $override{$key} = [];
  171. $override{$key}[O_SECTION] = $section;
  172. }
  173. close($comp_file);
  174. }
  175. sub load_override_extra
  176. {
  177. my $extra_override = shift;
  178. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $extra_override);
  179. while (<$comp_file>) {
  180. s/\#.*//;
  181. s/\s+$//;
  182. next unless $_;
  183. my ($p, $field, $value) = split(/\s+/, $_, 3);
  184. $extra_override{$p}{$field} = $value;
  185. }
  186. close($comp_file);
  187. }
  188. # Given PREFIX and DSC-FILE, process the file and returns the fields.
  189. sub process_dsc {
  190. my ($prefix, $file) = @_;
  191. my $basename = $file;
  192. my $dir = ($basename =~ s{^(.*)/}{}) ? $1 : '';
  193. $dir = "$prefix$dir";
  194. $dir =~ s{/+$}{};
  195. $dir = '.' if $dir eq '';
  196. # Parse ‘.dsc’ file.
  197. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  198. $fields->load($file);
  199. $fields->set_options(type => CTRL_INDEX_SRC);
  200. # Get checksums
  201. my $checksums = Dpkg::Checksums->new();
  202. $checksums->add_from_file($file, key => $basename);
  203. $checksums->add_from_control($fields, use_files_for_md5 => 1);
  204. my $source = $fields->{Source};
  205. my @binary = split /\s*,\s*/, $fields->{Binary};
  206. error(_g('no binary packages specified in %s'), $file) unless (@binary);
  207. # Rename the source field to package.
  208. $fields->{Package} = $fields->{Source};
  209. delete $fields->{Source};
  210. # The priority for the source package is the highest priority of the
  211. # binary packages it produces.
  212. my @binary_by_priority = sort {
  213. ($override{$a} ? $priority{$override{$a}[O_PRIORITY]} : 0)
  214. <=>
  215. ($override{$b} ? $priority{$override{$b}[O_PRIORITY]} : 0)
  216. } @binary;
  217. my $priority_override = $override{$binary_by_priority[-1]};
  218. my $priority = $priority_override
  219. ? $priority_override->[O_PRIORITY]
  220. : undef;
  221. $fields->{Priority} = $priority if defined $priority;
  222. # For the section override, first check for a record from the source
  223. # override file, else use the regular override file.
  224. my $section_override = $override{"source/$source"} || $override{$source};
  225. my $section = $section_override
  226. ? $section_override->[O_SECTION]
  227. : undef;
  228. $fields->{Section} = $section if defined $section;
  229. # For the maintainer override, use the override record for the first
  230. # binary. Modify the maintainer if necessary.
  231. my $maintainer_override = $override{$binary[0]};
  232. if ($maintainer_override && defined $maintainer_override->[O_MAINT_TO]) {
  233. if (!defined $maintainer_override->[O_MAINT_FROM] ||
  234. grep { $fields->{Maintainer} eq $_ }
  235. @{ $maintainer_override->[O_MAINT_FROM] }) {
  236. $fields->{Maintainer} = $maintainer_override->[O_MAINT_TO];
  237. }
  238. }
  239. # Process extra override
  240. if (exists $extra_override{$source}) {
  241. my ($field, $value);
  242. while(($field, $value) = each %{$extra_override{$source}}) {
  243. $fields->{$field} = $value;
  244. }
  245. }
  246. # A directory field will be inserted just before the files field.
  247. $fields->{Directory} = $dir;
  248. $checksums->export_to_control($fields, use_files_for_md5 => 1);
  249. return $fields;
  250. }
  251. sub main {
  252. my (@out);
  253. {
  254. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  255. GetOptions(@option_spec);
  256. }
  257. @ARGV >= 1 && @ARGV <= 3 or usageerr(_g('one to three arguments expected'));
  258. push @ARGV, undef if @ARGV < 2;
  259. push @ARGV, '' if @ARGV < 3;
  260. my ($dir, $override, $prefix) = @ARGV;
  261. load_override $override if defined $override;
  262. load_src_override $src_override, $override;
  263. load_override_extra $extra_override_file if defined $extra_override_file;
  264. open my $find_fh, '-|', "find -L \Q$dir\E -name '*.dsc' -print"
  265. or syserr(_g('cannot fork for %s'), 'find');
  266. while (<$find_fh>) {
  267. chomp;
  268. s{^\./+}{};
  269. my $fields;
  270. # FIXME: Fix it instead to not die on syntax and general errors?
  271. eval {
  272. $fields = process_dsc($prefix, $_);
  273. };
  274. if ($@) {
  275. warn $@;
  276. next;
  277. }
  278. if ($no_sort) {
  279. $fields->output(\*STDOUT);
  280. print "\n";
  281. }
  282. else {
  283. push @out, $fields;
  284. }
  285. }
  286. close $find_fh or error(close_msg, 'find');
  287. if (@out) {
  288. map {
  289. $_->output(\*STDOUT);
  290. print "\n";
  291. } sort {
  292. $a->{Package} cmp $b->{Package}
  293. } @out;
  294. }
  295. return 0;
  296. }
  297. $exit = main || $exit;
  298. $exit = 1 if $exit and not $exit % 256;
  299. exit $exit;