dpkg-scansources.pl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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::Control;
  26. use Dpkg::Checksums;
  27. use Dpkg::Compression::FileHandle;
  28. use Dpkg::Compression;
  29. textdomain('dpkg-dev');
  30. # Errors with a single package are warned about but don't affect the
  31. # exit code. Only errors which affect everything cause a non-zero exit.
  32. my $Exit = 0;
  33. # %Override is a hash of lists. The subs following describe what's in
  34. # the lists.
  35. my %Override;
  36. sub O_PRIORITY () { 0 }
  37. sub O_SECTION () { 1 }
  38. sub O_MAINT_FROM () { 2 } # undef for non-specific, else listref
  39. sub O_MAINT_TO () { 3 } # undef if there's no maint override
  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 @Option_spec = (
  54. 'debug!' => \$Debug,
  55. 'help|?' => \&usage,
  56. 'no-sort|n' => \$No_sort,
  57. 'source-override|s=s' => \$Src_override,
  58. 'extra-override|e=s' => \$Extra_override_file,
  59. 'version' => \&version,
  60. );
  61. sub debug {
  62. print @_, "\n" if $Debug;
  63. }
  64. sub version {
  65. printf _g("Debian %s version %s.\n"), $progname, $version;
  66. exit;
  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. "), $progname;
  83. exit;
  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. grep { $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. GetOptions(@Option_spec) or usage;
  255. @ARGV >= 1 && @ARGV <= 3 or usageerr(_g('one to three arguments expected'));
  256. push @ARGV, undef if @ARGV < 2;
  257. push @ARGV, '' if @ARGV < 3;
  258. my ($dir, $override, $prefix) = @ARGV;
  259. load_override $override if defined $override;
  260. load_src_override $Src_override, $override;
  261. load_override_extra $Extra_override_file if defined $Extra_override_file;
  262. open my $find_fh, '-|', "find -L \Q$dir\E -name '*.dsc' -print"
  263. or syserr(_g('cannot fork for %s'), 'find');
  264. while (<$find_fh>) {
  265. chomp;
  266. s-^\./+--;
  267. my $fields;
  268. # FIXME: Fix it instead to not die on syntax and general errors?
  269. eval {
  270. $fields = process_dsc($prefix, $_);
  271. };
  272. if ($@) {
  273. warn $@;
  274. next;
  275. }
  276. if ($No_sort) {
  277. $fields->output(\*STDOUT);
  278. print "\n";
  279. }
  280. else {
  281. push @out, $fields;
  282. }
  283. }
  284. close $find_fh or error(close_msg, 'find');
  285. if (@out) {
  286. map {
  287. $_->output(\*STDOUT);
  288. print "\n";
  289. } sort {
  290. $a->{Package} cmp $b->{Package}
  291. } @out;
  292. }
  293. return 0;
  294. }
  295. $Exit = main || $Exit;
  296. $Exit = 1 if $Exit and not $Exit % 256;
  297. exit $Exit;