V2.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Package::V2;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "0.01";
  19. use base 'Dpkg::Source::Package';
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Compression;
  24. use Dpkg::Source::Archive;
  25. use Dpkg::Source::Patch;
  26. use Dpkg::Exit;
  27. use Dpkg::Source::Functions qw(erasedir is_binary);
  28. use POSIX;
  29. use File::Basename;
  30. use File::Temp qw(tempfile tempdir);
  31. use File::Path;
  32. use File::Spec;
  33. use File::Find;
  34. our $CURRENT_MINOR_VERSION = "0";
  35. sub init_options {
  36. my ($self) = @_;
  37. $self->SUPER::init_options();
  38. $self->{'options'}{'include_removal'} = 0
  39. unless exists $self->{'options'}{'include_removal'};
  40. $self->{'options'}{'include_timestamp'} = 0
  41. unless exists $self->{'options'}{'include_timestamp'};
  42. $self->{'options'}{'include_binaries'} = 0
  43. unless exists $self->{'options'}{'include_binaries'};
  44. $self->{'options'}{'preparation'} = 1
  45. unless exists $self->{'options'}{'preparation'};
  46. $self->{'options'}{'skip_patches'} = 0
  47. unless exists $self->{'options'}{'skip_patches'};
  48. $self->{'options'}{'unapply_patches'} = 0
  49. unless exists $self->{'options'}{'unapply_patches'};
  50. $self->{'options'}{'skip_debianization'} = 0
  51. unless exists $self->{'options'}{'skip_debianization'};
  52. $self->{'options'}{'create_empty_orig'} = 0
  53. unless exists $self->{'options'}{'create_empty_orig'};
  54. $self->{'options'}{'abort_on_upstream_changes'} = 0
  55. unless exists $self->{'options'}{'abort_on_upstream_changes'};
  56. }
  57. sub parse_cmdline_option {
  58. my ($self, $opt) = @_;
  59. if ($opt =~ /^--include-removal$/) {
  60. $self->{'options'}{'include_removal'} = 1;
  61. return 1;
  62. } elsif ($opt =~ /^--include-timestamp$/) {
  63. $self->{'options'}{'include_timestamp'} = 1;
  64. return 1;
  65. } elsif ($opt =~ /^--include-binaries$/) {
  66. $self->{'options'}{'include_binaries'} = 1;
  67. return 1;
  68. } elsif ($opt =~ /^--no-preparation$/) {
  69. $self->{'options'}{'preparation'} = 0;
  70. return 1;
  71. } elsif ($opt =~ /^--skip-patches$/) {
  72. $self->{'options'}{'skip_patches'} = 1;
  73. return 1;
  74. } elsif ($opt =~ /^--unapply-patches$/) {
  75. $self->{'options'}{'unapply_patches'} = 1;
  76. return 1;
  77. } elsif ($opt =~ /^--skip-debianization$/) {
  78. $self->{'options'}{'skip_debianization'} = 1;
  79. return 1;
  80. } elsif ($opt =~ /^--create-empty-orig$/) {
  81. $self->{'options'}{'create_empty_orig'} = 1;
  82. return 1;
  83. } elsif ($opt =~ /^--abort-on-upstream-changes$/) {
  84. $self->{'options'}{'abort_on_upstream_changes'} = 1;
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. sub do_extract {
  90. my ($self, $newdirectory) = @_;
  91. my $fields = $self->{'fields'};
  92. my $dscdir = $self->{'basedir'};
  93. my $basename = $self->get_basename();
  94. my $basenamerev = $self->get_basename(1);
  95. my ($tarfile, $debianfile, %origtar, %seen);
  96. my $re_ext = $compression_re_file_ext;
  97. foreach my $file ($self->get_files()) {
  98. (my $uncompressed = $file) =~ s/\.$re_ext$//;
  99. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  100. $uncompressed) if $seen{$uncompressed};
  101. $seen{$uncompressed} = 1;
  102. if ($file =~ /^\Q$basename\E\.orig\.tar\.$re_ext$/) {
  103. $tarfile = $file;
  104. } elsif ($file =~ /^\Q$basename\E\.orig-([[:alnum:]-]+)\.tar\.$re_ext$/) {
  105. $origtar{$1} = $file;
  106. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$re_ext$/) {
  107. $debianfile = $file;
  108. } else {
  109. error(_g("unrecognized file for a %s source package: %s"),
  110. "v2.0", $file);
  111. }
  112. }
  113. unless ($tarfile and $debianfile) {
  114. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  115. }
  116. erasedir($newdirectory);
  117. # Extract main tarball
  118. info(_g("unpacking %s"), $tarfile);
  119. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  120. $tar->extract($newdirectory, no_fixperms => 1,
  121. options => [ "--anchored", "--no-wildcards-match-slash",
  122. "--exclude", "*/.pc", "--exclude", ".pc" ]);
  123. # The .pc exclusion is only needed for 3.0 (quilt) and to avoid
  124. # having an upstream tarball provide a directory with symlinks
  125. # that would be blindly followed when applying the patches
  126. # Extract additional orig tarballs
  127. foreach my $subdir (keys %origtar) {
  128. my $file = $origtar{$subdir};
  129. info(_g("unpacking %s"), $file);
  130. if (-e "$newdirectory/$subdir") {
  131. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  132. erasedir("$newdirectory/$subdir");
  133. }
  134. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  135. $tar->extract("$newdirectory/$subdir", no_fixperms => 1);
  136. }
  137. # Stop here if debianization is not wanted
  138. return if $self->{'options'}{'skip_debianization'};
  139. # Extract debian tarball after removing the debian directory
  140. info(_g("unpacking %s"), $debianfile);
  141. erasedir("$newdirectory/debian");
  142. # Exclude existing symlinks from extraction of debian.tar.gz as we
  143. # don't want to overwrite something outside of $newdirectory due to a
  144. # symlink
  145. my @exclude_symlinks;
  146. my $wanted = sub {
  147. return if not -l $_;
  148. my $fn = File::Spec->abs2rel($_, $newdirectory);
  149. push @exclude_symlinks, "--exclude", $fn;
  150. };
  151. find({ wanted => $wanted, no_chdir => 1 }, $newdirectory);
  152. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  153. $tar->extract($newdirectory, in_place => 1,
  154. options => [ '--anchored', '--no-wildcards',
  155. @exclude_symlinks ]);
  156. # Apply patches (in a separate method as it might be overriden)
  157. $self->apply_patches($newdirectory, usage => 'unpack')
  158. unless $self->{'options'}{'skip_patches'};
  159. }
  160. sub get_autopatch_name {
  161. return "zz_debian-diff-auto";
  162. }
  163. sub get_patches {
  164. my ($self, $dir, %opts) = @_;
  165. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  166. my @patches;
  167. my $pd = "$dir/debian/patches";
  168. my $auto_patch = $self->get_autopatch_name();
  169. if (-d $pd) {
  170. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  171. foreach my $patch (sort readdir(DIR)) {
  172. # patches match same rules as run-parts
  173. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  174. next if $opts{"skip_auto"} and $patch eq $auto_patch;
  175. push @patches, $patch;
  176. }
  177. closedir(DIR);
  178. }
  179. return @patches;
  180. }
  181. sub apply_patches {
  182. my ($self, $dir, %opts) = @_;
  183. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  184. my @patches = $self->get_patches($dir, %opts);
  185. return unless scalar(@patches);
  186. my $timestamp = time();
  187. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  188. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  189. foreach my $patch ($self->get_patches($dir, %opts)) {
  190. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  191. info(_g("applying %s"), $patch) unless $opts{"skip_auto"};
  192. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  193. $patch_obj->apply($dir, force_timestamp => 1,
  194. timestamp => $timestamp,
  195. add_options => [ '-E' ]);
  196. print APPLIED "$patch\n";
  197. }
  198. close(APPLIED);
  199. }
  200. sub unapply_patches {
  201. my ($self, $dir, %opts) = @_;
  202. my @patches = reverse($self->get_patches($dir, %opts));
  203. return unless scalar(@patches);
  204. my $timestamp = time();
  205. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  206. foreach my $patch (@patches) {
  207. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  208. info(_g("unapplying %s"), $patch) unless $opts{"quiet"};
  209. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  210. $patch_obj->apply($dir, force_timestamp => 1,
  211. timestamp => $timestamp,
  212. add_options => [ '-E', '-R' ]);
  213. }
  214. unlink($applied);
  215. }
  216. sub can_build {
  217. my ($self, $dir) = @_;
  218. return 1 if $self->find_original_tarballs(include_supplementary => 0);
  219. return 1 if $self->{'options'}{'create_empty_orig'} and
  220. $self->find_original_tarballs(include_main => 0);
  221. return (0, _g("no orig.tar file found"));
  222. }
  223. sub before_build {
  224. my ($self, $dir) = @_;
  225. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  226. }
  227. sub after_build {
  228. my ($self, $dir) = @_;
  229. $self->unapply_patches($dir) if $self->{'options'}{'unapply_patches'};
  230. }
  231. sub prepare_build {
  232. my ($self, $dir) = @_;
  233. $self->{'diff_options'} = {
  234. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'},
  235. include_removal => $self->{'options'}{'include_removal'},
  236. include_timestamp => $self->{'options'}{'include_timestamp'},
  237. use_dev_null => 1,
  238. };
  239. push @{$self->{'options'}{'tar_ignore'}}, "debian/patches/.dpkg-source-applied";
  240. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  241. if ($self->{'options'}{'create_empty_orig'} and
  242. not $self->find_original_tarballs(include_supplementary => 0))
  243. {
  244. # No main orig.tar, create a dummy one
  245. my $filename = $self->get_basename() . ".orig.tar." .
  246. $self->{'options'}{'comp_ext'};
  247. my $tar = Dpkg::Source::Archive->new(filename => $filename);
  248. $tar->create();
  249. $tar->finish();
  250. }
  251. }
  252. sub check_patches_applied {
  253. my ($self, $dir) = @_;
  254. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  255. unless (-e $applied) {
  256. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  257. $self->apply_patches($dir, usage => 'preparation');
  258. }
  259. }
  260. sub do_build {
  261. my ($self, $dir) = @_;
  262. my ($dirname, $updir) = fileparse($dir);
  263. my @argv = @{$self->{'options'}{'ARGV'}};
  264. if (scalar(@argv)) {
  265. usageerr(_g("-b takes only one parameter with format `%s'"),
  266. $self->{'fields'}{'Format'});
  267. }
  268. $self->prepare_build($dir);
  269. my $include_binaries = $self->{'options'}{'include_binaries'};
  270. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  271. my $sourcepackage = $self->{'fields'}{'Source'};
  272. my $basenamerev = $self->get_basename(1);
  273. my $basename = $self->get_basename();
  274. my $basedirname = $basename;
  275. $basedirname =~ s/_/-/;
  276. # Identify original tarballs
  277. my ($tarfile, %origtar);
  278. my @origtarballs;
  279. foreach (sort $self->find_original_tarballs()) {
  280. if (/\.orig\.tar\.$compression_re_file_ext$/) {
  281. if (defined($tarfile)) {
  282. error(_g("several orig.tar files found (%s and %s) but only " .
  283. "one is allowed"), $tarfile, $_);
  284. }
  285. $tarfile = $_;
  286. push @origtarballs, $_;
  287. $self->add_file($_);
  288. } elsif (/\.orig-([[:alnum:]-]+)\.tar\.$compression_re_file_ext$/) {
  289. $origtar{$1} = $_;
  290. push @origtarballs, $_;
  291. $self->add_file($_);
  292. }
  293. }
  294. error(_g("no orig.tar file found")) unless $tarfile;
  295. info(_g("building %s using existing %s"),
  296. $sourcepackage, "@origtarballs");
  297. # Unpack a second copy for comparison
  298. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  299. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  300. # Extract main tarball
  301. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  302. $tar->extract($tmp);
  303. # Extract additional orig tarballs
  304. foreach my $subdir (keys %origtar) {
  305. my $file = $origtar{$subdir};
  306. $tar = Dpkg::Source::Archive->new(filename => $file);
  307. $tar->extract("$tmp/$subdir");
  308. }
  309. # Copy over the debian directory
  310. erasedir("$tmp/debian");
  311. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  312. subprocerr(_g("copy of the debian directory")) if $?;
  313. # Apply all patches except the last automatic one
  314. $self->apply_patches($tmp, skip_auto => 1, usage => 'build');
  315. # Prepare handling of binary files
  316. my %auth_bin_files;
  317. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  318. if (-f $incbin_file) {
  319. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  320. while(defined($_ = <INC>)) {
  321. chomp; s/^\s*//; s/\s*$//;
  322. next if /^#/ or /^$/;
  323. $auth_bin_files{$_} = 1;
  324. }
  325. close(INC);
  326. }
  327. my @binary_files;
  328. my $handle_binary = sub {
  329. my ($self, $old, $new) = @_;
  330. my $relfn = File::Spec->abs2rel($new, $dir);
  331. # Include binaries if they are whitelisted or if
  332. # --include-binaries has been given
  333. if ($include_binaries or $auth_bin_files{$relfn}) {
  334. push @binary_files, $relfn;
  335. } else {
  336. errormsg(_g("cannot represent change to %s: %s"), $new,
  337. _g("binary file contents changed"));
  338. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  339. " to store the modified binary in the debian tarball"),
  340. $relfn);
  341. $self->register_error();
  342. }
  343. };
  344. # Check if the debian directory contains unwanted binary files
  345. my $unwanted_binaries = 0;
  346. my $check_binary = sub {
  347. my $fn = File::Spec->abs2rel($_, $dir);
  348. if (-f $_ and is_binary($_)) {
  349. if ($include_binaries or $auth_bin_files{$fn}) {
  350. push @binary_files, $fn;
  351. } else {
  352. errormsg(_g("unwanted binary file: %s"), $fn);
  353. $unwanted_binaries++;
  354. }
  355. }
  356. };
  357. my $tar_ignore_glob = "{" . join(",",
  358. map {
  359. my $copy = $_;
  360. $copy =~ s/,/\\,/g;
  361. $copy;
  362. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  363. my $filter_ignore = sub {
  364. # Filter out files that are not going to be included in the debian
  365. # tarball due to ignores.
  366. my %exclude;
  367. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  368. foreach my $fn (glob($tar_ignore_glob)) {
  369. $exclude{$fn} = 1;
  370. }
  371. my @result;
  372. foreach my $fn (@_) {
  373. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  374. push @result, $fn;
  375. }
  376. }
  377. return @result;
  378. };
  379. find({ wanted => $check_binary, preprocess => $filter_ignore,
  380. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  381. error(P_("detected %d unwanted binary file (add it in " .
  382. "debian/source/include-binaries to allow its inclusion).",
  383. "detected %d unwanted binary files (add them in " .
  384. "debian/source/include-binaries to allow their inclusion).",
  385. $unwanted_binaries), $unwanted_binaries)
  386. if $unwanted_binaries;
  387. # Create a patch
  388. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  389. $self->get_autopatch_name());
  390. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  391. DIR => $updir, UNLINK => 0);
  392. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  393. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  394. compression => "none");
  395. $diff->create();
  396. $diff->set_header($self->get_patch_header($dir, $autopatch));
  397. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  398. %{$self->{'diff_options'}}, handle_binary_func => $handle_binary);
  399. error(_g("unrepresentable changes to source")) if not $diff->finish();
  400. # The previous auto-patch must be removed, it has not been used and it
  401. # will be recreated if it's still needed
  402. if (-e $autopatch) {
  403. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  404. }
  405. # Install the diff as the new autopatch
  406. if (not -s $tmpdiff) {
  407. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  408. } else {
  409. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  410. info(_g("local changes stored in %s, the modified files are:"), $autopatch);
  411. my $analysis = $diff->analyze($dir);
  412. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  413. print " $fn\n";
  414. }
  415. rename($tmpdiff, $autopatch) ||
  416. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  417. chmod(0666 & ~ umask(), $autopatch) ||
  418. syserr(_g("unable to change permission of `%s'"), $autopatch);
  419. }
  420. $self->register_autopatch($dir);
  421. if (-e $autopatch and $self->{'options'}{'abort_on_upstream_changes'}) {
  422. error(_g("aborting due to --abort-on-upstream-changes"));
  423. }
  424. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  425. pop @Dpkg::Exit::handlers;
  426. # Remove the temporary directory
  427. erasedir($tmp);
  428. pop @Dpkg::Exit::handlers;
  429. # Update debian/source/include-binaries if needed
  430. if (scalar(@binary_files) and $include_binaries) {
  431. mkpath(File::Spec->catdir($dir, "debian", "source"));
  432. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  433. foreach my $binary (@binary_files) {
  434. unless ($auth_bin_files{$binary}) {
  435. print INC "$binary\n";
  436. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  437. }
  438. }
  439. close(INC);
  440. }
  441. # Create the debian.tar
  442. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  443. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  444. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  445. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  446. $tar->add_directory("debian");
  447. foreach my $binary (@binary_files) {
  448. $tar->add_file($binary) unless $binary =~ m{^debian/};
  449. }
  450. $tar->finish();
  451. $self->add_file($debianfile);
  452. }
  453. sub get_patch_header {
  454. my ($self, $dir, $previous) = @_;
  455. my $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  456. my $text;
  457. if (-f $ph) {
  458. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  459. $text = join("", <PH>);
  460. close(PH);
  461. return $text;
  462. }
  463. return "Description: Undocumented upstream changes
  464. This patch has been created by dpkg-source during the package build
  465. but it might have accumulated changes from several uploads. Please
  466. check the changelog to (hopefully) learn more on those changes.\n\n";
  467. }
  468. sub register_autopatch {
  469. my ($self, $dir) = @_;
  470. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  471. $self->get_autopatch_name());
  472. if (-e $autopatch) {
  473. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  474. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  475. print APPLIED ($self->get_autopatch_name() . "\n");
  476. close(APPLIED);
  477. }
  478. }
  479. # vim:et:sw=4:ts=8
  480. 1;