dpkg-source.pl 13 KB

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