dpkg-source.pl 13 KB

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