V2_0.pm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. sub do_extract {
  30. my ($self, $newdirectory) = @_;
  31. my $fields = $self->{'fields'};
  32. my $dscdir = $self->{'basedir'};
  33. check_version($fields->{'Version'});
  34. my $basename = $self->get_basename();
  35. my $basenamerev = $self->get_basename(1);
  36. my ($tarfile, $debianfile, %origtar, %seen);
  37. foreach my $file ($self->get_files()) {
  38. (my $uncompressed = $file) =~ s/\.$comp_regex$//;
  39. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  40. $uncompressed) if $seen{$uncompressed};
  41. $seen{$uncompressed} = 1;
  42. if ($file =~ /^\Q$basename\E\.orig\.tar\.$comp_regex$/) {
  43. $tarfile = $file;
  44. } elsif ($file =~ /^\Q$basename\E\.orig-(\w+)\.tar\.$comp_regex$/) {
  45. $origtar{$1} = $file;
  46. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$comp_regex$/) {
  47. $debianfile = $file;
  48. } else {
  49. error(_g("unrecognized file for a %s source package: %s"),
  50. "v2.0", $file);
  51. }
  52. }
  53. unless ($tarfile and $debianfile) {
  54. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  55. }
  56. erasedir($newdirectory);
  57. # Extract main tarball
  58. info(_g("unpacking %s"), $tarfile);
  59. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  60. $tar->extract($newdirectory);
  61. # Extract additional orig tarballs
  62. foreach my $subdir (keys %origtar) {
  63. my $file = $origtar{$subdir};
  64. info(_g("unpacking %s"), $file);
  65. if (-e "$newdirectory/$subdir") {
  66. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  67. erasedir("$newdirectory/$subdir");
  68. }
  69. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  70. $tar->extract("$newdirectory/$subdir");
  71. }
  72. # Extract debian tarball after removing the debian directory
  73. info(_g("%s: unpacking %s"), $debianfile);
  74. erasedir("$newdirectory/debian");
  75. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  76. $tar->extract("$newdirectory/debian");
  77. # Apply patches (in a separate method as it might be overriden)
  78. $self->apply_patches($newdirectory);
  79. }
  80. sub get_autopatch_name {
  81. return "zz_debian-diff-auto";
  82. }
  83. sub get_patches {
  84. my ($self, $dir, $skip_auto) = @_;
  85. my @patches;
  86. my $pd = "$dir/debian/patches";
  87. my $auto_patch = $self->get_autopatch_name();
  88. if (-d $pd) {
  89. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  90. foreach my $patch (sort readdir(DIR)) {
  91. # patches match same rules as run-parts
  92. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  93. next if $skip_auto and $patch eq $auto_patch;
  94. push @patches, "$pd/$patch";
  95. }
  96. closedir(DIR);
  97. }
  98. return @patches;
  99. }
  100. sub apply_patches {
  101. my ($self, $dir, $skip_auto) = @_;
  102. my $timestamp = time();
  103. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  104. info(_g("applying %s"), basename($patch)) unless $skip_auto;
  105. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  106. $patch_obj->apply($dir, force_timestamp => 1,
  107. timestamp => $timestamp);
  108. }
  109. }
  110. sub can_build {
  111. my ($self, $dir) = @_;
  112. foreach ($self->find_original_tarballs()) {
  113. return 1 if /\.orig\.tar\.$comp_regex$/;
  114. }
  115. return (0, _g("no orig.tar file found"));
  116. }
  117. sub do_build {
  118. my ($self, $dir) = @_;
  119. my @argv = @{$self->{'options'}{'ARGV'}};
  120. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  121. my $diff_ignore_regexp = $self->{'options'}{'diff_ignore_regexp'};
  122. $dir =~ s{/+$}{}; # Strip trailing /
  123. my ($dirname, $updir) = fileparse($dir);
  124. if (scalar(@argv)) {
  125. usageerr(_g("-b takes only one parameter with v2.0 source packages"));
  126. }
  127. my $sourcepackage = $self->{'fields'}{'Source'};
  128. my $basenamerev = $self->get_basename(1);
  129. my $basename = $self->get_basename();
  130. my $basedirname = $basename;
  131. $basedirname =~ s/_/-/;
  132. # Identify original tarballs
  133. my ($tarfile, %origtar);
  134. my @origtarballs;
  135. foreach (sort $self->find_original_tarballs()) {
  136. if (/\.orig\.tar\.$comp_regex$/) {
  137. $tarfile = $_;
  138. push @origtarballs, $_;
  139. $self->add_file($_);
  140. } elsif (/\.orig-(\w+)\.tar\.$comp_regex$/) {
  141. $origtar{$1} = $_;
  142. push @origtarballs, $_;
  143. $self->add_file($_);
  144. }
  145. }
  146. error(_g("no orig.tar file found")) unless $tarfile;
  147. info(_g("building %s using existing %s"),
  148. $sourcepackage, "@origtarballs");
  149. # Unpack a second copy for comparison
  150. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  151. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  152. # Extract main tarball
  153. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  154. $tar->extract($tmp);
  155. # Extract additional orig tarballs
  156. foreach my $subdir (keys %origtar) {
  157. my $file = $origtar{$subdir};
  158. $tar = Dpkg::Source::Archive->new(filename => $file);
  159. $tar->extract("$tmp/$subdir");
  160. }
  161. # Copy over the debian directory
  162. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  163. subprocerr(_g("copy of the debian directory")) if $?;
  164. # Apply all patches except the last automatic one
  165. $self->apply_patches($tmp, 1);
  166. # Create a patch
  167. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  168. DIR => $updir, UNLINK => 0);
  169. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  170. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  171. compression => "none");
  172. $diff->create();
  173. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  174. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'});
  175. error(_g("unrepresentable changes to source")) if not $diff->finish();
  176. # The previous auto-patch must be removed, it has not been used and it
  177. # will be recreated if it's still needed
  178. my $autopatch = "$dir/debian/patches/" . $self->get_autopatch_name();
  179. if (-e $autopatch) {
  180. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  181. }
  182. # Install the diff as the new autopatch
  183. if (not -s $tmpdiff) {
  184. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  185. } else {
  186. rename($tmpdiff, $autopatch) ||
  187. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  188. }
  189. $self->register_autopatch($dir);
  190. pop @Dpkg::Exit::handlers;
  191. # Remove the temporary directory
  192. erasedir($tmp);
  193. pop @Dpkg::Exit::handlers;
  194. # Create the debian.tar
  195. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  196. info(_g("building %s in %s"),
  197. $sourcepackage, $debianfile);
  198. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  199. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  200. $tar->add_directory("debian");
  201. $tar->finish();
  202. $self->add_file($debianfile);
  203. }
  204. sub register_autopatch {
  205. my ($self, $dir) = @_;
  206. }
  207. # vim:et:sw=4:ts=8
  208. 1;