V2_0.pm 13 KB

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