V2.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Package::V2;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "0.01";
  19. use base 'Dpkg::Source::Package';
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Compression;
  24. use Dpkg::Source::Archive;
  25. use Dpkg::Source::Patch;
  26. use Dpkg::Exit;
  27. use Dpkg::Source::Functions qw(erasedir is_binary);
  28. use POSIX;
  29. use File::Basename;
  30. use File::Temp qw(tempfile tempdir);
  31. use File::Path;
  32. use File::Spec;
  33. use File::Find;
  34. our $CURRENT_MINOR_VERSION = "0";
  35. sub init_options {
  36. my ($self) = @_;
  37. $self->SUPER::init_options();
  38. $self->{'options'}{'include_removal'} = 0
  39. unless exists $self->{'options'}{'include_removal'};
  40. $self->{'options'}{'include_timestamp'} = 0
  41. unless exists $self->{'options'}{'include_timestamp'};
  42. $self->{'options'}{'include_binaries'} = 0
  43. unless exists $self->{'options'}{'include_binaries'};
  44. $self->{'options'}{'preparation'} = 1
  45. unless exists $self->{'options'}{'preparation'};
  46. $self->{'options'}{'skip_patches'} = 0
  47. unless exists $self->{'options'}{'skip_patches'};
  48. $self->{'options'}{'skip_debianization'} = 0
  49. unless exists $self->{'options'}{'skip_debianization'};
  50. }
  51. sub parse_cmdline_option {
  52. my ($self, $opt) = @_;
  53. if ($opt =~ /^--include-removal$/) {
  54. $self->{'options'}{'include_removal'} = 1;
  55. return 1;
  56. } elsif ($opt =~ /^--include-timestamp$/) {
  57. $self->{'options'}{'include_timestamp'} = 1;
  58. return 1;
  59. } elsif ($opt =~ /^--include-binaries$/) {
  60. $self->{'options'}{'include_binaries'} = 1;
  61. return 1;
  62. } elsif ($opt =~ /^--no-preparation$/) {
  63. $self->{'options'}{'preparation'} = 0;
  64. return 1;
  65. } elsif ($opt =~ /^--skip-patches$/) {
  66. $self->{'options'}{'skip_patches'} = 1;
  67. return 1;
  68. } elsif ($opt =~ /^--skip-debianization$/) {
  69. $self->{'options'}{'skip_debianization'} = 1;
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. sub do_extract {
  75. my ($self, $newdirectory) = @_;
  76. my $fields = $self->{'fields'};
  77. my $dscdir = $self->{'basedir'};
  78. my $basename = $self->get_basename();
  79. my $basenamerev = $self->get_basename(1);
  80. my ($tarfile, $debianfile, %origtar, %seen);
  81. my $re_ext = $compression_re_file_ext;
  82. foreach my $file ($self->get_files()) {
  83. (my $uncompressed = $file) =~ s/\.$re_ext$//;
  84. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  85. $uncompressed) if $seen{$uncompressed};
  86. $seen{$uncompressed} = 1;
  87. if ($file =~ /^\Q$basename\E\.orig\.tar\.$re_ext$/) {
  88. $tarfile = $file;
  89. } elsif ($file =~ /^\Q$basename\E\.orig-([\w-]+)\.tar\.$re_ext$/) {
  90. $origtar{$1} = $file;
  91. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$re_ext$/) {
  92. $debianfile = $file;
  93. } else {
  94. error(_g("unrecognized file for a %s source package: %s"),
  95. "v2.0", $file);
  96. }
  97. }
  98. unless ($tarfile and $debianfile) {
  99. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  100. }
  101. erasedir($newdirectory);
  102. # Extract main tarball
  103. info(_g("unpacking %s"), $tarfile);
  104. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  105. $tar->extract($newdirectory, no_fixperms => 1);
  106. # Extract additional orig tarballs
  107. foreach my $subdir (keys %origtar) {
  108. my $file = $origtar{$subdir};
  109. info(_g("unpacking %s"), $file);
  110. if (-e "$newdirectory/$subdir") {
  111. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  112. erasedir("$newdirectory/$subdir");
  113. }
  114. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  115. $tar->extract("$newdirectory/$subdir", no_fixperms => 1);
  116. }
  117. # Stop here if debianization is not wanted
  118. return if $self->{'options'}{'skip_debianization'};
  119. # Extract debian tarball after removing the debian directory
  120. info(_g("unpacking %s"), $debianfile);
  121. erasedir("$newdirectory/debian");
  122. # Exclude existing symlinks from extraction of debian.tar.gz as we
  123. # don't want to overwrite something outside of $newdirectory due to a
  124. # symlink
  125. my @exclude_symlinks;
  126. my $wanted = sub {
  127. return if not -l $_;
  128. my $fn = File::Spec->abs2rel($_, $newdirectory);
  129. push @exclude_symlinks, "--exclude", $fn;
  130. };
  131. find({ wanted => $wanted, no_chdir => 1 }, $newdirectory);
  132. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  133. $tar->extract($newdirectory, in_place => 1,
  134. options => [ '--anchored', '--no-wildcards',
  135. @exclude_symlinks ]);
  136. # Apply patches (in a separate method as it might be overriden)
  137. $self->apply_patches($newdirectory, usage => 'unpack')
  138. unless $self->{'options'}{'skip_patches'};
  139. }
  140. sub get_autopatch_name {
  141. return "zz_debian-diff-auto";
  142. }
  143. sub get_patches {
  144. my ($self, $dir, %opts) = @_;
  145. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  146. my @patches;
  147. my $pd = "$dir/debian/patches";
  148. my $auto_patch = $self->get_autopatch_name();
  149. if (-d $pd) {
  150. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  151. foreach my $patch (sort readdir(DIR)) {
  152. # patches match same rules as run-parts
  153. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  154. next if $opts{"skip_auto"} and $patch eq $auto_patch;
  155. push @patches, $patch;
  156. }
  157. closedir(DIR);
  158. }
  159. return @patches;
  160. }
  161. sub apply_patches {
  162. my ($self, $dir, %opts) = @_;
  163. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  164. my @patches = $self->get_patches($dir, %opts);
  165. return unless scalar(@patches);
  166. my $timestamp = time();
  167. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  168. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  169. foreach my $patch ($self->get_patches($dir, %opts)) {
  170. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  171. info(_g("applying %s"), $patch) unless $opts{"skip_auto"};
  172. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  173. $patch_obj->apply($dir, force_timestamp => 1,
  174. timestamp => $timestamp,
  175. add_options => [ '-E' ]);
  176. print APPLIED "$patch\n";
  177. }
  178. close(APPLIED);
  179. }
  180. sub can_build {
  181. my ($self, $dir) = @_;
  182. foreach ($self->find_original_tarballs()) {
  183. return 1 if /\.orig\.tar\.$compression_re_file_ext$/;
  184. }
  185. return (0, _g("no orig.tar file found"));
  186. }
  187. sub prepare_build {
  188. my ($self, $dir) = @_;
  189. $self->{'diff_options'} = {
  190. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'},
  191. include_removal => $self->{'options'}{'include_removal'},
  192. include_timestamp => $self->{'options'}{'include_timestamp'},
  193. use_dev_null => 1,
  194. };
  195. push @{$self->{'options'}{'tar_ignore'}}, "debian/patches/.dpkg-source-applied";
  196. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  197. }
  198. sub check_patches_applied {
  199. my ($self, $dir) = @_;
  200. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  201. unless (-e $applied) {
  202. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  203. $self->apply_patches($dir, usage => 'preparation');
  204. }
  205. }
  206. sub do_build {
  207. my ($self, $dir) = @_;
  208. my ($dirname, $updir) = fileparse($dir);
  209. my @argv = @{$self->{'options'}{'ARGV'}};
  210. if (scalar(@argv)) {
  211. usageerr(_g("-b takes only one parameter with format `%s'"),
  212. $self->{'fields'}{'Format'});
  213. }
  214. $self->prepare_build($dir);
  215. my $include_binaries = $self->{'options'}{'include_binaries'};
  216. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  217. my $sourcepackage = $self->{'fields'}{'Source'};
  218. my $basenamerev = $self->get_basename(1);
  219. my $basename = $self->get_basename();
  220. my $basedirname = $basename;
  221. $basedirname =~ s/_/-/;
  222. # Identify original tarballs
  223. my ($tarfile, %origtar);
  224. my @origtarballs;
  225. foreach (sort $self->find_original_tarballs()) {
  226. if (/\.orig\.tar\.$compression_re_file_ext$/) {
  227. if (defined($tarfile)) {
  228. error(_g("several orig.tar files found (%s and %s) but only " .
  229. "one is allowed"), $tarfile, $_);
  230. }
  231. $tarfile = $_;
  232. push @origtarballs, $_;
  233. $self->add_file($_);
  234. } elsif (/\.orig-([\w-]+)\.tar\.$compression_re_file_ext$/) {
  235. $origtar{$1} = $_;
  236. push @origtarballs, $_;
  237. $self->add_file($_);
  238. }
  239. }
  240. error(_g("no orig.tar file found")) unless $tarfile;
  241. info(_g("building %s using existing %s"),
  242. $sourcepackage, "@origtarballs");
  243. # Unpack a second copy for comparison
  244. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  245. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  246. # Extract main tarball
  247. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  248. $tar->extract($tmp);
  249. # Extract additional orig tarballs
  250. foreach my $subdir (keys %origtar) {
  251. my $file = $origtar{$subdir};
  252. $tar = Dpkg::Source::Archive->new(filename => $file);
  253. $tar->extract("$tmp/$subdir");
  254. }
  255. # Copy over the debian directory
  256. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  257. subprocerr(_g("copy of the debian directory")) if $?;
  258. # Apply all patches except the last automatic one
  259. $self->apply_patches($tmp, skip_auto => 1, usage => 'build');
  260. # Prepare handling of binary files
  261. my %auth_bin_files;
  262. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  263. if (-f $incbin_file) {
  264. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  265. while(defined($_ = <INC>)) {
  266. chomp; s/^\s*//; s/\s*$//;
  267. next if /^#/ or /^$/;
  268. $auth_bin_files{$_} = 1;
  269. }
  270. close(INC);
  271. }
  272. my @binary_files;
  273. my $handle_binary = sub {
  274. my ($self, $old, $new) = @_;
  275. my $relfn = File::Spec->abs2rel($new, $dir);
  276. # Include binaries if they are whitelisted or if
  277. # --include-binaries has been given
  278. if ($include_binaries or $auth_bin_files{$relfn}) {
  279. push @binary_files, $relfn;
  280. } else {
  281. errormsg(_g("cannot represent change to %s: %s"), $new,
  282. _g("binary file contents changed"));
  283. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  284. " to store the modified binary in the debian tarball"),
  285. $relfn);
  286. $self->register_error();
  287. }
  288. };
  289. # Check if the debian directory contains unwanted binary files
  290. my $unwanted_binaries = 0;
  291. my $check_binary = sub {
  292. my $fn = File::Spec->abs2rel($_, $dir);
  293. if (-f $_ and is_binary($_)) {
  294. if ($include_binaries or $auth_bin_files{$fn}) {
  295. push @binary_files, $fn;
  296. } else {
  297. errormsg(_g("unwanted binary file: %s"), $fn);
  298. $unwanted_binaries++;
  299. }
  300. }
  301. };
  302. my $tar_ignore_glob = "{" . join(",",
  303. map {
  304. my $copy = $_;
  305. $copy =~ s/,/\\,/g;
  306. $copy;
  307. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  308. my $filter_ignore = sub {
  309. # Filter out files that are not going to be included in the debian
  310. # tarball due to ignores.
  311. my %exclude;
  312. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  313. foreach my $fn (glob($tar_ignore_glob)) {
  314. $exclude{$fn} = 1;
  315. }
  316. my @result;
  317. foreach my $fn (@_) {
  318. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  319. push @result, $fn;
  320. }
  321. }
  322. return @result;
  323. };
  324. find({ wanted => $check_binary, preprocess => $filter_ignore,
  325. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  326. error(_g("detected %d unwanted binary file(s) " .
  327. "(add them in debian/source/include-binaries to allow their " .
  328. "inclusion)."), $unwanted_binaries) if $unwanted_binaries;
  329. # Create a patch
  330. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  331. $self->get_autopatch_name());
  332. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  333. DIR => $updir, UNLINK => 0);
  334. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  335. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  336. compression => "none");
  337. $diff->create();
  338. $diff->set_header($self->get_patch_header($dir, $autopatch));
  339. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  340. %{$self->{'diff_options'}}, handle_binary_func => $handle_binary);
  341. error(_g("unrepresentable changes to source")) if not $diff->finish();
  342. # The previous auto-patch must be removed, it has not been used and it
  343. # will be recreated if it's still needed
  344. if (-e $autopatch) {
  345. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  346. }
  347. # Install the diff as the new autopatch
  348. if (not -s $tmpdiff) {
  349. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  350. } else {
  351. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  352. info(_g("local changes stored in %s, the modified files are:"), $autopatch);
  353. my $analysis = $diff->analyze($dir);
  354. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  355. print " $fn\n";
  356. }
  357. rename($tmpdiff, $autopatch) ||
  358. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  359. chmod(0666 & ~ umask(), $autopatch) ||
  360. syserr(_g("unable to change permission of `%s'"), $autopatch);
  361. }
  362. $self->register_autopatch($dir);
  363. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  364. pop @Dpkg::Exit::handlers;
  365. # Remove the temporary directory
  366. erasedir($tmp);
  367. pop @Dpkg::Exit::handlers;
  368. # Update debian/source/include-binaries if needed
  369. if (scalar(@binary_files) and $include_binaries) {
  370. mkpath(File::Spec->catdir($dir, "debian", "source"));
  371. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  372. foreach my $binary (@binary_files) {
  373. unless ($auth_bin_files{$binary}) {
  374. print INC "$binary\n";
  375. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  376. }
  377. }
  378. close(INC);
  379. }
  380. # Create the debian.tar
  381. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  382. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  383. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  384. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  385. $tar->add_directory("debian");
  386. foreach my $binary (@binary_files) {
  387. $tar->add_file($binary) unless $binary =~ m{^debian/};
  388. }
  389. $tar->finish();
  390. $self->add_file($debianfile);
  391. }
  392. sub get_patch_header {
  393. my ($self, $dir, $previous) = @_;
  394. my $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  395. my $text;
  396. if (-f $ph) {
  397. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  398. $text = join("", <PH>);
  399. close(PH);
  400. return $text;
  401. }
  402. return "Description: Undocumented upstream changes
  403. This patch has been created by dpkg-source during the package build
  404. but it might have accumulated changes from several uploads. Please
  405. check the changelog to (hopefully) learn more on those changes.\n\n";
  406. }
  407. sub register_autopatch {
  408. my ($self, $dir) = @_;
  409. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  410. $self->get_autopatch_name());
  411. if (-e $autopatch) {
  412. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  413. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  414. print APPLIED ($self->get_autopatch_name() . "\n");
  415. close(APPLIED);
  416. }
  417. }
  418. # vim:et:sw=4:ts=8
  419. 1;