V2.pm 18 KB

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