dpkg-scansources.pl 9.6 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 ();
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Util qw(:list);
  26. use Dpkg::Control;
  27. use Dpkg::Checksums;
  28. use Dpkg::Compression::FileHandle;
  29. use Dpkg::Compression;
  30. textdomain('dpkg-dev');
  31. # Errors with a single package are warned about but don't affect the
  32. # exit code. Only errors which affect everything cause a non-zero exit.
  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|?' => sub { usage(); exit 0; },
  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"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  67. exit;
  68. }
  69. sub usage {
  70. printf _g(
  71. "Usage: %s [<option>...] <binary-path> [<override-file> [<path-prefix>]] > 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. "), $Dpkg::PROGNAME;
  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::Compression::FileHandle->new(filename => $file);
  94. while (<$comp_file>) {
  95. s/#.*//;
  96. next if /^\s*$/;
  97. s/\s+$//;
  98. my @data = split ' ', $_, 4;
  99. unless (@data == 3 || @data == 4) {
  100. warning(_g('invalid override entry at line %d (%d fields)'),
  101. $., 0 + @data);
  102. next;
  103. }
  104. my ($package, $priority, $section, $maintainer) = @data;
  105. if (exists $override{$package}) {
  106. warning(_g('ignoring duplicate override entry for %s at line %d'),
  107. $package, $.);
  108. next;
  109. }
  110. if (!$priority{$priority}) {
  111. warning(_g('ignoring override entry for %s, invalid priority %s'),
  112. $package, $priority);
  113. next;
  114. }
  115. $override{$package} = [];
  116. $override{$package}[O_PRIORITY] = $priority;
  117. $override{$package}[O_SECTION] = $section;
  118. if (!defined $maintainer) {
  119. # do nothing
  120. }
  121. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  122. $override{$package}[O_MAINT_FROM] = [split m{\s*//\s*}, $1];
  123. $override{$package}[O_MAINT_TO] = $2;
  124. }
  125. else {
  126. $override{$package}[O_MAINT_TO] = $maintainer;
  127. }
  128. }
  129. close($comp_file);
  130. }
  131. sub load_src_override {
  132. my ($user_file, $regular_file) = @_;
  133. my ($file);
  134. local $_;
  135. if (defined $user_file) {
  136. $file = $user_file;
  137. }
  138. elsif (defined $regular_file) {
  139. my $comp = compression_guess_from_filename($regular_file);
  140. if (defined($comp)) {
  141. $file = $regular_file;
  142. my $ext = compression_get_property($comp, 'file_ext');
  143. $file =~ s/\.$ext$/.src.$ext/;
  144. } else {
  145. $file = "$regular_file.src";
  146. }
  147. return unless -e $file;
  148. }
  149. else {
  150. return;
  151. }
  152. debug "source override file $file";
  153. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
  154. while (<$comp_file>) {
  155. s/#.*//;
  156. next if /^\s*$/;
  157. s/\s+$//;
  158. my @data = split ' ', $_;
  159. unless (@data == 2) {
  160. warning(_g('invalid source override entry at line %d (%d fields)'),
  161. $., 0 + @data);
  162. next;
  163. }
  164. my ($package, $section) = @data;
  165. my $key = "source/$package";
  166. if (exists $override{$key}) {
  167. warning(_g('ignoring duplicate source override entry for %s at line %d'),
  168. $package, $.);
  169. next;
  170. }
  171. $override{$key} = [];
  172. $override{$key}[O_SECTION] = $section;
  173. }
  174. close($comp_file);
  175. }
  176. sub load_override_extra
  177. {
  178. my $extra_override = shift;
  179. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $extra_override);
  180. while (<$comp_file>) {
  181. s/\#.*//;
  182. s/\s+$//;
  183. next unless $_;
  184. my ($p, $field, $value) = split(/\s+/, $_, 3);
  185. $extra_override{$p}{$field} = $value;
  186. }
  187. close($comp_file);
  188. }
  189. # Given PREFIX and DSC-FILE, process the file and returns the fields.
  190. sub process_dsc {
  191. my ($prefix, $file) = @_;
  192. my $basename = $file;
  193. my $dir = ($basename =~ s{^(.*)/}{}) ? $1 : '';
  194. $dir = "$prefix$dir";
  195. $dir =~ s{/+$}{};
  196. $dir = '.' if $dir eq '';
  197. # Parse ‘.dsc’ file.
  198. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  199. $fields->load($file);
  200. $fields->set_options(type => CTRL_INDEX_SRC);
  201. # Get checksums
  202. my $checksums = Dpkg::Checksums->new();
  203. $checksums->add_from_file($file, key => $basename);
  204. $checksums->add_from_control($fields, use_files_for_md5 => 1);
  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. any { $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. $fields->{Directory} = $dir;
  249. $checksums->export_to_control($fields, use_files_for_md5 => 1);
  250. return $fields;
  251. }
  252. sub main {
  253. my (@out);
  254. {
  255. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  256. GetOptions(@option_spec);
  257. }
  258. usageerr(_g('one to three arguments expected'))
  259. if @ARGV < 1 or @ARGV > 3;
  260. push @ARGV, undef if @ARGV < 2;
  261. push @ARGV, '' if @ARGV < 3;
  262. my ($dir, $override, $prefix) = @ARGV;
  263. load_override $override if defined $override;
  264. load_src_override $src_override, $override;
  265. load_override_extra $extra_override_file if defined $extra_override_file;
  266. open my $find_fh, '-|', "find -L \Q$dir\E -name '*.dsc' -print"
  267. or syserr(_g('cannot fork for %s'), 'find');
  268. while (<$find_fh>) {
  269. chomp;
  270. s{^\./+}{};
  271. my $fields;
  272. # FIXME: Fix it instead to not die on syntax and general errors?
  273. eval {
  274. $fields = process_dsc($prefix, $_);
  275. };
  276. if ($@) {
  277. warn $@;
  278. next;
  279. }
  280. if ($no_sort) {
  281. $fields->output(\*STDOUT);
  282. print "\n";
  283. }
  284. else {
  285. push @out, $fields;
  286. }
  287. }
  288. close $find_fh or error(close_msg, 'find');
  289. if (@out) {
  290. foreach my $dsc (sort { $a->{Package} cmp $b->{Package} } @out) {
  291. $dsc->output(\*STDOUT);
  292. print "\n";
  293. }
  294. }
  295. return 0;
  296. }
  297. $exit = main || $exit;
  298. $exit = 1 if $exit and not $exit % 256;
  299. exit $exit;