V2.pm 19 KB

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