dpkg-source.pl 17 KB

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