dpkg-source.pl 14 KB

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