dpkg-source.pl 15 KB

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