dpkg-source.pl 13 KB

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