dpkg-source.pl 15 KB

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