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_values no_ignorecase);
24 use List
::Util
qw(any);
29 use Dpkg
::ErrorHandling
;
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) {
129 } elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
130 $override{$package}[O_MAINT_TO
] = $2;
131 $override{$package}[O_MAINT_FROM
] = [split m{\s*//\s*}, $1];
133 $override{$package}[O_MAINT_TO
] = $maintainer;
139 sub load_src_override
{
140 my ($user_file, $regular_file) = @_;
144 if (defined $user_file) {
146 } elsif (defined $regular_file) {
147 my $comp = compression_guess_from_filename
($regular_file);
148 if (defined($comp)) {
149 $file = $regular_file;
150 my $ext = compression_get_file_extension
($comp);
151 $file =~ s/\.$ext$/.src.$ext/;
153 $file = "$regular_file.src";
155 return unless -e
$file;
160 debug
(1, "source override file $file");
161 my $comp_file = Dpkg
::Compression
::FileHandle
->new(filename
=> $file);
162 while (<$comp_file>) {
167 my @data = split ' ';
168 unless (@data == 2) {
169 warning
(g_
('invalid source override entry at line %d (%d fields)'),
174 my ($package, $section) = @data;
175 my $key = "source/$package";
176 if (exists $override{$key}) {
177 warning
(g_
('ignoring duplicate source override entry for %s at line %d'),
181 $override{$key} = [];
182 $override{$key}[O_SECTION
] = $section;
187 sub load_override_extra
189 my $extra_override = shift;
190 my $comp_file = Dpkg
::Compression
::FileHandle
->new(filename
=> $extra_override);
192 while (<$comp_file>) {
197 my ($p, $field, $value) = split(/\s+/, $_, 3);
198 $extra_override{$p}{$field} = $value;
203 # Given PREFIX and DSC-FILE, process the file and returns the fields.
206 my ($prefix, $file) = @_;
208 my $basename = $file;
209 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
210 my $dir = ($basename =~ s{^(.*)/}{}) ?
$1 : '';
211 $dir = "$prefix$dir";
213 $dir = '.' if $dir eq '';
216 my $fields = Dpkg
::Control
->new(type
=> CTRL_PKG_SRC
);
217 $fields->load($file);
218 $fields->set_options(type
=> CTRL_INDEX_SRC
);
221 my $checksums = Dpkg
::Checksums
->new();
222 $checksums->add_from_file($file, key
=> $basename);
223 $checksums->add_from_control($fields, use_files_for_md5
=> 1);
225 my $source = $fields->{Source
};
226 my @binary = split /\s*,\s*/, $fields->{Binary
} // '';
228 error
(g_
('no binary packages specified in %s'), $file) unless (@binary);
230 # Rename the source field to package.
231 $fields->{Package
} = $fields->{Source
};
232 delete $fields->{Source
};
234 # The priority for the source package is the highest priority of the
235 # binary packages it produces.
236 my @binary_by_priority = sort {
237 ($override{$a} ?
$priority{$override{$a}[O_PRIORITY
]} : 0)
239 ($override{$b} ?
$priority{$override{$b}[O_PRIORITY
]} : 0)
241 my $priority_override = $override{$binary_by_priority[-1]};
242 my $priority = $priority_override
243 ?
$priority_override->[O_PRIORITY
]
245 $fields->{Priority
} = $priority if defined $priority;
247 # For the section override, first check for a record from the source
248 # override file, else use the regular override file.
249 my $section_override = $override{"source/$source"} || $override{$source};
250 my $section = $section_override
251 ?
$section_override->[O_SECTION
]
253 $fields->{Section
} = $section if defined $section;
255 # For the maintainer override, use the override record for the first
256 # binary. Modify the maintainer if necessary.
257 my $maintainer_override = $override{$binary[0]};
258 if ($maintainer_override && defined $maintainer_override->[O_MAINT_TO
]) {
259 if (!defined $maintainer_override->[O_MAINT_FROM
] ||
260 any
{ $fields->{Maintainer
} eq $_ }
261 @
{ $maintainer_override->[O_MAINT_FROM
] }) {
262 $fields->{Maintainer
} = $maintainer_override->[O_MAINT_TO
];
266 # Process extra override
267 if (exists $extra_override{$source}) {
269 while (($field, $value) = each %{$extra_override{$source}}) {
270 $fields->{$field} = $value;
274 # A directory field will be inserted just before the files field.
275 $fields->{Directory
} = $dir;
277 $checksums->export_to_control($fields, use_files_for_md5
=> 1);
279 push @sources, $fields;
285 local $SIG{__WARN__
} = sub { usageerr
($_[0]) };
286 GetOptions
(@option_spec);
289 usageerr
(g_
('one to three arguments expected'))
290 if not 1 <= @ARGV <= 3;
292 push @ARGV, undef if @ARGV < 2;
293 push @ARGV, '' if @ARGV < 3;
294 my ($dir, $override, $prefix) = @ARGV;
296 report_options
(debug_level
=> $debug);
298 load_override
$override if defined $override;
299 load_src_override
$src_override, $override;
300 load_override_extra
$extra_override_file if defined $extra_override_file;
304 push @dsc, $File::Find
::name
if m/\.dsc$/;
307 find
({ follow
=> 1, follow_skip
=> 2, wanted
=> $scan_dsc }, $dir);
308 foreach my $fn (@dsc) {
309 # FIXME: Fix it instead to not die on syntax and general errors?
311 process_dsc
($prefix, $fn);
321 $a->{Package
} . $a->{Version
} cmp $b->{Package
} . $b->{Version
}
324 foreach my $dsc (@sources) {
325 $dsc->output(\
*STDOUT
);