dpkg-scansources.pl 9.0 KB

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