dpkg-source.pl 17 KB

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