dpkg-source.pl 13 KB

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