dpkg-source.pl 18 KB

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