dpkg-source.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #! /usr/bin/perl
  2. #
  3. # dpkg-source
  4. #
  5. # Copyright © 1996 Ian Jackson <ian@davenant.greenend.org.uk>
  6. # Copyright © 1997 Klee Dienes <klee@debian.org>
  7. # Copyright © 1999-2003 Wichert Akkerman <wakkerma@debian.org>
  8. # Copyright © 1999 Ben Collins <bcollins@debian.org>
  9. # Copyright © 2000-2003 Adam Heath <doogie@debian.org>
  10. # Copyright © 2005 Brendan O'Dea <bod@debian.org>
  11. # Copyright © 2006-2008 Frank Lichtenheld <djpig@debian.org>
  12. # Copyright © 2006-2009,2012 Guillem Jover <guillem@debian.org>
  13. # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
  14. #
  15. # This program is free software; you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation; either version 2 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. use strict;
  28. use warnings;
  29. use Dpkg ();
  30. use Dpkg::Gettext;
  31. use Dpkg::ErrorHandling;
  32. use Dpkg::Util qw(:list);
  33. use Dpkg::Arch qw(debarch_eq debarch_is debarch_is_wildcard);
  34. use Dpkg::Deps;
  35. use Dpkg::Compression;
  36. use Dpkg::Conf;
  37. use Dpkg::Control::Info;
  38. use Dpkg::Control::Fields;
  39. use Dpkg::Substvars;
  40. use Dpkg::Version;
  41. use Dpkg::Vars;
  42. use Dpkg::Changelog::Parse;
  43. use Dpkg::Source::Package qw(get_default_diff_ignore_regex
  44. set_default_diff_ignore_regex
  45. get_default_tar_ignore_pattern);
  46. use Dpkg::Vendor qw(run_vendor_hook);
  47. use Cwd;
  48. use File::Basename;
  49. use File::Spec;
  50. textdomain('dpkg-dev');
  51. my $controlfile;
  52. my $changelogfile;
  53. my $changelogformat;
  54. my $build_format;
  55. my %options = (
  56. # Compression related
  57. compression => compression_get_default(),
  58. comp_level => compression_get_default_level(),
  59. comp_ext => compression_get_property(compression_get_default(), 'file_ext'),
  60. # Ignore files
  61. tar_ignore => [],
  62. diff_ignore_regex => '',
  63. # Misc options
  64. copy_orig_tarballs => 1,
  65. no_check => 0,
  66. require_valid_signature => 0,
  67. );
  68. # Fields to remove/override
  69. my %remove;
  70. my %override;
  71. my $substvars = Dpkg::Substvars->new();
  72. my $tar_ignore_default_pattern_done;
  73. my $diff_ignore_regex = get_default_diff_ignore_regex();
  74. my @options;
  75. my @cmdline_options;
  76. while (@ARGV && $ARGV[0] =~ m/^-/) {
  77. $_ = shift(@ARGV);
  78. if (m/^-b$/) {
  79. setopmode('-b');
  80. } elsif (m/^-x$/) {
  81. setopmode('-x');
  82. } elsif (m/^--(before|after)-build$/) {
  83. setopmode($_);
  84. } elsif (m/^--commit$/) {
  85. setopmode($_);
  86. } elsif (m/^--print-format$/) {
  87. setopmode('--print-format');
  88. report_options(info_fh => \*STDERR); # Avoid clutter on STDOUT
  89. } else {
  90. push @options, $_;
  91. }
  92. }
  93. my $dir;
  94. if (defined($options{opmode}) &&
  95. $options{opmode} =~ /^(-b|--print-format|--(before|after)-build|--commit)$/) {
  96. if (not scalar(@ARGV)) {
  97. usageerr(_g('%s needs a directory'), $options{opmode})
  98. unless $1 eq '--commit';
  99. $dir = '.';
  100. } else {
  101. $dir = File::Spec->catdir(shift(@ARGV));
  102. }
  103. stat($dir) or syserr(_g('cannot stat directory %s'), $dir);
  104. if (not -d $dir) {
  105. error(_g('directory argument %s is not a directory'), $dir);
  106. }
  107. if ($dir eq '.') {
  108. # . is never correct, adjust automatically
  109. $dir = basename(cwd());
  110. chdir('..') or syserr(_g("unable to chdir to `%s'"), '..');
  111. }
  112. # --format options are not allowed, they would take precedence
  113. # over real command line options, debian/source/format should be used
  114. # instead
  115. # --unapply-patches is only allowed in local-options as it's a matter
  116. # of personal taste and the default should be to keep patches applied
  117. my $forbidden_opts_re = {
  118. 'options' => qr/^--(?:format=|unapply-patches$|abort-on-upstream-changes$)/,
  119. 'local-options' => qr/^--format=/,
  120. };
  121. foreach my $filename ('local-options', 'options') {
  122. my $conf = Dpkg::Conf->new();
  123. my $optfile = File::Spec->catfile($dir, 'debian', 'source', $filename);
  124. next unless -f $optfile;
  125. $conf->load($optfile);
  126. $conf->filter(remove => sub { $_[0] =~ $forbidden_opts_re->{$filename} });
  127. if (@$conf) {
  128. info(_g('using options from %s: %s'), $optfile, join(' ', @$conf))
  129. unless $options{opmode} eq '--print-format';
  130. unshift @options, @$conf;
  131. }
  132. }
  133. }
  134. while (@options) {
  135. $_ = shift(@options);
  136. if (m/^--format=(.*)$/) {
  137. $build_format //= $1;
  138. } elsif (m/^-(?:Z|-compression=)(.*)$/) {
  139. my $compression = $1;
  140. $options{compression} = $compression;
  141. $options{comp_ext} = compression_get_property($compression, 'file_ext');
  142. usageerr(_g('%s is not a supported compression'), $compression)
  143. unless compression_is_supported($compression);
  144. compression_set_default($compression);
  145. } elsif (m/^-(?:z|-compression-level=)(.*)$/) {
  146. my $comp_level = $1;
  147. $options{comp_level} = $comp_level;
  148. usageerr(_g('%s is not a compression level'), $comp_level)
  149. unless compression_is_valid_level($comp_level);
  150. compression_set_default_level($comp_level);
  151. } elsif (m/^-c(.*)$/) {
  152. $controlfile = $1;
  153. } elsif (m/^-l(.*)$/) {
  154. $changelogfile = $1;
  155. } elsif (m/^-F([0-9a-z]+)$/) {
  156. $changelogformat = $1;
  157. } elsif (m/^-D([^\=:]+)[=:](.*)$/s) {
  158. $override{$1} = $2;
  159. } elsif (m/^-U([^\=:]+)$/) {
  160. $remove{$1} = 1;
  161. } elsif (m/^-(?:i|-diff-ignore(?:$|=))(.*)$/) {
  162. $options{diff_ignore_regex} = $1 ? $1 : $diff_ignore_regex;
  163. } elsif (m/^--extend-diff-ignore=(.+)$/) {
  164. $diff_ignore_regex .= "|$1";
  165. if ($options{diff_ignore_regex}) {
  166. $options{diff_ignore_regex} .= "|$1";
  167. }
  168. set_default_diff_ignore_regex($diff_ignore_regex);
  169. } elsif (m/^-(?:I|-tar-ignore=)(.+)$/) {
  170. push @{$options{tar_ignore}}, $1;
  171. } elsif (m/^-(?:I|-tar-ignore)$/) {
  172. unless ($tar_ignore_default_pattern_done) {
  173. push @{$options{tar_ignore}}, get_default_tar_ignore_pattern();
  174. # Prevent adding multiple times
  175. $tar_ignore_default_pattern_done = 1;
  176. }
  177. } elsif (m/^--no-copy$/) {
  178. $options{copy_orig_tarballs} = 0;
  179. } elsif (m/^--no-check$/) {
  180. $options{no_check} = 1;
  181. } elsif (m/^--require-valid-signature$/) {
  182. $options{require_valid_signature} = 1;
  183. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:](.*)$/s) {
  184. $substvars->set($1, $2);
  185. } elsif (m/^-T(.*)$/) {
  186. $substvars->load($1) if -e $1;
  187. } elsif (m/^-(\?|-help)$/) {
  188. usage();
  189. exit(0);
  190. } elsif (m/^--version$/) {
  191. version();
  192. exit(0);
  193. } elsif (m/^-[EW]$/) {
  194. # Deprecated option
  195. warning(_g('-E and -W are deprecated, they are without effect'));
  196. } elsif (m/^-q$/) {
  197. report_options(quiet_warnings => 1);
  198. $options{quiet} = 1;
  199. } elsif (m/^--$/) {
  200. last;
  201. } else {
  202. push @cmdline_options, $_;
  203. }
  204. }
  205. unless (defined($options{opmode})) {
  206. usageerr(_g('need a command (-x, -b, --before-build, --after-build, --print-format, --commit)'));
  207. }
  208. if ($options{opmode} =~ /^(-b|--print-format|--(before|after)-build|--commit)$/) {
  209. $options{ARGV} = \@ARGV;
  210. $changelogfile ||= "$dir/debian/changelog";
  211. $controlfile ||= "$dir/debian/control";
  212. my %ch_options = (file => $changelogfile);
  213. $ch_options{changelogformat} = $changelogformat if $changelogformat;
  214. my $changelog = changelog_parse(%ch_options);
  215. my $control = Dpkg::Control::Info->new($controlfile);
  216. my $srcpkg = Dpkg::Source::Package->new(options => \%options);
  217. my $fields = $srcpkg->{fields};
  218. my @sourcearch;
  219. my %archadded;
  220. my @binarypackages;
  221. # Scan control info of source package
  222. my $src_fields = $control->get_source();
  223. error(_g("%s doesn't contain any information about the source package"),
  224. $controlfile) unless defined $src_fields;
  225. my $src_sect = $src_fields->{'Section'} || 'unknown';
  226. my $src_prio = $src_fields->{'Priority'} || 'unknown';
  227. foreach (keys %{$src_fields}) {
  228. my $v = $src_fields->{$_};
  229. if (m/^Source$/i) {
  230. set_source_package($v);
  231. $fields->{$_} = $v;
  232. } elsif (m/^Uploaders$/i) {
  233. ($fields->{$_} = $v) =~ s/\s*[\r\n]\s*/ /g; # Merge in a single-line
  234. } elsif (m/^Build-(Depends|Conflicts)(-Arch|-Indep)?$/i) {
  235. my $dep;
  236. my $type = field_get_dep_type($_);
  237. $dep = deps_parse($v, build_dep => 1, union => $type eq 'union');
  238. error(_g('error occurred while parsing %s'), $_) unless defined $dep;
  239. my $facts = Dpkg::Deps::KnownFacts->new();
  240. $dep->simplify_deps($facts);
  241. $dep->sort() if $type eq 'union';
  242. $fields->{$_} = $dep->output();
  243. } else {
  244. field_transfer_single($src_fields, $fields);
  245. }
  246. }
  247. # Scan control info of binary packages
  248. my @pkglist;
  249. foreach my $pkg ($control->get_packages()) {
  250. my $p = $pkg->{'Package'};
  251. my $sect = $pkg->{'Section'} || $src_sect;
  252. my $prio = $pkg->{'Priority'} || $src_prio;
  253. my $type = $pkg->{'Package-Type'} ||
  254. $pkg->get_custom_field('Package-Type') || 'deb';
  255. push @pkglist, sprintf('%s %s %s %s', $p, $type, $sect, $prio);
  256. push(@binarypackages,$p);
  257. foreach (keys %{$pkg}) {
  258. my $v = $pkg->{$_};
  259. if (m/^Architecture$/) {
  260. # Gather all binary architectures in one set. 'any' and 'all'
  261. # are special-cased as they need to be the only ones in the
  262. # current stanza if present.
  263. if (debarch_eq($v, 'any') || debarch_eq($v, 'all')) {
  264. push(@sourcearch, $v) unless $archadded{$v}++;
  265. } else {
  266. for my $a (split(/\s+/, $v)) {
  267. error(_g("`%s' is not a legal architecture string"),
  268. $a)
  269. unless $a =~ /^[\w-]+$/;
  270. error(_g('architecture %s only allowed on its ' .
  271. "own (list for package %s is `%s')"),
  272. $a, $p, $a)
  273. if $a eq 'any' or $a eq 'all';
  274. push(@sourcearch, $a) unless $archadded{$a}++;
  275. }
  276. }
  277. } elsif (m/^Homepage$/) {
  278. # Do not overwrite the same field from the source entry
  279. } else {
  280. field_transfer_single($pkg, $fields);
  281. }
  282. }
  283. }
  284. unless (scalar(@pkglist)) {
  285. error(_g("%s doesn't list any binary package"), $controlfile);
  286. }
  287. if (any { $_ eq 'any' } @sourcearch) {
  288. # If we encounter one 'any' then the other arches become insignificant
  289. # except for 'all' that must also be kept
  290. if (any { $_ eq 'all' } @sourcearch) {
  291. @sourcearch = qw(any all);
  292. } else {
  293. @sourcearch = qw(any);
  294. }
  295. } else {
  296. # Minimize arch list, by removing arches already covered by wildcards
  297. my @arch_wildcards = grep { debarch_is_wildcard($_) } @sourcearch;
  298. my @mini_sourcearch = @arch_wildcards;
  299. foreach my $arch (@sourcearch) {
  300. if (none { debarch_is($arch, $_) } @arch_wildcards) {
  301. push @mini_sourcearch, $arch;
  302. }
  303. }
  304. @sourcearch = @mini_sourcearch;
  305. }
  306. $fields->{'Architecture'} = join(' ', @sourcearch);
  307. $fields->{'Package-List'} = "\n" . join("\n", sort @pkglist);
  308. # Scan fields of dpkg-parsechangelog
  309. foreach (keys %{$changelog}) {
  310. my $v = $changelog->{$_};
  311. if (m/^Source$/) {
  312. set_source_package($v);
  313. $fields->{$_} = $v;
  314. } elsif (m/^Version$/) {
  315. my ($ok, $error) = version_check($v);
  316. error($error) unless $ok;
  317. $fields->{$_} = $v;
  318. } elsif (m/^Binary-Only$/) {
  319. error(_g('building source for a binary-only release'))
  320. if $v eq 'yes' and $options{opmode} eq '-b';
  321. } elsif (m/^Maintainer$/i) {
  322. # Do not replace the field coming from the source entry
  323. } else {
  324. field_transfer_single($changelog, $fields);
  325. }
  326. }
  327. $fields->{'Binary'} = join(', ', @binarypackages);
  328. # Avoid overly long line by splitting over multiple lines
  329. if (length($fields->{'Binary'}) > 980) {
  330. $fields->{'Binary'} =~ s/(.{0,980}), ?/$1,\n/g;
  331. }
  332. # Select the format to use
  333. if (not defined $build_format) {
  334. if (-e "$dir/debian/source/format") {
  335. open(my $format_fh, '<', "$dir/debian/source/format")
  336. or syserr(_g('cannot read %s'), "$dir/debian/source/format");
  337. $build_format = <$format_fh>;
  338. chomp($build_format) if defined $build_format;
  339. error(_g('%s is empty'), "$dir/debian/source/format")
  340. unless defined $build_format and length $build_format;
  341. close($format_fh);
  342. } else {
  343. warning(_g('no source format specified in %s, ' .
  344. 'see dpkg-source(1)'), 'debian/source/format')
  345. if $options{opmode} eq '-b';
  346. $build_format = '1.0';
  347. }
  348. }
  349. $fields->{'Format'} = $build_format;
  350. $srcpkg->upgrade_object_type(); # Fails if format is unsupported
  351. # Parse command line options
  352. $srcpkg->init_options();
  353. $srcpkg->parse_cmdline_options(@cmdline_options);
  354. if ($options{opmode} eq '--print-format') {
  355. print $fields->{'Format'} . "\n";
  356. exit(0);
  357. } elsif ($options{opmode} eq '--before-build') {
  358. $srcpkg->before_build($dir);
  359. exit(0);
  360. } elsif ($options{opmode} eq '--after-build') {
  361. $srcpkg->after_build($dir);
  362. exit(0);
  363. } elsif ($options{opmode} eq '--commit') {
  364. $srcpkg->commit($dir);
  365. exit(0);
  366. }
  367. # Verify pre-requisites are met
  368. my ($res, $msg) = $srcpkg->can_build($dir);
  369. error(_g("can't build with source format '%s': %s"), $build_format, $msg) unless $res;
  370. # Only -b left
  371. info(_g("using source format `%s'"), $fields->{'Format'});
  372. run_vendor_hook('before-source-build', $srcpkg);
  373. # Build the files (.tar.gz, .diff.gz, etc)
  374. $srcpkg->build($dir);
  375. # Write the .dsc
  376. my $dscname = $srcpkg->get_basename(1) . '.dsc';
  377. info(_g('building %s in %s'), get_source_package(), $dscname);
  378. $srcpkg->write_dsc(filename => $dscname,
  379. remove => \%remove,
  380. override => \%override,
  381. substvars => $substvars);
  382. exit(0);
  383. } elsif ($options{opmode} eq '-x') {
  384. # Check command line
  385. unless (scalar(@ARGV)) {
  386. usageerr(_g('-x needs at least one argument, the .dsc'));
  387. }
  388. if (scalar(@ARGV) > 2) {
  389. usageerr(_g('-x takes no more than two arguments'));
  390. }
  391. my $dsc = shift(@ARGV);
  392. if (-d $dsc) {
  393. usageerr(_g('-x needs the .dsc file as first argument, not a directory'));
  394. }
  395. # Create the object that does everything
  396. my $srcpkg = Dpkg::Source::Package->new(filename => $dsc,
  397. options => \%options);
  398. # Parse command line options
  399. $srcpkg->parse_cmdline_options(@cmdline_options);
  400. # Decide where to unpack
  401. my $newdirectory = $srcpkg->get_basename();
  402. $newdirectory =~ s/_/-/g;
  403. if (@ARGV) {
  404. $newdirectory = File::Spec->catdir(shift(@ARGV));
  405. if (-e $newdirectory) {
  406. error(_g('unpack target exists: %s'), $newdirectory);
  407. }
  408. }
  409. # Various checks before unpacking
  410. unless ($options{no_check}) {
  411. if ($srcpkg->is_signed()) {
  412. $srcpkg->check_signature();
  413. } else {
  414. if ($options{require_valid_signature}) {
  415. error(_g("%s doesn't contain a valid OpenPGP signature"), $dsc);
  416. } else {
  417. warning(_g('extracting unsigned source package (%s)'), $dsc);
  418. }
  419. }
  420. $srcpkg->check_checksums();
  421. }
  422. # Unpack the source package (delegated to Dpkg::Source::Package::*)
  423. info(_g('extracting %s in %s'), $srcpkg->{fields}{'Source'}, $newdirectory);
  424. $srcpkg->extract($newdirectory);
  425. exit(0);
  426. }
  427. sub setopmode {
  428. if (defined($options{opmode})) {
  429. usageerr(_g('only one of -x, -b or --print-format allowed, and only once'));
  430. }
  431. $options{opmode} = $_[0];
  432. }
  433. sub version {
  434. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  435. print _g('
  436. This is free software; see the GNU General Public License version 2 or
  437. later for copying conditions. There is NO warranty.
  438. ');
  439. }
  440. sub usage {
  441. printf _g(
  442. 'Usage: %s [<option>...] <command>')
  443. . "\n\n" . _g(
  444. 'Commands:
  445. -x <filename>.dsc [<output-dir>]
  446. extract source package.
  447. -b <dir> build source package.
  448. --print-format <dir> print the source format that would be
  449. used to build the source package.
  450. --commit [<dir> [<patch-name>]]
  451. store upstream changes in a new patch.')
  452. . "\n\n" . _g(
  453. "Build options:
  454. -c<control-file> get control info from this file.
  455. -l<changelog-file> get per-version info from this file.
  456. -F<changelog-format> force changelog format.
  457. -V<name>=<value> set a substitution variable.
  458. -T<substvars-file> read variables here.
  459. -D<field>=<value> override or add a .dsc field and value.
  460. -U<field> remove a field.
  461. -q quiet mode.
  462. -i[<regex>] filter out files to ignore diffs of
  463. (defaults to: '%s').
  464. -I[<pattern>] filter out files when building tarballs
  465. (defaults to: %s).
  466. -Z<compression> select compression to use (defaults to '%s',
  467. supported are: %s).
  468. -z<level> compression level to use (defaults to '%d',
  469. supported are: '1'-'9', 'best', 'fast')")
  470. . "\n\n" . _g(
  471. "Extract options:
  472. --no-copy don't copy .orig tarballs
  473. --no-check don't check signature and checksums before unpacking
  474. --require-valid-signature abort if the package doesn't have a valid signature")
  475. . "\n\n" . _g(
  476. 'General options:
  477. -?, --help show this help message.
  478. --version show the version.')
  479. . "\n\n" . _g(
  480. 'More options are available but they depend on the source package format.
  481. See dpkg-source(1) for more info.') . "\n",
  482. $Dpkg::PROGNAME,
  483. get_default_diff_ignore_regex(),
  484. join(' ', map { "-I$_" } get_default_tar_ignore_pattern()),
  485. compression_get_default(),
  486. join(' ', compression_get_list()),
  487. compression_get_default_level();
  488. }