dpkg-source.pl 14 KB

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