V2_0.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 errormsg 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. use File::Find;
  32. sub init_options {
  33. my ($self) = @_;
  34. $self->SUPER::init_options();
  35. $self->{'options'}{'include_removal'} = 0
  36. unless exists $self->{'options'}{'include_removal'};
  37. $self->{'options'}{'include_timestamp'} = 0
  38. unless exists $self->{'options'}{'include_timestamp'};
  39. $self->{'options'}{'include_binaries'} = 0
  40. unless exists $self->{'options'}{'include_binaries'};
  41. }
  42. sub parse_cmdline_option {
  43. my ($self, $opt) = @_;
  44. if ($opt =~ /^--include-removal$/) {
  45. $self->{'options'}{'include_removal'} = 1;
  46. return 1;
  47. } elsif ($opt =~ /^--include-timestamp$/) {
  48. $self->{'options'}{'include_timestamp'} = 1;
  49. return 1;
  50. } elsif ($opt =~ /^--include-binaries$/) {
  51. $self->{'options'}{'include_binaries'} = 1;
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. sub do_extract {
  57. my ($self, $newdirectory) = @_;
  58. my $fields = $self->{'fields'};
  59. my $dscdir = $self->{'basedir'};
  60. check_version($fields->{'Version'});
  61. my $basename = $self->get_basename();
  62. my $basenamerev = $self->get_basename(1);
  63. my ($tarfile, $debianfile, %origtar, %seen);
  64. foreach my $file ($self->get_files()) {
  65. (my $uncompressed = $file) =~ s/\.$comp_regex$//;
  66. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  67. $uncompressed) if $seen{$uncompressed};
  68. $seen{$uncompressed} = 1;
  69. if ($file =~ /^\Q$basename\E\.orig\.tar\.$comp_regex$/) {
  70. $tarfile = $file;
  71. } elsif ($file =~ /^\Q$basename\E\.orig-(\w+)\.tar\.$comp_regex$/) {
  72. $origtar{$1} = $file;
  73. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$comp_regex$/) {
  74. $debianfile = $file;
  75. } else {
  76. error(_g("unrecognized file for a %s source package: %s"),
  77. "v2.0", $file);
  78. }
  79. }
  80. unless ($tarfile and $debianfile) {
  81. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  82. }
  83. erasedir($newdirectory);
  84. # Extract main tarball
  85. info(_g("unpacking %s"), $tarfile);
  86. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  87. $tar->extract($newdirectory, no_fixperms => 1);
  88. # Extract additional orig tarballs
  89. foreach my $subdir (keys %origtar) {
  90. my $file = $origtar{$subdir};
  91. info(_g("unpacking %s"), $file);
  92. if (-e "$newdirectory/$subdir") {
  93. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  94. erasedir("$newdirectory/$subdir");
  95. }
  96. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  97. $tar->extract("$newdirectory/$subdir", no_fixperms => 1);
  98. }
  99. # Extract debian tarball after removing the debian directory
  100. info(_g("unpacking %s"), $debianfile);
  101. erasedir("$newdirectory/debian");
  102. # Exclude existing symlinks from extraction of debian.tar.gz as we
  103. # don't want to overwrite something outside of $newdirectory due to a
  104. # symlink
  105. my @exclude_symlinks;
  106. my $wanted = sub {
  107. return if not -l $_;
  108. my $fn = File::Spec->abs2rel($_, $newdirectory);
  109. push @exclude_symlinks, "--exclude", $fn;
  110. };
  111. find({ wanted => $wanted, no_chdir => 1 }, $newdirectory);
  112. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  113. $tar->extract($newdirectory, in_place => 1,
  114. options => [ '--anchored', '--no-wildcards',
  115. @exclude_symlinks ]);
  116. # Apply patches (in a separate method as it might be overriden)
  117. $self->apply_patches($newdirectory);
  118. }
  119. sub get_autopatch_name {
  120. return "zz_debian-diff-auto";
  121. }
  122. sub get_patches {
  123. my ($self, $dir, $skip_auto) = @_;
  124. my @patches;
  125. my $pd = "$dir/debian/patches";
  126. my $auto_patch = $self->get_autopatch_name();
  127. if (-d $pd) {
  128. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  129. foreach my $patch (sort readdir(DIR)) {
  130. # patches match same rules as run-parts
  131. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  132. next if $skip_auto and $patch eq $auto_patch;
  133. push @patches, $patch;
  134. }
  135. closedir(DIR);
  136. }
  137. return @patches;
  138. }
  139. sub apply_patches {
  140. my ($self, $dir, $skip_auto) = @_;
  141. my $timestamp = time();
  142. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  143. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  144. info(_g("applying %s"), $patch) unless $skip_auto;
  145. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  146. $patch_obj->apply($dir, force_timestamp => 1,
  147. timestamp => $timestamp,
  148. add_options => [ '-E' ]);
  149. }
  150. }
  151. sub can_build {
  152. my ($self, $dir) = @_;
  153. foreach ($self->find_original_tarballs()) {
  154. return 1 if /\.orig\.tar\.$comp_regex$/;
  155. }
  156. return (0, _g("no orig.tar file found"));
  157. }
  158. sub prepare_build {
  159. my ($self, $dir) = @_;
  160. $self->{'diff_options'} = {
  161. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'},
  162. include_removal => $self->{'options'}{'include_removal'},
  163. include_timestamp => $self->{'options'}{'include_timestamp'},
  164. };
  165. }
  166. sub do_build {
  167. my ($self, $dir) = @_;
  168. my @argv = @{$self->{'options'}{'ARGV'}};
  169. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  170. my $include_binaries = $self->{'options'}{'include_binaries'};
  171. my ($dirname, $updir) = fileparse($dir);
  172. if (scalar(@argv)) {
  173. usageerr(_g("-b takes only one parameter with format `%s'"),
  174. $self->{'fields'}{'Format'});
  175. }
  176. $self->prepare_build($dir);
  177. my $sourcepackage = $self->{'fields'}{'Source'};
  178. my $basenamerev = $self->get_basename(1);
  179. my $basename = $self->get_basename();
  180. my $basedirname = $basename;
  181. $basedirname =~ s/_/-/;
  182. # Identify original tarballs
  183. my ($tarfile, %origtar);
  184. my @origtarballs;
  185. foreach (sort $self->find_original_tarballs()) {
  186. if (/\.orig\.tar\.$comp_regex$/) {
  187. $tarfile = $_;
  188. push @origtarballs, $_;
  189. $self->add_file($_);
  190. } elsif (/\.orig-(\w+)\.tar\.$comp_regex$/) {
  191. $origtar{$1} = $_;
  192. push @origtarballs, $_;
  193. $self->add_file($_);
  194. }
  195. }
  196. error(_g("no orig.tar file found")) unless $tarfile;
  197. info(_g("building %s using existing %s"),
  198. $sourcepackage, "@origtarballs");
  199. # Unpack a second copy for comparison
  200. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  201. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  202. # Extract main tarball
  203. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  204. $tar->extract($tmp);
  205. # Extract additional orig tarballs
  206. foreach my $subdir (keys %origtar) {
  207. my $file = $origtar{$subdir};
  208. $tar = Dpkg::Source::Archive->new(filename => $file);
  209. $tar->extract("$tmp/$subdir");
  210. }
  211. # Copy over the debian directory
  212. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  213. subprocerr(_g("copy of the debian directory")) if $?;
  214. # Apply all patches except the last automatic one
  215. $self->apply_patches($tmp, 1);
  216. # Prepare handling of binary files
  217. my %auth_bin_files;
  218. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  219. if (-f $incbin_file) {
  220. open(INC, "<", $incbin_file) || syserr(_g("can't read %s"), $incbin_file);
  221. while(defined($_ = <INC>)) {
  222. chomp; s/^\s*//; s/\s*$//;
  223. next if /^#/;
  224. $auth_bin_files{$_} = 1;
  225. }
  226. close(INC);
  227. }
  228. my @binary_files;
  229. my $handle_binary = sub {
  230. my ($self, $old, $new) = @_;
  231. my $relfn = File::Spec->abs2rel($new, $dir);
  232. # Include binaries if they are whitelisted or if
  233. # --include-binaries has been given
  234. if ($include_binaries or $auth_bin_files{$relfn}) {
  235. push @binary_files, $relfn;
  236. } else {
  237. errormsg(_g("cannot represent change to %s: %s"), $new,
  238. _g("binary file contents changed"));
  239. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  240. " to store the modified binary in the debian tarball"),
  241. $relfn);
  242. $self->register_error();
  243. }
  244. };
  245. # Create a patch
  246. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  247. DIR => $updir, UNLINK => 0);
  248. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  249. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  250. compression => "none");
  251. $diff->create();
  252. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  253. %{$self->{'diff_options'}}, handle_binary_func => $handle_binary);
  254. error(_g("unrepresentable changes to source")) if not $diff->finish();
  255. # The previous auto-patch must be removed, it has not been used and it
  256. # will be recreated if it's still needed
  257. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  258. $self->get_autopatch_name());
  259. if (-e $autopatch) {
  260. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  261. }
  262. # Install the diff as the new autopatch
  263. if (not -s $tmpdiff) {
  264. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  265. } else {
  266. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  267. rename($tmpdiff, $autopatch) ||
  268. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  269. }
  270. $self->register_autopatch($dir);
  271. pop @Dpkg::Exit::handlers;
  272. # Remove the temporary directory
  273. erasedir($tmp);
  274. pop @Dpkg::Exit::handlers;
  275. # Update debian/source/include-binaries if needed
  276. if (scalar(@binary_files) and $include_binaries) {
  277. mkpath(File::Spec->catdir($dir, "debian", "source"));
  278. open(INC, ">>", $incbin_file) || syserr(_g("can't write %s"), $incbin_file);
  279. foreach my $binary (@binary_files) {
  280. unless ($auth_bin_files{$binary}) {
  281. print INC "$binary\n";
  282. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  283. }
  284. }
  285. close(INC);
  286. }
  287. # Create the debian.tar
  288. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  289. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  290. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  291. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  292. $tar->add_directory("debian");
  293. foreach my $binary (@binary_files) {
  294. $tar->add_file($binary);
  295. }
  296. $tar->finish();
  297. $self->add_file($debianfile);
  298. }
  299. sub register_autopatch {
  300. my ($self, $dir) = @_;
  301. }
  302. # vim:et:sw=4:ts=8
  303. 1;