dpkg-scansources.pl 9.0 KB

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