V2_0.pm 8.0 KB

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