V2.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. use base 'Dpkg::Source::Package';
  19. use Dpkg;
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Compression;
  23. use Dpkg::Source::Archive;
  24. use Dpkg::Source::Patch;
  25. use Dpkg::Exit;
  26. use Dpkg::Source::Functions qw(erasedir is_binary);
  27. use POSIX;
  28. use File::Basename;
  29. use File::Temp qw(tempfile tempdir);
  30. use File::Path;
  31. use File::Spec;
  32. use File::Find;
  33. our $CURRENT_MINOR_VERSION = "0";
  34. sub init_options {
  35. my ($self) = @_;
  36. $self->SUPER::init_options();
  37. $self->{'options'}{'include_removal'} = 0
  38. unless exists $self->{'options'}{'include_removal'};
  39. $self->{'options'}{'include_timestamp'} = 0
  40. unless exists $self->{'options'}{'include_timestamp'};
  41. $self->{'options'}{'include_binaries'} = 0
  42. unless exists $self->{'options'}{'include_binaries'};
  43. $self->{'options'}{'preparation'} = 1
  44. unless exists $self->{'options'}{'preparation'};
  45. $self->{'options'}{'skip_patches'} = 0
  46. unless exists $self->{'options'}{'skip_patches'};
  47. $self->{'options'}{'skip_debianization'} = 0
  48. unless exists $self->{'options'}{'skip_debianization'};
  49. }
  50. sub parse_cmdline_option {
  51. my ($self, $opt) = @_;
  52. if ($opt =~ /^--include-removal$/) {
  53. $self->{'options'}{'include_removal'} = 1;
  54. return 1;
  55. } elsif ($opt =~ /^--include-timestamp$/) {
  56. $self->{'options'}{'include_timestamp'} = 1;
  57. return 1;
  58. } elsif ($opt =~ /^--include-binaries$/) {
  59. $self->{'options'}{'include_binaries'} = 1;
  60. return 1;
  61. } elsif ($opt =~ /^--no-preparation$/) {
  62. $self->{'options'}{'preparation'} = 0;
  63. return 1;
  64. } elsif ($opt =~ /^--skip-patches$/) {
  65. $self->{'options'}{'skip_patches'} = 1;
  66. return 1;
  67. } elsif ($opt =~ /^--skip-debianization$/) {
  68. $self->{'options'}{'skip_debianization'} = 1;
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. sub do_extract {
  74. my ($self, $newdirectory) = @_;
  75. my $fields = $self->{'fields'};
  76. my $dscdir = $self->{'basedir'};
  77. my $basename = $self->get_basename();
  78. my $basenamerev = $self->get_basename(1);
  79. my ($tarfile, $debianfile, %origtar, %seen);
  80. foreach my $file ($self->get_files()) {
  81. (my $uncompressed = $file) =~ s/\.$comp_regex$//;
  82. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  83. $uncompressed) if $seen{$uncompressed};
  84. $seen{$uncompressed} = 1;
  85. if ($file =~ /^\Q$basename\E\.orig\.tar\.$comp_regex$/) {
  86. $tarfile = $file;
  87. } elsif ($file =~ /^\Q$basename\E\.orig-([\w-]+)\.tar\.$comp_regex$/) {
  88. $origtar{$1} = $file;
  89. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$comp_regex$/) {
  90. $debianfile = $file;
  91. } else {
  92. error(_g("unrecognized file for a %s source package: %s"),
  93. "v2.0", $file);
  94. }
  95. }
  96. unless ($tarfile and $debianfile) {
  97. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  98. }
  99. erasedir($newdirectory);
  100. # Extract main tarball
  101. info(_g("unpacking %s"), $tarfile);
  102. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  103. $tar->extract($newdirectory, no_fixperms => 1);
  104. # Extract additional orig tarballs
  105. foreach my $subdir (keys %origtar) {
  106. my $file = $origtar{$subdir};
  107. info(_g("unpacking %s"), $file);
  108. if (-e "$newdirectory/$subdir") {
  109. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  110. erasedir("$newdirectory/$subdir");
  111. }
  112. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  113. $tar->extract("$newdirectory/$subdir", no_fixperms => 1);
  114. }
  115. # Stop here if debianization is not wanted
  116. return if $self->{'options'}{'skip_debianization'};
  117. # Extract debian tarball after removing the debian directory
  118. info(_g("unpacking %s"), $debianfile);
  119. erasedir("$newdirectory/debian");
  120. # Exclude existing symlinks from extraction of debian.tar.gz as we
  121. # don't want to overwrite something outside of $newdirectory due to a
  122. # symlink
  123. my @exclude_symlinks;
  124. my $wanted = sub {
  125. return if not -l $_;
  126. my $fn = File::Spec->abs2rel($_, $newdirectory);
  127. push @exclude_symlinks, "--exclude", $fn;
  128. };
  129. find({ wanted => $wanted, no_chdir => 1 }, $newdirectory);
  130. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  131. $tar->extract($newdirectory, in_place => 1,
  132. options => [ '--anchored', '--no-wildcards',
  133. @exclude_symlinks ]);
  134. # Apply patches (in a separate method as it might be overriden)
  135. $self->apply_patches($newdirectory) unless $self->{'options'}{'skip_patches'};
  136. }
  137. sub get_autopatch_name {
  138. return "zz_debian-diff-auto";
  139. }
  140. sub get_patches {
  141. my ($self, $dir, $skip_auto) = @_;
  142. my @patches;
  143. my $pd = "$dir/debian/patches";
  144. my $auto_patch = $self->get_autopatch_name();
  145. if (-d $pd) {
  146. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  147. foreach my $patch (sort readdir(DIR)) {
  148. # patches match same rules as run-parts
  149. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  150. next if $skip_auto and $patch eq $auto_patch;
  151. push @patches, $patch;
  152. }
  153. closedir(DIR);
  154. }
  155. return @patches;
  156. }
  157. sub apply_patches {
  158. my ($self, $dir, $skip_auto) = @_;
  159. my @patches = $self->get_patches($dir, $skip_auto);
  160. return unless scalar(@patches);
  161. my $timestamp = time();
  162. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  163. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  164. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  165. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  166. info(_g("applying %s"), $patch) unless $skip_auto;
  167. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  168. $patch_obj->apply($dir, force_timestamp => 1,
  169. timestamp => $timestamp,
  170. add_options => [ '-E' ]);
  171. print APPLIED "$patch\n";
  172. }
  173. close(APPLIED);
  174. }
  175. sub can_build {
  176. my ($self, $dir) = @_;
  177. foreach ($self->find_original_tarballs()) {
  178. return 1 if /\.orig\.tar\.$comp_regex$/;
  179. }
  180. return (0, _g("no orig.tar file found"));
  181. }
  182. sub prepare_build {
  183. my ($self, $dir) = @_;
  184. $self->{'diff_options'} = {
  185. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'},
  186. include_removal => $self->{'options'}{'include_removal'},
  187. include_timestamp => $self->{'options'}{'include_timestamp'},
  188. use_dev_null => 1,
  189. };
  190. push @{$self->{'options'}{'tar_ignore'}}, "debian/patches/.dpkg-source-applied";
  191. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  192. }
  193. sub check_patches_applied {
  194. my ($self, $dir) = @_;
  195. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  196. unless (-e $applied) {
  197. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  198. $self->apply_patches($dir);
  199. }
  200. }
  201. sub do_build {
  202. my ($self, $dir) = @_;
  203. my ($dirname, $updir) = fileparse($dir);
  204. my @argv = @{$self->{'options'}{'ARGV'}};
  205. if (scalar(@argv)) {
  206. usageerr(_g("-b takes only one parameter with format `%s'"),
  207. $self->{'fields'}{'Format'});
  208. }
  209. $self->prepare_build($dir);
  210. my $include_binaries = $self->{'options'}{'include_binaries'};
  211. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  212. my $sourcepackage = $self->{'fields'}{'Source'};
  213. my $basenamerev = $self->get_basename(1);
  214. my $basename = $self->get_basename();
  215. my $basedirname = $basename;
  216. $basedirname =~ s/_/-/;
  217. # Identify original tarballs
  218. my ($tarfile, %origtar);
  219. my @origtarballs;
  220. foreach (sort $self->find_original_tarballs()) {
  221. if (/\.orig\.tar\.$comp_regex$/) {
  222. $tarfile = $_;
  223. push @origtarballs, $_;
  224. $self->add_file($_);
  225. } elsif (/\.orig-([\w-]+)\.tar\.$comp_regex$/) {
  226. $origtar{$1} = $_;
  227. push @origtarballs, $_;
  228. $self->add_file($_);
  229. }
  230. }
  231. error(_g("no orig.tar file found")) unless $tarfile;
  232. info(_g("building %s using existing %s"),
  233. $sourcepackage, "@origtarballs");
  234. # Unpack a second copy for comparison
  235. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  236. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  237. # Extract main tarball
  238. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  239. $tar->extract($tmp);
  240. # Extract additional orig tarballs
  241. foreach my $subdir (keys %origtar) {
  242. my $file = $origtar{$subdir};
  243. $tar = Dpkg::Source::Archive->new(filename => $file);
  244. $tar->extract("$tmp/$subdir");
  245. }
  246. # Copy over the debian directory
  247. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  248. subprocerr(_g("copy of the debian directory")) if $?;
  249. # Apply all patches except the last automatic one
  250. $self->apply_patches($tmp, 1);
  251. # Prepare handling of binary files
  252. my %auth_bin_files;
  253. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  254. if (-f $incbin_file) {
  255. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  256. while(defined($_ = <INC>)) {
  257. chomp; s/^\s*//; s/\s*$//;
  258. next if /^#/ or /^$/;
  259. $auth_bin_files{$_} = 1;
  260. }
  261. close(INC);
  262. }
  263. my @binary_files;
  264. my $handle_binary = sub {
  265. my ($self, $old, $new) = @_;
  266. my $relfn = File::Spec->abs2rel($new, $dir);
  267. # Include binaries if they are whitelisted or if
  268. # --include-binaries has been given
  269. if ($include_binaries or $auth_bin_files{$relfn}) {
  270. push @binary_files, $relfn;
  271. } else {
  272. errormsg(_g("cannot represent change to %s: %s"), $new,
  273. _g("binary file contents changed"));
  274. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  275. " to store the modified binary in the debian tarball"),
  276. $relfn);
  277. $self->register_error();
  278. }
  279. };
  280. # Check if the debian directory contains unwanted binary files
  281. my $unwanted_binaries = 0;
  282. my $check_binary = sub {
  283. my $fn = File::Spec->abs2rel($_, $dir);
  284. if (-f $_ and is_binary($_)) {
  285. if ($include_binaries or $auth_bin_files{$fn}) {
  286. push @binary_files, $fn;
  287. } else {
  288. errormsg(_g("unwanted binary file: %s"), $fn);
  289. $unwanted_binaries++;
  290. }
  291. }
  292. };
  293. my $tar_ignore_glob = "{" . join(",",
  294. map {
  295. my $copy = $_;
  296. $copy =~ s/,/\\,/g;
  297. $copy;
  298. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  299. my $filter_ignore = sub {
  300. # Filter out files that are not going to be included in the debian
  301. # tarball due to ignores.
  302. my %exclude;
  303. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  304. foreach my $fn (glob($tar_ignore_glob)) {
  305. $exclude{$fn} = 1;
  306. }
  307. my @result;
  308. foreach my $fn (@_) {
  309. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  310. push @result, $fn;
  311. }
  312. }
  313. return @result;
  314. };
  315. find({ wanted => $check_binary, preprocess => $filter_ignore,
  316. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  317. error(_g("detected %d unwanted binary file(s) " .
  318. "(add them in debian/source/include-binaries to allow their " .
  319. "inclusion)."), $unwanted_binaries) if $unwanted_binaries;
  320. # Create a patch
  321. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  322. DIR => $updir, UNLINK => 0);
  323. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  324. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  325. compression => "none");
  326. $diff->create();
  327. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  328. %{$self->{'diff_options'}}, handle_binary_func => $handle_binary);
  329. error(_g("unrepresentable changes to source")) if not $diff->finish();
  330. # The previous auto-patch must be removed, it has not been used and it
  331. # will be recreated if it's still needed
  332. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  333. $self->get_autopatch_name());
  334. if (-e $autopatch) {
  335. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  336. }
  337. # Install the diff as the new autopatch
  338. if (not -s $tmpdiff) {
  339. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  340. } else {
  341. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  342. info(_g("local changes stored in %s, the modified files are:"), $autopatch);
  343. my $analysis = $diff->analyze($dir);
  344. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  345. print " $fn\n";
  346. }
  347. rename($tmpdiff, $autopatch) ||
  348. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  349. chmod(0666 & ~ umask(), $autopatch) ||
  350. syserr(_g("unable to change permission of `%s'"), $autopatch);
  351. }
  352. $self->register_autopatch($dir);
  353. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  354. pop @Dpkg::Exit::handlers;
  355. # Remove the temporary directory
  356. erasedir($tmp);
  357. pop @Dpkg::Exit::handlers;
  358. # Update debian/source/include-binaries if needed
  359. if (scalar(@binary_files) and $include_binaries) {
  360. mkpath(File::Spec->catdir($dir, "debian", "source"));
  361. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  362. foreach my $binary (@binary_files) {
  363. unless ($auth_bin_files{$binary}) {
  364. print INC "$binary\n";
  365. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  366. }
  367. }
  368. close(INC);
  369. }
  370. # Create the debian.tar
  371. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  372. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  373. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  374. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  375. $tar->add_directory("debian");
  376. foreach my $binary (@binary_files) {
  377. $tar->add_file($binary) unless $binary =~ m{^debian/};
  378. }
  379. $tar->finish();
  380. $self->add_file($debianfile);
  381. }
  382. sub register_autopatch {
  383. my ($self, $dir) = @_;
  384. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  385. $self->get_autopatch_name());
  386. if (-e $autopatch) {
  387. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  388. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  389. print APPLIED ($self->get_autopatch_name() . "\n");
  390. close(APPLIED);
  391. }
  392. }
  393. # vim:et:sw=4:ts=8
  394. 1;