V2_0.pm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Copyright 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Source::Package::V2_0;
  14. use strict;
  15. use warnings;
  16. use base 'Dpkg::Source::Package';
  17. use Dpkg;
  18. use Dpkg::Gettext;
  19. use Dpkg::ErrorHandling qw(error syserr warning usageerr subprocerr info);
  20. use Dpkg::Compression;
  21. use Dpkg::Source::Archive;
  22. use Dpkg::Source::Patch;
  23. use Dpkg::Version qw(check_version);
  24. use Dpkg::Exit;
  25. use Dpkg::Source::Functions qw(erasedir);
  26. use POSIX;
  27. use File::Basename;
  28. use File::Temp qw(tempfile tempdir);
  29. use File::Path;
  30. use File::Spec;
  31. sub do_extract {
  32. my ($self, $newdirectory) = @_;
  33. my $fields = $self->{'fields'};
  34. my $dscdir = $self->{'basedir'};
  35. check_version($fields->{'Version'});
  36. my $basename = $self->get_basename();
  37. my $basenamerev = $self->get_basename(1);
  38. my ($tarfile, $debianfile, %origtar, %seen);
  39. foreach my $file ($self->get_files()) {
  40. (my $uncompressed = $file) =~ s/\.$comp_regex$//;
  41. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  42. $uncompressed) if $seen{$uncompressed};
  43. $seen{$uncompressed} = 1;
  44. if ($file =~ /^\Q$basename\E\.orig\.tar\.$comp_regex$/) {
  45. $tarfile = $file;
  46. } elsif ($file =~ /^\Q$basename\E\.orig-(\w+)\.tar\.$comp_regex$/) {
  47. $origtar{$1} = $file;
  48. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$comp_regex$/) {
  49. $debianfile = $file;
  50. } else {
  51. error(_g("unrecognized file for a %s source package: %s"),
  52. "v2.0", $file);
  53. }
  54. }
  55. unless ($tarfile and $debianfile) {
  56. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  57. }
  58. erasedir($newdirectory);
  59. # Extract main tarball
  60. info(_g("unpacking %s"), $tarfile);
  61. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  62. $tar->extract($newdirectory);
  63. # Extract additional orig tarballs
  64. foreach my $subdir (keys %origtar) {
  65. my $file = $origtar{$subdir};
  66. info(_g("unpacking %s"), $file);
  67. if (-e "$newdirectory/$subdir") {
  68. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  69. erasedir("$newdirectory/$subdir");
  70. }
  71. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  72. $tar->extract("$newdirectory/$subdir");
  73. }
  74. # Extract debian tarball after removing the debian directory
  75. info(_g("unpacking %s"), $debianfile);
  76. erasedir("$newdirectory/debian");
  77. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  78. $tar->extract("$newdirectory/debian");
  79. # Apply patches (in a separate method as it might be overriden)
  80. $self->apply_patches($newdirectory);
  81. }
  82. sub get_autopatch_name {
  83. return "zz_debian-diff-auto";
  84. }
  85. sub get_patches {
  86. my ($self, $dir, $skip_auto) = @_;
  87. my @patches;
  88. my $pd = "$dir/debian/patches";
  89. my $auto_patch = $self->get_autopatch_name();
  90. if (-d $pd) {
  91. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  92. foreach my $patch (sort readdir(DIR)) {
  93. # patches match same rules as run-parts
  94. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  95. next if $skip_auto and $patch eq $auto_patch;
  96. push @patches, $patch;
  97. }
  98. closedir(DIR);
  99. }
  100. return @patches;
  101. }
  102. sub apply_patches {
  103. my ($self, $dir, $skip_auto) = @_;
  104. my $timestamp = time();
  105. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  106. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  107. info(_g("applying %s"), $patch) unless $skip_auto;
  108. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  109. $patch_obj->apply($dir, force_timestamp => 1,
  110. timestamp => $timestamp);
  111. }
  112. }
  113. sub can_build {
  114. my ($self, $dir) = @_;
  115. foreach ($self->find_original_tarballs()) {
  116. return 1 if /\.orig\.tar\.$comp_regex$/;
  117. }
  118. return (0, _g("no orig.tar file found"));
  119. }
  120. sub prepare_build {
  121. my ($self, $dir) = @_;
  122. $self->{'diff_options'} = {
  123. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'}
  124. };
  125. }
  126. sub do_build {
  127. my ($self, $dir) = @_;
  128. my @argv = @{$self->{'options'}{'ARGV'}};
  129. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  130. my ($dirname, $updir) = fileparse($dir);
  131. if (scalar(@argv)) {
  132. usageerr(_g("-b takes only one parameter with v2.0 source packages"));
  133. }
  134. $self->prepare_build($dir);
  135. my $sourcepackage = $self->{'fields'}{'Source'};
  136. my $basenamerev = $self->get_basename(1);
  137. my $basename = $self->get_basename();
  138. my $basedirname = $basename;
  139. $basedirname =~ s/_/-/;
  140. # Identify original tarballs
  141. my ($tarfile, %origtar);
  142. my @origtarballs;
  143. foreach (sort $self->find_original_tarballs()) {
  144. if (/\.orig\.tar\.$comp_regex$/) {
  145. $tarfile = $_;
  146. push @origtarballs, $_;
  147. $self->add_file($_);
  148. } elsif (/\.orig-(\w+)\.tar\.$comp_regex$/) {
  149. $origtar{$1} = $_;
  150. push @origtarballs, $_;
  151. $self->add_file($_);
  152. }
  153. }
  154. error(_g("no orig.tar file found")) unless $tarfile;
  155. info(_g("building %s using existing %s"),
  156. $sourcepackage, "@origtarballs");
  157. # Unpack a second copy for comparison
  158. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  159. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  160. # Extract main tarball
  161. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  162. $tar->extract($tmp);
  163. # Extract additional orig tarballs
  164. foreach my $subdir (keys %origtar) {
  165. my $file = $origtar{$subdir};
  166. $tar = Dpkg::Source::Archive->new(filename => $file);
  167. $tar->extract("$tmp/$subdir");
  168. }
  169. # Copy over the debian directory
  170. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  171. subprocerr(_g("copy of the debian directory")) if $?;
  172. # Apply all patches except the last automatic one
  173. $self->apply_patches($tmp, 1);
  174. # Create a patch
  175. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  176. DIR => $updir, UNLINK => 0);
  177. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  178. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  179. compression => "none");
  180. $diff->create();
  181. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  182. %{$self->{'diff_options'}});
  183. error(_g("unrepresentable changes to source")) if not $diff->finish();
  184. # The previous auto-patch must be removed, it has not been used and it
  185. # will be recreated if it's still needed
  186. my $autopatch = "$dir/debian/patches/" . $self->get_autopatch_name();
  187. if (-e $autopatch) {
  188. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  189. }
  190. # Install the diff as the new autopatch
  191. if (not -s $tmpdiff) {
  192. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  193. } else {
  194. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  195. rename($tmpdiff, $autopatch) ||
  196. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  197. }
  198. $self->register_autopatch($dir);
  199. pop @Dpkg::Exit::handlers;
  200. # Remove the temporary directory
  201. erasedir($tmp);
  202. pop @Dpkg::Exit::handlers;
  203. # Create the debian.tar
  204. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  205. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  206. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  207. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  208. $tar->add_directory("debian");
  209. $tar->finish();
  210. $self->add_file($debianfile);
  211. }
  212. sub register_autopatch {
  213. my ($self, $dir) = @_;
  214. }
  215. # vim:et:sw=4:ts=8
  216. 1;