dpkg-scansources.pl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 File::Find;
  23. use Dpkg ();
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Util qw(:list);
  27. use Dpkg::Control;
  28. use Dpkg::Checksums;
  29. use Dpkg::Compression::FileHandle;
  30. use Dpkg::Compression;
  31. textdomain('dpkg-dev');
  32. # Hash of lists. The constants below describe what is in the lists.
  33. my %override;
  34. use constant {
  35. O_PRIORITY => 0,
  36. O_SECTION => 1,
  37. O_MAINT_FROM => 2, # undef for non-specific, else listref
  38. O_MAINT_TO => 3, # undef if there's no maint override
  39. };
  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 @sources;
  54. my @option_spec = (
  55. 'debug!' => \$debug,
  56. 'help|?' => sub { usage(); exit 0; },
  57. 'version' => sub { version(); exit 0; },
  58. 'no-sort|n' => \$no_sort,
  59. 'source-override|s=s' => \$src_override,
  60. 'extra-override|e=s' => \$extra_override_file,
  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. }
  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 load_override {
  85. my $file = shift;
  86. local $_;
  87. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
  88. while (<$comp_file>) {
  89. s/#.*//;
  90. next if /^\s*$/;
  91. s/\s+$//;
  92. my @data = split ' ', $_, 4;
  93. unless (@data == 3 || @data == 4) {
  94. warning(g_('invalid override entry at line %d (%d fields)'),
  95. $., 0 + @data);
  96. next;
  97. }
  98. my ($package, $priority, $section, $maintainer) = @data;
  99. if (exists $override{$package}) {
  100. warning(g_('ignoring duplicate override entry for %s at line %d'),
  101. $package, $.);
  102. next;
  103. }
  104. if (!$priority{$priority}) {
  105. warning(g_('ignoring override entry for %s, invalid priority %s'),
  106. $package, $priority);
  107. next;
  108. }
  109. $override{$package} = [];
  110. $override{$package}[O_PRIORITY] = $priority;
  111. $override{$package}[O_SECTION] = $section;
  112. if (!defined $maintainer) {
  113. # do nothing
  114. }
  115. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  116. $override{$package}[O_MAINT_FROM] = [split m{\s*//\s*}, $1];
  117. $override{$package}[O_MAINT_TO] = $2;
  118. }
  119. else {
  120. $override{$package}[O_MAINT_TO] = $maintainer;
  121. }
  122. }
  123. close($comp_file);
  124. }
  125. sub load_src_override {
  126. my ($user_file, $regular_file) = @_;
  127. my ($file);
  128. local $_;
  129. if (defined $user_file) {
  130. $file = $user_file;
  131. }
  132. elsif (defined $regular_file) {
  133. my $comp = compression_guess_from_filename($regular_file);
  134. if (defined($comp)) {
  135. $file = $regular_file;
  136. my $ext = compression_get_property($comp, 'file_ext');
  137. $file =~ s/\.$ext$/.src.$ext/;
  138. } else {
  139. $file = "$regular_file.src";
  140. }
  141. return unless -e $file;
  142. }
  143. else {
  144. return;
  145. }
  146. debug "source override file $file";
  147. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
  148. while (<$comp_file>) {
  149. s/#.*//;
  150. next if /^\s*$/;
  151. s/\s+$//;
  152. my @data = split ' ';
  153. unless (@data == 2) {
  154. warning(g_('invalid source override entry at line %d (%d fields)'),
  155. $., 0 + @data);
  156. next;
  157. }
  158. my ($package, $section) = @data;
  159. my $key = "source/$package";
  160. if (exists $override{$key}) {
  161. warning(g_('ignoring duplicate source override entry for %s at line %d'),
  162. $package, $.);
  163. next;
  164. }
  165. $override{$key} = [];
  166. $override{$key}[O_SECTION] = $section;
  167. }
  168. close($comp_file);
  169. }
  170. sub load_override_extra
  171. {
  172. my $extra_override = shift;
  173. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $extra_override);
  174. while (<$comp_file>) {
  175. s/\#.*//;
  176. s/\s+$//;
  177. next unless $_;
  178. my ($p, $field, $value) = split(/\s+/, $_, 3);
  179. $extra_override{$p}{$field} = $value;
  180. }
  181. close($comp_file);
  182. }
  183. # Given PREFIX and DSC-FILE, process the file and returns the fields.
  184. sub process_dsc {
  185. my ($prefix, $file) = @_;
  186. my $basename = $file;
  187. my $dir = ($basename =~ s{^(.*)/}{}) ? $1 : '';
  188. $dir = "$prefix$dir";
  189. $dir =~ s{/+$}{};
  190. $dir = '.' if $dir eq '';
  191. # Parse ‘.dsc’ file.
  192. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  193. $fields->load($file);
  194. $fields->set_options(type => CTRL_INDEX_SRC);
  195. # Get checksums
  196. my $checksums = Dpkg::Checksums->new();
  197. $checksums->add_from_file($file, key => $basename);
  198. $checksums->add_from_control($fields, use_files_for_md5 => 1);
  199. my $source = $fields->{Source};
  200. my @binary = split /\s*,\s*/, $fields->{Binary} // '';
  201. error(g_('no binary packages specified in %s'), $file) unless (@binary);
  202. # Rename the source field to package.
  203. $fields->{Package} = $fields->{Source};
  204. delete $fields->{Source};
  205. # The priority for the source package is the highest priority of the
  206. # binary packages it produces.
  207. my @binary_by_priority = sort {
  208. ($override{$a} ? $priority{$override{$a}[O_PRIORITY]} : 0)
  209. <=>
  210. ($override{$b} ? $priority{$override{$b}[O_PRIORITY]} : 0)
  211. } @binary;
  212. my $priority_override = $override{$binary_by_priority[-1]};
  213. my $priority = $priority_override
  214. ? $priority_override->[O_PRIORITY]
  215. : undef;
  216. $fields->{Priority} = $priority if defined $priority;
  217. # For the section override, first check for a record from the source
  218. # override file, else use the regular override file.
  219. my $section_override = $override{"source/$source"} || $override{$source};
  220. my $section = $section_override
  221. ? $section_override->[O_SECTION]
  222. : undef;
  223. $fields->{Section} = $section if defined $section;
  224. # For the maintainer override, use the override record for the first
  225. # binary. Modify the maintainer if necessary.
  226. my $maintainer_override = $override{$binary[0]};
  227. if ($maintainer_override && defined $maintainer_override->[O_MAINT_TO]) {
  228. if (!defined $maintainer_override->[O_MAINT_FROM] ||
  229. any { $fields->{Maintainer} eq $_ }
  230. @{ $maintainer_override->[O_MAINT_FROM] }) {
  231. $fields->{Maintainer} = $maintainer_override->[O_MAINT_TO];
  232. }
  233. }
  234. # Process extra override
  235. if (exists $extra_override{$source}) {
  236. my ($field, $value);
  237. while (($field, $value) = each %{$extra_override{$source}}) {
  238. $fields->{$field} = $value;
  239. }
  240. }
  241. # A directory field will be inserted just before the files field.
  242. $fields->{Directory} = $dir;
  243. $checksums->export_to_control($fields, use_files_for_md5 => 1);
  244. push @sources, $fields;
  245. }
  246. ### Main
  247. {
  248. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  249. GetOptions(@option_spec);
  250. }
  251. usageerr(g_('one to three arguments expected'))
  252. if @ARGV < 1 or @ARGV > 3;
  253. push @ARGV, undef if @ARGV < 2;
  254. push @ARGV, '' if @ARGV < 3;
  255. my ($dir, $override, $prefix) = @ARGV;
  256. load_override $override if defined $override;
  257. load_src_override $src_override, $override;
  258. load_override_extra $extra_override_file if defined $extra_override_file;
  259. my @dsc;
  260. my $scan_dsc = sub {
  261. push @dsc, $File::Find::name if m/\.dsc$/;
  262. };
  263. find({ follow => 1, follow_skip => 2, wanted => $scan_dsc }, $dir);
  264. foreach my $fn (@dsc) {
  265. # FIXME: Fix it instead to not die on syntax and general errors?
  266. eval {
  267. process_dsc($prefix, $fn);
  268. };
  269. if ($@) {
  270. warn $@;
  271. next;
  272. }
  273. }
  274. if (not $no_sort) {
  275. @sources = sort {
  276. $a->{Package} . $a->{Version} cmp $b->{Package} . $b->{Version}
  277. } @sources;
  278. }
  279. foreach my $dsc (@sources) {
  280. $dsc->output(\*STDOUT);
  281. print "\n";
  282. }