V2.pm 19 KB

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