V2_0.pm 9.1 KB

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