V2.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 before_build {
  193. my ($self, $dir) = @_;
  194. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  195. }
  196. sub prepare_build {
  197. my ($self, $dir) = @_;
  198. $self->{'diff_options'} = {
  199. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'},
  200. include_removal => $self->{'options'}{'include_removal'},
  201. include_timestamp => $self->{'options'}{'include_timestamp'},
  202. use_dev_null => 1,
  203. };
  204. push @{$self->{'options'}{'tar_ignore'}}, "debian/patches/.dpkg-source-applied";
  205. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  206. if ($self->{'options'}{'create_empty_orig'} and
  207. not $self->find_original_tarballs(include_supplementary => 0))
  208. {
  209. # No main orig.tar, create a dummy one
  210. my $filename = $self->get_basename() . ".orig.tar." .
  211. $self->{'options'}{'comp_ext'};
  212. my $tar = Dpkg::Source::Archive->new(filename => $filename);
  213. $tar->create();
  214. $tar->finish();
  215. }
  216. }
  217. sub check_patches_applied {
  218. my ($self, $dir) = @_;
  219. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  220. unless (-e $applied) {
  221. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  222. $self->apply_patches($dir, usage => 'preparation');
  223. }
  224. }
  225. sub do_build {
  226. my ($self, $dir) = @_;
  227. my ($dirname, $updir) = fileparse($dir);
  228. my @argv = @{$self->{'options'}{'ARGV'}};
  229. if (scalar(@argv)) {
  230. usageerr(_g("-b takes only one parameter with format `%s'"),
  231. $self->{'fields'}{'Format'});
  232. }
  233. $self->prepare_build($dir);
  234. my $include_binaries = $self->{'options'}{'include_binaries'};
  235. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  236. my $sourcepackage = $self->{'fields'}{'Source'};
  237. my $basenamerev = $self->get_basename(1);
  238. my $basename = $self->get_basename();
  239. my $basedirname = $basename;
  240. $basedirname =~ s/_/-/;
  241. # Identify original tarballs
  242. my ($tarfile, %origtar);
  243. my @origtarballs;
  244. foreach (sort $self->find_original_tarballs()) {
  245. if (/\.orig\.tar\.$compression_re_file_ext$/) {
  246. if (defined($tarfile)) {
  247. error(_g("several orig.tar files found (%s and %s) but only " .
  248. "one is allowed"), $tarfile, $_);
  249. }
  250. $tarfile = $_;
  251. push @origtarballs, $_;
  252. $self->add_file($_);
  253. } elsif (/\.orig-([[:alnum:]-]+)\.tar\.$compression_re_file_ext$/) {
  254. $origtar{$1} = $_;
  255. push @origtarballs, $_;
  256. $self->add_file($_);
  257. }
  258. }
  259. error(_g("no orig.tar file found")) unless $tarfile;
  260. info(_g("building %s using existing %s"),
  261. $sourcepackage, "@origtarballs");
  262. # Unpack a second copy for comparison
  263. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  264. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  265. # Extract main tarball
  266. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  267. $tar->extract($tmp);
  268. # Extract additional orig tarballs
  269. foreach my $subdir (keys %origtar) {
  270. my $file = $origtar{$subdir};
  271. $tar = Dpkg::Source::Archive->new(filename => $file);
  272. $tar->extract("$tmp/$subdir");
  273. }
  274. # Copy over the debian directory
  275. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  276. subprocerr(_g("copy of the debian directory")) if $?;
  277. # Apply all patches except the last automatic one
  278. $self->apply_patches($tmp, skip_auto => 1, usage => 'build');
  279. # Prepare handling of binary files
  280. my %auth_bin_files;
  281. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  282. if (-f $incbin_file) {
  283. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  284. while(defined($_ = <INC>)) {
  285. chomp; s/^\s*//; s/\s*$//;
  286. next if /^#/ or /^$/;
  287. $auth_bin_files{$_} = 1;
  288. }
  289. close(INC);
  290. }
  291. my @binary_files;
  292. my $handle_binary = sub {
  293. my ($self, $old, $new) = @_;
  294. my $relfn = File::Spec->abs2rel($new, $dir);
  295. # Include binaries if they are whitelisted or if
  296. # --include-binaries has been given
  297. if ($include_binaries or $auth_bin_files{$relfn}) {
  298. push @binary_files, $relfn;
  299. } else {
  300. errormsg(_g("cannot represent change to %s: %s"), $new,
  301. _g("binary file contents changed"));
  302. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  303. " to store the modified binary in the debian tarball"),
  304. $relfn);
  305. $self->register_error();
  306. }
  307. };
  308. # Check if the debian directory contains unwanted binary files
  309. my $unwanted_binaries = 0;
  310. my $check_binary = sub {
  311. my $fn = File::Spec->abs2rel($_, $dir);
  312. if (-f $_ and is_binary($_)) {
  313. if ($include_binaries or $auth_bin_files{$fn}) {
  314. push @binary_files, $fn;
  315. } else {
  316. errormsg(_g("unwanted binary file: %s"), $fn);
  317. $unwanted_binaries++;
  318. }
  319. }
  320. };
  321. my $tar_ignore_glob = "{" . join(",",
  322. map {
  323. my $copy = $_;
  324. $copy =~ s/,/\\,/g;
  325. $copy;
  326. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  327. my $filter_ignore = sub {
  328. # Filter out files that are not going to be included in the debian
  329. # tarball due to ignores.
  330. my %exclude;
  331. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  332. foreach my $fn (glob($tar_ignore_glob)) {
  333. $exclude{$fn} = 1;
  334. }
  335. my @result;
  336. foreach my $fn (@_) {
  337. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  338. push @result, $fn;
  339. }
  340. }
  341. return @result;
  342. };
  343. find({ wanted => $check_binary, preprocess => $filter_ignore,
  344. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  345. error(_g("detected %d unwanted binary file(s) " .
  346. "(add them in debian/source/include-binaries to allow their " .
  347. "inclusion)."), $unwanted_binaries) if $unwanted_binaries;
  348. # Create a patch
  349. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  350. $self->get_autopatch_name());
  351. my ($difffh, $tmpdiff) = tempfile("$basenamerev.diff.XXXXXX",
  352. DIR => $updir, UNLINK => 0);
  353. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  354. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  355. compression => "none");
  356. $diff->create();
  357. $diff->set_header($self->get_patch_header($dir, $autopatch));
  358. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  359. %{$self->{'diff_options'}}, handle_binary_func => $handle_binary);
  360. error(_g("unrepresentable changes to source")) if not $diff->finish();
  361. # The previous auto-patch must be removed, it has not been used and it
  362. # will be recreated if it's still needed
  363. if (-e $autopatch) {
  364. unlink($autopatch) || syserr(_g("cannot remove %s"), $autopatch);
  365. }
  366. # Install the diff as the new autopatch
  367. if (not -s $tmpdiff) {
  368. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  369. } else {
  370. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  371. info(_g("local changes stored in %s, the modified files are:"), $autopatch);
  372. my $analysis = $diff->analyze($dir);
  373. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  374. print " $fn\n";
  375. }
  376. rename($tmpdiff, $autopatch) ||
  377. syserr(_g("cannot rename %s to %s"), $tmpdiff, $autopatch);
  378. chmod(0666 & ~ umask(), $autopatch) ||
  379. syserr(_g("unable to change permission of `%s'"), $autopatch);
  380. }
  381. $self->register_autopatch($dir);
  382. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  383. pop @Dpkg::Exit::handlers;
  384. # Remove the temporary directory
  385. erasedir($tmp);
  386. pop @Dpkg::Exit::handlers;
  387. # Update debian/source/include-binaries if needed
  388. if (scalar(@binary_files) and $include_binaries) {
  389. mkpath(File::Spec->catdir($dir, "debian", "source"));
  390. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  391. foreach my $binary (@binary_files) {
  392. unless ($auth_bin_files{$binary}) {
  393. print INC "$binary\n";
  394. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  395. }
  396. }
  397. close(INC);
  398. }
  399. # Create the debian.tar
  400. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  401. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  402. $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  403. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  404. $tar->add_directory("debian");
  405. foreach my $binary (@binary_files) {
  406. $tar->add_file($binary) unless $binary =~ m{^debian/};
  407. }
  408. $tar->finish();
  409. $self->add_file($debianfile);
  410. }
  411. sub get_patch_header {
  412. my ($self, $dir, $previous) = @_;
  413. my $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  414. my $text;
  415. if (-f $ph) {
  416. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  417. $text = join("", <PH>);
  418. close(PH);
  419. return $text;
  420. }
  421. return "Description: Undocumented upstream changes
  422. This patch has been created by dpkg-source during the package build
  423. but it might have accumulated changes from several uploads. Please
  424. check the changelog to (hopefully) learn more on those changes.\n\n";
  425. }
  426. sub register_autopatch {
  427. my ($self, $dir) = @_;
  428. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  429. $self->get_autopatch_name());
  430. if (-e $autopatch) {
  431. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  432. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  433. print APPLIED ($self->get_autopatch_name() . "\n");
  434. close(APPLIED);
  435. }
  436. }
  437. # vim:et:sw=4:ts=8
  438. 1;