3 # Copyright © 1999 Roderick Schertler
4 # Copyright © 2002 Wichert Akkerman <wakkerma@debian.org>
5 # Copyright © 2006-2009, 2011-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
23 use Getopt::Long qw(:config posix_default bundling no_ignorecase);
28 use Dpkg::ErrorHandling;
29 use Dpkg::Util qw(:list);
32 use Dpkg::Compression::FileHandle;
33 use Dpkg::Compression;
35 textdomain('dpkg-dev');
37 # Hash of lists. The constants below describe what is in the lists.
42 O_MAINT_FROM => 2, # undef for non-specific, else listref
43 O_MAINT_TO => 3, # undef if there's no maint override
60 my $src_override = undef;
61 my $extra_override_file = undef;
66 'help|?' => sub { usage(); exit 0; },
67 'version' => sub { version(); exit 0; },
68 'no-sort|n' => \$no_sort,
69 'source-override|s=s' => \$src_override,
70 'extra-override|e=s' => \$extra_override_file,
74 printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
79 "Usage: %s [<option>...] <binary-path> [<override-file> [<path-prefix>]] > Sources
82 -n, --no-sort don't sort by package before outputting.
83 -e, --extra-override <file>
84 use extra override file.
85 -s, --source-override <file>
86 use file for additional source overrides, default
87 is regular override file with .src appended.
88 --debug turn debugging on.
89 -?, --help show this help message.
90 --version show the version.
92 See the man page for the full documentation.
100 my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
101 while (<$comp_file>) {
106 my @data = split ' ', $_, 4;
107 unless (@data == 3 || @data == 4) {
108 warning(g_('invalid override entry at line %d (%d fields)'),
112 my ($package, $priority, $section, $maintainer) = @data;
113 if (exists $override{$package}) {
114 warning(g_('ignoring duplicate override entry for %s at line %d'),
118 if (!$priority{$priority}) {
119 warning(g_('ignoring override entry for %s, invalid priority %s'),
120 $package, $priority);
124 $override{$package} = [];
125 $override{$package}[O_PRIORITY] = $priority;
126 $override{$package}[O_SECTION] = $section;
127 if (!defined $maintainer) {
130 elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
131 $override{$package}[O_MAINT_FROM] = [split m{\s*//\s*}, $1];
132 $override{$package}[O_MAINT_TO] = $2;
135 $override{$package}[O_MAINT_TO] = $maintainer;
141 sub load_src_override {
142 my ($user_file, $regular_file) = @_;
146 if (defined $user_file) {
149 elsif (defined $regular_file) {
150 my $comp = compression_guess_from_filename($regular_file);
151 if (defined($comp)) {
152 $file = $regular_file;
153 my $ext = compression_get_property($comp, 'file_ext');
154 $file =~ s/\.$ext$/.src.$ext/;
156 $file = "$regular_file.src";
158 return unless -e $file;
164 debug(1, "source override file $file");
165 my $comp_file = Dpkg::Compression::FileHandle->new(filename => $file);
166 while (<$comp_file>) {
171 my @data = split ' ';
172 unless (@data == 2) {
173 warning(g_('invalid source override entry at line %d (%d fields)'),
178 my ($package, $section) = @data;
179 my $key = "source/$package";
180 if (exists $override{$key}) {
181 warning(g_('ignoring duplicate source override entry for %s at line %d'),
185 $override{$key} = [];
186 $override{$key}[O_SECTION] = $section;
191 sub load_override_extra
193 my $extra_override = shift;
194 my $comp_file = Dpkg::Compression::FileHandle->new(filename => $extra_override);
196 while (<$comp_file>) {
201 my ($p, $field, $value) = split(/\s+/, $_, 3);
202 $extra_override{$p}{$field} = $value;
207 # Given PREFIX and DSC-FILE, process the file and returns the fields.
210 my ($prefix, $file) = @_;
212 my $basename = $file;
213 my $dir = ($basename =~ s{^(.*)/}{}) ? $1 : '';
214 $dir = "$prefix$dir";
216 $dir = '.' if $dir eq '';
219 my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
220 $fields->load($file);
221 $fields->set_options(type => CTRL_INDEX_SRC);
224 my $checksums = Dpkg::Checksums->new();
225 $checksums->add_from_file($file, key => $basename);
226 $checksums->add_from_control($fields, use_files_for_md5 => 1);
228 my $source = $fields->{Source};
229 my @binary = split /\s*,\s*/, $fields->{Binary} // '';
231 error(g_('no binary packages specified in %s'), $file) unless (@binary);
233 # Rename the source field to package.
234 $fields->{Package} = $fields->{Source};
235 delete $fields->{Source};
237 # The priority for the source package is the highest priority of the
238 # binary packages it produces.
239 my @binary_by_priority = sort {
240 ($override{$a} ? $priority{$override{$a}[O_PRIORITY]} : 0)
242 ($override{$b} ? $priority{$override{$b}[O_PRIORITY]} : 0)
244 my $priority_override = $override{$binary_by_priority[-1]};
245 my $priority = $priority_override
246 ? $priority_override->[O_PRIORITY]
248 $fields->{Priority} = $priority if defined $priority;
250 # For the section override, first check for a record from the source
251 # override file, else use the regular override file.
252 my $section_override = $override{"source/$source"} || $override{$source};
253 my $section = $section_override
254 ? $section_override->[O_SECTION]
256 $fields->{Section} = $section if defined $section;
258 # For the maintainer override, use the override record for the first
259 # binary. Modify the maintainer if necessary.
260 my $maintainer_override = $override{$binary[0]};
261 if ($maintainer_override && defined $maintainer_override->[O_MAINT_TO]) {
262 if (!defined $maintainer_override->[O_MAINT_FROM] ||
263 any { $fields->{Maintainer} eq $_ }
264 @{ $maintainer_override->[O_MAINT_FROM] }) {
265 $fields->{Maintainer} = $maintainer_override->[O_MAINT_TO];
269 # Process extra override
270 if (exists $extra_override{$source}) {
272 while (($field, $value) = each %{$extra_override{$source}}) {
273 $fields->{$field} = $value;
277 # A directory field will be inserted just before the files field.
278 $fields->{Directory} = $dir;
280 $checksums->export_to_control($fields, use_files_for_md5 => 1);
282 push @sources, $fields;
288 local $SIG{__WARN__} = sub { usageerr($_[0]) };
289 GetOptions(@option_spec);
292 usageerr(g_('one to three arguments expected'))
293 if @ARGV < 1 or @ARGV > 3;
295 push @ARGV, undef if @ARGV < 2;
296 push @ARGV, '' if @ARGV < 3;
297 my ($dir, $override, $prefix) = @ARGV;
299 report_options(debug_level => $debug);
301 load_override $override if defined $override;
302 load_src_override $src_override, $override;
303 load_override_extra $extra_override_file if defined $extra_override_file;
307 push @dsc, $File::Find::name if m/\.dsc$/;
310 find({ follow => 1, follow_skip => 2, wanted => $scan_dsc }, $dir);
311 foreach my $fn (@dsc) {
312 # FIXME: Fix it instead to not die on syntax and general errors?
314 process_dsc($prefix, $fn);
324 $a->{Package} . $a->{Version} cmp $b->{Package} . $b->{Version}
327 foreach my $dsc (@sources) {
328 $dsc->output(\*STDOUT);