dpkg-scansources.pl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 <https://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. # Hash of lists. The constants below describe what is in the lists.
  35. my %override;
  36. use constant {
  37. O_PRIORITY => 0,
  38. O_SECTION => 1,
  39. O_MAINT_FROM => 2, # undef for non-specific, else listref
  40. O_MAINT_TO => 3, # undef if there's no maint override
  41. };
  42. my %extra_override;
  43. my %priority = (
  44. 'extra' => 1,
  45. 'optional' => 2,
  46. 'standard' => 3,
  47. 'important' => 4,
  48. 'required' => 5,
  49. );
  50. # Switches
  51. my $debug = 0;
  52. my $no_sort = 0;
  53. my $src_override = undef;
  54. my $extra_override_file = undef;
  55. my @sources;
  56. my @option_spec = (
  57. 'debug!' => \$debug,
  58. 'help|?' => sub { usage(); exit 0; },
  59. 'version' => sub { version(); exit 0; },
  60. 'no-sort|n' => \$no_sort,
  61. 'source-override|s=s' => \$src_override,
  62. 'extra-override|e=s' => \$extra_override_file,
  63. );
  64. sub debug {
  65. print @_, "\n" if $debug;
  66. }
  67. sub version {
  68. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  69. }
  70. sub usage {
  71. printf g_(
  72. "Usage: %s [<option>...] <binary-path> [<override-file> [<path-prefix>]] > Sources
  73. Options:
  74. -n, --no-sort don't sort by package before outputting.
  75. -e, --extra-override <file>
  76. use extra override file.
  77. -s, --source-override <file>
  78. use file for additional source overrides, default
  79. is regular override file with .src appended.
  80. --debug turn debugging on.
  81. -?, --help show this help message.
  82. --version show the version.
  83. See the man page for the full documentation.
  84. "), $Dpkg::PROGNAME;
  85. }
  86. sub load_override {
  87. my $file = shift;
  88. local $_;
  89. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
  90. while (<$comp_file>) {
  91. s/#.*//;
  92. next if /^\s*$/;
  93. s/\s+$//;
  94. my @data = split ' ', $_, 4;
  95. unless (@data == 3 || @data == 4) {
  96. warning(g_('invalid override entry at line %d (%d fields)'),
  97. $., 0 + @data);
  98. next;
  99. }
  100. my ($package, $priority, $section, $maintainer) = @data;
  101. if (exists $override{$package}) {
  102. warning(g_('ignoring duplicate override entry for %s at line %d'),
  103. $package, $.);
  104. next;
  105. }
  106. if (!$priority{$priority}) {
  107. warning(g_('ignoring override entry for %s, invalid priority %s'),
  108. $package, $priority);
  109. next;
  110. }
  111. $override{$package} = [];
  112. $override{$package}[O_PRIORITY] = $priority;
  113. $override{$package}[O_SECTION] = $section;
  114. if (!defined $maintainer) {
  115. # do nothing
  116. }
  117. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  118. $override{$package}[O_MAINT_FROM] = [split m{\s*//\s*}, $1];
  119. $override{$package}[O_MAINT_TO] = $2;
  120. }
  121. else {
  122. $override{$package}[O_MAINT_TO] = $maintainer;
  123. }
  124. }
  125. close($comp_file);
  126. }
  127. sub load_src_override {
  128. my ($user_file, $regular_file) = @_;
  129. my ($file);
  130. local $_;
  131. if (defined $user_file) {
  132. $file = $user_file;
  133. }
  134. elsif (defined $regular_file) {
  135. my $comp = compression_guess_from_filename($regular_file);
  136. if (defined($comp)) {
  137. $file = $regular_file;
  138. my $ext = compression_get_property($comp, 'file_ext');
  139. $file =~ s/\.$ext$/.src.$ext/;
  140. } else {
  141. $file = "$regular_file.src";
  142. }
  143. return unless -e $file;
  144. }
  145. else {
  146. return;
  147. }
  148. debug "source override file $file";
  149. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
  150. while (<$comp_file>) {
  151. s/#.*//;
  152. next if /^\s*$/;
  153. s/\s+$//;
  154. my @data = split ' ';
  155. unless (@data == 2) {
  156. warning(g_('invalid source override entry at line %d (%d fields)'),
  157. $., 0 + @data);
  158. next;
  159. }
  160. my ($package, $section) = @data;
  161. my $key = "source/$package";
  162. if (exists $override{$key}) {
  163. warning(g_('ignoring duplicate source override entry for %s at line %d'),
  164. $package, $.);
  165. next;
  166. }
  167. $override{$key} = [];
  168. $override{$key}[O_SECTION] = $section;
  169. }
  170. close($comp_file);
  171. }
  172. sub load_override_extra
  173. {
  174. my $extra_override = shift;
  175. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $extra_override);
  176. while (<$comp_file>) {
  177. s/\#.*//;
  178. s/\s+$//;
  179. next unless $_;
  180. my ($p, $field, $value) = split(/\s+/, $_, 3);
  181. $extra_override{$p}{$field} = $value;
  182. }
  183. close($comp_file);
  184. }
  185. # Given PREFIX and DSC-FILE, process the file and returns the fields.
  186. sub process_dsc {
  187. my ($prefix, $file) = @_;
  188. my $basename = $file;
  189. my $dir = ($basename =~ s{^(.*)/}{}) ? $1 : '';
  190. $dir = "$prefix$dir";
  191. $dir =~ s{/+$}{};
  192. $dir = '.' if $dir eq '';
  193. # Parse ‘.dsc’ file.
  194. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  195. $fields->load($file);
  196. $fields->set_options(type => CTRL_INDEX_SRC);
  197. # Get checksums
  198. my $checksums = Dpkg::Checksums->new();
  199. $checksums->add_from_file($file, key => $basename);
  200. $checksums->add_from_control($fields, use_files_for_md5 => 1);
  201. my $source = $fields->{Source};
  202. my @binary = split /\s*,\s*/, $fields->{Binary} // '';
  203. error(g_('no binary packages specified in %s'), $file) unless (@binary);
  204. # Rename the source field to package.
  205. $fields->{Package} = $fields->{Source};
  206. delete $fields->{Source};
  207. # The priority for the source package is the highest priority of the
  208. # binary packages it produces.
  209. my @binary_by_priority = sort {
  210. ($override{$a} ? $priority{$override{$a}[O_PRIORITY]} : 0)
  211. <=>
  212. ($override{$b} ? $priority{$override{$b}[O_PRIORITY]} : 0)
  213. } @binary;
  214. my $priority_override = $override{$binary_by_priority[-1]};
  215. my $priority = $priority_override
  216. ? $priority_override->[O_PRIORITY]
  217. : undef;
  218. $fields->{Priority} = $priority if defined $priority;
  219. # For the section override, first check for a record from the source
  220. # override file, else use the regular override file.
  221. my $section_override = $override{"source/$source"} || $override{$source};
  222. my $section = $section_override
  223. ? $section_override->[O_SECTION]
  224. : undef;
  225. $fields->{Section} = $section if defined $section;
  226. # For the maintainer override, use the override record for the first
  227. # binary. Modify the maintainer if necessary.
  228. my $maintainer_override = $override{$binary[0]};
  229. if ($maintainer_override && defined $maintainer_override->[O_MAINT_TO]) {
  230. if (!defined $maintainer_override->[O_MAINT_FROM] ||
  231. any { $fields->{Maintainer} eq $_ }
  232. @{ $maintainer_override->[O_MAINT_FROM] }) {
  233. $fields->{Maintainer} = $maintainer_override->[O_MAINT_TO];
  234. }
  235. }
  236. # Process extra override
  237. if (exists $extra_override{$source}) {
  238. my ($field, $value);
  239. while (($field, $value) = each %{$extra_override{$source}}) {
  240. $fields->{$field} = $value;
  241. }
  242. }
  243. # A directory field will be inserted just before the files field.
  244. $fields->{Directory} = $dir;
  245. $checksums->export_to_control($fields, use_files_for_md5 => 1);
  246. push @sources, $fields;
  247. }
  248. sub main {
  249. {
  250. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  251. GetOptions(@option_spec);
  252. }
  253. usageerr(g_('one to three arguments expected'))
  254. if @ARGV < 1 or @ARGV > 3;
  255. push @ARGV, undef if @ARGV < 2;
  256. push @ARGV, '' if @ARGV < 3;
  257. my ($dir, $override, $prefix) = @ARGV;
  258. load_override $override if defined $override;
  259. load_src_override $src_override, $override;
  260. load_override_extra $extra_override_file if defined $extra_override_file;
  261. open my $find_fh, '-|', 'find', '-L', $dir, '-name', '*.dsc', '-print'
  262. or syserr(g_('cannot fork for %s'), 'find');
  263. while (<$find_fh>) {
  264. chomp;
  265. s{^\./+}{};
  266. # FIXME: Fix it instead to not die on syntax and general errors?
  267. eval {
  268. process_dsc($prefix, $_);
  269. };
  270. if ($@) {
  271. warn $@;
  272. next;
  273. }
  274. }
  275. close $find_fh or syserr(g_('error closing %s (%s)'), 'find', $!);
  276. if (not $no_sort) {
  277. @sources = sort { $a->{Package} cmp $b->{Package} } @sources;
  278. }
  279. foreach my $dsc (@sources) {
  280. $dsc->output(\*STDOUT);
  281. print "\n";
  282. }
  283. return 0;
  284. }
  285. $exit = main || $exit;
  286. $exit = 1 if $exit and not $exit % 256;
  287. exit $exit;