V2_0.pm 9.1 KB

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