dpkg-source.pl 15 KB

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