V2.pm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. # Copyright © 2008-2011 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 fs_time);
  28. use Dpkg::Vendor qw(run_vendor_hook);
  29. use Dpkg::Control;
  30. use Dpkg::Changelog::Parse;
  31. use POSIX;
  32. use File::Basename;
  33. use File::Temp qw(tempfile tempdir);
  34. use File::Path;
  35. use File::Spec;
  36. use File::Find;
  37. use File::Copy;
  38. our $CURRENT_MINOR_VERSION = "0";
  39. sub init_options {
  40. my ($self) = @_;
  41. $self->SUPER::init_options();
  42. $self->{'options'}{'include_removal'} = 0
  43. unless exists $self->{'options'}{'include_removal'};
  44. $self->{'options'}{'include_timestamp'} = 0
  45. unless exists $self->{'options'}{'include_timestamp'};
  46. $self->{'options'}{'include_binaries'} = 0
  47. unless exists $self->{'options'}{'include_binaries'};
  48. $self->{'options'}{'preparation'} = 1
  49. unless exists $self->{'options'}{'preparation'};
  50. $self->{'options'}{'skip_patches'} = 0
  51. unless exists $self->{'options'}{'skip_patches'};
  52. $self->{'options'}{'unapply_patches'} = 0
  53. unless exists $self->{'options'}{'unapply_patches'};
  54. $self->{'options'}{'skip_debianization'} = 0
  55. unless exists $self->{'options'}{'skip_debianization'};
  56. $self->{'options'}{'create_empty_orig'} = 0
  57. unless exists $self->{'options'}{'create_empty_orig'};
  58. $self->{'options'}{'abort_on_upstream_changes'} = 0
  59. unless exists $self->{'options'}{'abort_on_upstream_changes'};
  60. }
  61. sub parse_cmdline_option {
  62. my ($self, $opt) = @_;
  63. if ($opt =~ /^--include-removal$/) {
  64. $self->{'options'}{'include_removal'} = 1;
  65. return 1;
  66. } elsif ($opt =~ /^--include-timestamp$/) {
  67. $self->{'options'}{'include_timestamp'} = 1;
  68. return 1;
  69. } elsif ($opt =~ /^--include-binaries$/) {
  70. $self->{'options'}{'include_binaries'} = 1;
  71. return 1;
  72. } elsif ($opt =~ /^--no-preparation$/) {
  73. $self->{'options'}{'preparation'} = 0;
  74. return 1;
  75. } elsif ($opt =~ /^--skip-patches$/) {
  76. $self->{'options'}{'skip_patches'} = 1;
  77. return 1;
  78. } elsif ($opt =~ /^--unapply-patches$/) {
  79. $self->{'options'}{'unapply_patches'} = 1;
  80. return 1;
  81. } elsif ($opt =~ /^--skip-debianization$/) {
  82. $self->{'options'}{'skip_debianization'} = 1;
  83. return 1;
  84. } elsif ($opt =~ /^--create-empty-orig$/) {
  85. $self->{'options'}{'create_empty_orig'} = 1;
  86. return 1;
  87. } elsif ($opt =~ /^--abort-on-upstream-changes$/) {
  88. $self->{'options'}{'abort_on_upstream_changes'} = 1;
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. sub do_extract {
  94. my ($self, $newdirectory) = @_;
  95. my $fields = $self->{'fields'};
  96. my $dscdir = $self->{'basedir'};
  97. my $basename = $self->get_basename();
  98. my $basenamerev = $self->get_basename(1);
  99. my ($tarfile, $debianfile, %origtar, %seen);
  100. my $re_ext = $compression_re_file_ext;
  101. foreach my $file ($self->get_files()) {
  102. (my $uncompressed = $file) =~ s/\.$re_ext$//;
  103. error(_g("duplicate files in %s source package: %s.*"), "v2.0",
  104. $uncompressed) if $seen{$uncompressed};
  105. $seen{$uncompressed} = 1;
  106. if ($file =~ /^\Q$basename\E\.orig\.tar\.$re_ext$/) {
  107. $tarfile = $file;
  108. } elsif ($file =~ /^\Q$basename\E\.orig-([[:alnum:]-]+)\.tar\.$re_ext$/) {
  109. $origtar{$1} = $file;
  110. } elsif ($file =~ /^\Q$basenamerev\E\.debian\.tar\.$re_ext$/) {
  111. $debianfile = $file;
  112. } else {
  113. error(_g("unrecognized file for a %s source package: %s"),
  114. "v2.0", $file);
  115. }
  116. }
  117. unless ($tarfile and $debianfile) {
  118. error(_g("missing orig.tar or debian.tar file in v2.0 source package"));
  119. }
  120. erasedir($newdirectory);
  121. # Extract main tarball
  122. info(_g("unpacking %s"), $tarfile);
  123. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  124. $tar->extract($newdirectory, no_fixperms => 1,
  125. options => [ "--anchored", "--no-wildcards-match-slash",
  126. "--exclude", "*/.pc", "--exclude", ".pc" ]);
  127. # The .pc exclusion is only needed for 3.0 (quilt) and to avoid
  128. # having an upstream tarball provide a directory with symlinks
  129. # that would be blindly followed when applying the patches
  130. # Extract additional orig tarballs
  131. foreach my $subdir (keys %origtar) {
  132. my $file = $origtar{$subdir};
  133. info(_g("unpacking %s"), $file);
  134. if (-e "$newdirectory/$subdir") {
  135. warning(_g("required removal of `%s' installed by original tarball"), $subdir);
  136. erasedir("$newdirectory/$subdir");
  137. }
  138. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$file");
  139. $tar->extract("$newdirectory/$subdir", no_fixperms => 1);
  140. }
  141. # Stop here if debianization is not wanted
  142. return if $self->{'options'}{'skip_debianization'};
  143. # Extract debian tarball after removing the debian directory
  144. info(_g("unpacking %s"), $debianfile);
  145. erasedir("$newdirectory/debian");
  146. # Exclude existing symlinks from extraction of debian.tar.gz as we
  147. # don't want to overwrite something outside of $newdirectory due to a
  148. # symlink
  149. my @exclude_symlinks;
  150. my $wanted = sub {
  151. return if not -l $_;
  152. my $fn = File::Spec->abs2rel($_, $newdirectory);
  153. push @exclude_symlinks, "--exclude", $fn;
  154. };
  155. find({ wanted => $wanted, no_chdir => 1 }, $newdirectory);
  156. $tar = Dpkg::Source::Archive->new(filename => "$dscdir$debianfile");
  157. $tar->extract($newdirectory, in_place => 1,
  158. options => [ '--anchored', '--no-wildcards',
  159. @exclude_symlinks ]);
  160. # Apply patches (in a separate method as it might be overriden)
  161. $self->apply_patches($newdirectory, usage => 'unpack')
  162. unless $self->{'options'}{'skip_patches'};
  163. }
  164. sub get_autopatch_name {
  165. return "zz_debian-diff-auto";
  166. }
  167. sub get_patches {
  168. my ($self, $dir, %opts) = @_;
  169. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  170. my @patches;
  171. my $pd = "$dir/debian/patches";
  172. my $auto_patch = $self->get_autopatch_name();
  173. if (-d $pd) {
  174. opendir(DIR, $pd) || syserr(_g("cannot opendir %s"), $pd);
  175. foreach my $patch (sort readdir(DIR)) {
  176. # patches match same rules as run-parts
  177. next unless $patch =~ /^[\w-]+$/ and -f "$pd/$patch";
  178. next if $opts{"skip_auto"} and $patch eq $auto_patch;
  179. push @patches, $patch;
  180. }
  181. closedir(DIR);
  182. }
  183. return @patches;
  184. }
  185. sub apply_patches {
  186. my ($self, $dir, %opts) = @_;
  187. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  188. my @patches = $self->get_patches($dir, %opts);
  189. return unless scalar(@patches);
  190. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  191. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  192. print APPLIED "# During $opts{'usage'}\n";
  193. my $timestamp = fs_time($applied);
  194. foreach my $patch ($self->get_patches($dir, %opts)) {
  195. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  196. info(_g("applying %s"), $patch) unless $opts{"skip_auto"};
  197. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  198. $patch_obj->apply($dir, force_timestamp => 1,
  199. timestamp => $timestamp,
  200. add_options => [ '-E' ]);
  201. print APPLIED "$patch\n";
  202. }
  203. close(APPLIED);
  204. }
  205. sub unapply_patches {
  206. my ($self, $dir, %opts) = @_;
  207. my @patches = reverse($self->get_patches($dir, %opts));
  208. return unless scalar(@patches);
  209. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  210. my $timestamp = fs_time($applied);
  211. foreach my $patch (@patches) {
  212. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  213. info(_g("unapplying %s"), $patch) unless $opts{"quiet"};
  214. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  215. $patch_obj->apply($dir, force_timestamp => 1, verbose => 0,
  216. timestamp => $timestamp,
  217. add_options => [ '-E', '-R' ]);
  218. }
  219. unlink($applied);
  220. }
  221. sub upstream_tarball_template {
  222. my ($self) = @_;
  223. my $ext = "{" . join(",",
  224. sort map {
  225. compression_get_property($_, "file_ext")
  226. } compression_get_list()) . "}";
  227. return "../" . $self->get_basename() . ".orig.tar.$ext";
  228. }
  229. sub can_build {
  230. my ($self, $dir) = @_;
  231. return 1 if $self->find_original_tarballs(include_supplementary => 0);
  232. return 1 if $self->{'options'}{'create_empty_orig'} and
  233. $self->find_original_tarballs(include_main => 0);
  234. return (0, sprintf(_g("no upstream tarball found at %s"),
  235. $self->upstream_tarball_template()));
  236. }
  237. sub before_build {
  238. my ($self, $dir) = @_;
  239. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  240. }
  241. sub after_build {
  242. my ($self, $dir) = @_;
  243. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  244. my $reason = "";
  245. if (-e $applied) {
  246. open(APPLIED, "<", $applied) || syserr(_g("cannot read %s"), $applied);
  247. $reason = <APPLIED>;
  248. close(APPLIED);
  249. }
  250. if ($reason =~ /^# During preparation/ or
  251. $self->{'options'}{'unapply_patches'}) {
  252. $self->unapply_patches($dir);
  253. }
  254. }
  255. sub prepare_build {
  256. my ($self, $dir) = @_;
  257. $self->{'diff_options'} = {
  258. diff_ignore_regexp => $self->{'options'}{'diff_ignore_regexp'},
  259. include_removal => $self->{'options'}{'include_removal'},
  260. include_timestamp => $self->{'options'}{'include_timestamp'},
  261. use_dev_null => 1,
  262. };
  263. push @{$self->{'options'}{'tar_ignore'}}, "debian/patches/.dpkg-source-applied";
  264. $self->check_patches_applied($dir) if $self->{'options'}{'preparation'};
  265. if ($self->{'options'}{'create_empty_orig'} and
  266. not $self->find_original_tarballs(include_supplementary => 0))
  267. {
  268. # No main orig.tar, create a dummy one
  269. my $filename = $self->get_basename() . ".orig.tar." .
  270. $self->{'options'}{'comp_ext'};
  271. my $tar = Dpkg::Source::Archive->new(filename => $filename);
  272. $tar->create();
  273. $tar->finish();
  274. }
  275. }
  276. sub check_patches_applied {
  277. my ($self, $dir) = @_;
  278. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  279. unless (-e $applied) {
  280. info(_g("patches are not applied, applying them now"));
  281. $self->apply_patches($dir, usage => 'preparation');
  282. }
  283. }
  284. sub generate_patch {
  285. my ($self, $dir, %opts) = @_;
  286. my ($dirname, $updir) = fileparse($dir);
  287. my $basedirname = $self->get_basename();
  288. $basedirname =~ s/_/-/;
  289. # Identify original tarballs
  290. my ($tarfile, %origtar);
  291. my @origtarballs;
  292. foreach (sort $self->find_original_tarballs()) {
  293. if (/\.orig\.tar\.$compression_re_file_ext$/) {
  294. if (defined($tarfile)) {
  295. error(_g("several orig.tar files found (%s and %s) but only " .
  296. "one is allowed"), $tarfile, $_);
  297. }
  298. $tarfile = $_;
  299. push @origtarballs, $_;
  300. $self->add_file($_);
  301. } elsif (/\.orig-([[:alnum:]-]+)\.tar\.$compression_re_file_ext$/) {
  302. $origtar{$1} = $_;
  303. push @origtarballs, $_;
  304. $self->add_file($_);
  305. }
  306. }
  307. error(_g("no upstream tarball found at %s"),
  308. $self->upstream_tarball_template()) unless $tarfile;
  309. info(_g("building %s using existing %s"),
  310. $self->{'fields'}{'Source'}, "@origtarballs");
  311. # Unpack a second copy for comparison
  312. my $tmp = tempdir("$dirname.orig.XXXXXX", DIR => $updir);
  313. push @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  314. # Extract main tarball
  315. my $tar = Dpkg::Source::Archive->new(filename => $tarfile);
  316. $tar->extract($tmp);
  317. # Extract additional orig tarballs
  318. foreach my $subdir (keys %origtar) {
  319. my $file = $origtar{$subdir};
  320. $tar = Dpkg::Source::Archive->new(filename => $file);
  321. $tar->extract("$tmp/$subdir");
  322. }
  323. # Copy over the debian directory
  324. erasedir("$tmp/debian");
  325. system("cp", "-a", "--", "$dir/debian", "$tmp/");
  326. subprocerr(_g("copy of the debian directory")) if $?;
  327. # Apply all patches except the last automatic one
  328. $self->apply_patches($tmp, skip_auto => 1, usage => 'build');
  329. # Create a patch
  330. my ($difffh, $tmpdiff) = tempfile($self->get_basename(1) . ".diff.XXXXXX",
  331. DIR => $updir, UNLINK => 0);
  332. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  333. my $diff = Dpkg::Source::Patch->new(filename => $tmpdiff,
  334. compression => "none");
  335. $diff->create();
  336. $diff->set_header($self->get_patch_header($dir));
  337. $diff->add_diff_directory($tmp, $dir, basedirname => $basedirname,
  338. %{$self->{'diff_options'}},
  339. handle_binary_func => $opts{'handle_binary'},
  340. order_from => $opts{'order_from'});
  341. error(_g("unrepresentable changes to source")) if not $diff->finish();
  342. if (-s $tmpdiff) {
  343. info(_g("local changes detected, the modified files are:"));
  344. my $analysis = $diff->analyze($dir, verbose => 0);
  345. foreach my $fn (sort keys %{$analysis->{'filepatched'}}) {
  346. print " $fn\n";
  347. }
  348. }
  349. # Remove the temporary directory
  350. erasedir($tmp);
  351. pop @Dpkg::Exit::handlers;
  352. pop @Dpkg::Exit::handlers;
  353. return $tmpdiff;
  354. }
  355. sub do_build {
  356. my ($self, $dir) = @_;
  357. my @argv = @{$self->{'options'}{'ARGV'}};
  358. if (scalar(@argv)) {
  359. usageerr(_g("-b takes only one parameter with format `%s'"),
  360. $self->{'fields'}{'Format'});
  361. }
  362. $self->prepare_build($dir);
  363. my $include_binaries = $self->{'options'}{'include_binaries'};
  364. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  365. my $sourcepackage = $self->{'fields'}{'Source'};
  366. my $basenamerev = $self->get_basename(1);
  367. # Prepare handling of binary files
  368. my %auth_bin_files;
  369. my $incbin_file = File::Spec->catfile($dir, "debian", "source", "include-binaries");
  370. if (-f $incbin_file) {
  371. open(INC, "<", $incbin_file) || syserr(_g("cannot read %s"), $incbin_file);
  372. while(defined($_ = <INC>)) {
  373. chomp; s/^\s*//; s/\s*$//;
  374. next if /^#/ or /^$/;
  375. $auth_bin_files{$_} = 1;
  376. }
  377. close(INC);
  378. }
  379. my @binary_files;
  380. my $handle_binary = sub {
  381. my ($self, $old, $new) = @_;
  382. my $relfn = File::Spec->abs2rel($new, $dir);
  383. # Include binaries if they are whitelisted or if
  384. # --include-binaries has been given
  385. if ($include_binaries or $auth_bin_files{$relfn}) {
  386. push @binary_files, $relfn;
  387. } else {
  388. errormsg(_g("cannot represent change to %s: %s"), $new,
  389. _g("binary file contents changed"));
  390. errormsg(_g("add %s in debian/source/include-binaries if you want" .
  391. " to store the modified binary in the debian tarball"),
  392. $relfn);
  393. $self->register_error();
  394. }
  395. };
  396. # Check if the debian directory contains unwanted binary files
  397. my $unwanted_binaries = 0;
  398. my $check_binary = sub {
  399. my $fn = File::Spec->abs2rel($_, $dir);
  400. if (-f $_ and is_binary($_)) {
  401. if ($include_binaries or $auth_bin_files{$fn}) {
  402. push @binary_files, $fn;
  403. } else {
  404. errormsg(_g("unwanted binary file: %s"), $fn);
  405. $unwanted_binaries++;
  406. }
  407. }
  408. };
  409. my $tar_ignore_glob = "{" . join(",",
  410. map {
  411. my $copy = $_;
  412. $copy =~ s/,/\\,/g;
  413. $copy;
  414. } @{$self->{'options'}{'tar_ignore'}}) . "}";
  415. my $filter_ignore = sub {
  416. # Filter out files that are not going to be included in the debian
  417. # tarball due to ignores.
  418. my %exclude;
  419. my $reldir = File::Spec->abs2rel($File::Find::dir, $dir);
  420. my $cwd = getcwd();
  421. # Apply the pattern both from the top dir and from the inspected dir
  422. chdir($dir) || syserr(_g("unable to chdir to `%s'"), $dir);
  423. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  424. chdir($cwd) || syserr(_g("unable to chdir to `%s'"), $cwd);
  425. chdir($File::Find::dir) ||
  426. syserr(_g("unable to chdir to `%s'"), $File::Find::dir);
  427. $exclude{$_} = 1 foreach glob($tar_ignore_glob);
  428. chdir($cwd) || syserr(_g("unable to chdir to `%s'"), $cwd);
  429. my @result;
  430. foreach my $fn (@_) {
  431. unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
  432. push @result, $fn;
  433. }
  434. }
  435. return @result;
  436. };
  437. find({ wanted => $check_binary, preprocess => $filter_ignore,
  438. no_chdir => 1 }, File::Spec->catdir($dir, "debian"));
  439. error(P_("detected %d unwanted binary file (add it in " .
  440. "debian/source/include-binaries to allow its inclusion).",
  441. "detected %d unwanted binary files (add them in " .
  442. "debian/source/include-binaries to allow their inclusion).",
  443. $unwanted_binaries), $unwanted_binaries)
  444. if $unwanted_binaries;
  445. # Create a patch
  446. my $autopatch = File::Spec->catfile($dir, "debian", "patches",
  447. $self->get_autopatch_name());
  448. my $tmpdiff = $self->generate_patch($dir, order_from => $autopatch,
  449. handle_binary => $handle_binary,
  450. usage => 'build');
  451. push @Dpkg::Exit::handlers, sub { unlink($tmpdiff) };
  452. # Install the diff as the new autopatch
  453. mkpath(File::Spec->catdir($dir, "debian", "patches"));
  454. $autopatch = $self->register_patch($dir, $tmpdiff,
  455. $self->get_autopatch_name());
  456. if (-e $autopatch and $self->{'options'}{'abort_on_upstream_changes'}) {
  457. error(_g("aborting due to --abort-on-upstream-changes"));
  458. }
  459. info(_g("local changes have been recorded in a new patch: %s"), $autopatch);
  460. rmdir(File::Spec->catdir($dir, "debian", "patches")); # No check on purpose
  461. unlink($tmpdiff) || syserr(_g("cannot remove %s"), $tmpdiff);
  462. pop @Dpkg::Exit::handlers;
  463. # Update debian/source/include-binaries if needed
  464. if (scalar(@binary_files) and $include_binaries) {
  465. mkpath(File::Spec->catdir($dir, "debian", "source"));
  466. open(INC, ">>", $incbin_file) || syserr(_g("cannot write %s"), $incbin_file);
  467. foreach my $binary (@binary_files) {
  468. unless ($auth_bin_files{$binary}) {
  469. print INC "$binary\n";
  470. info(_g("adding %s to %s"), $binary, "debian/source/include-binaries");
  471. }
  472. }
  473. close(INC);
  474. }
  475. # Create the debian.tar
  476. my $debianfile = "$basenamerev.debian.tar." . $self->{'options'}{'comp_ext'};
  477. info(_g("building %s in %s"), $sourcepackage, $debianfile);
  478. my $tar = Dpkg::Source::Archive->new(filename => $debianfile);
  479. $tar->create(options => \@tar_ignore, 'chdir' => $dir);
  480. $tar->add_directory("debian");
  481. foreach my $binary (@binary_files) {
  482. $tar->add_file($binary) unless $binary =~ m{^debian/};
  483. }
  484. $tar->finish();
  485. $self->add_file($debianfile);
  486. }
  487. sub get_patch_header {
  488. my ($self, $dir) = @_;
  489. my $ph = File::Spec->catfile($dir, "debian", "source", "local-patch-header");
  490. unless (-f $ph) {
  491. $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  492. }
  493. my $text;
  494. if (-f $ph) {
  495. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  496. $text = join("", <PH>);
  497. close(PH);
  498. return $text;
  499. }
  500. my $ch_info = changelog_parse(offset => 0, count => 1,
  501. file => File::Spec->catfile($dir, "debian", "changelog"));
  502. return '' if not defined $ch_info;
  503. my $header = Dpkg::Control->new(type => CTRL_UNKNOWN);
  504. $header->{'Description'} = "<short summary of the patch>\n";
  505. $header->{'Description'} .=
  506. "TODO: Put a short summary on the line above and replace this paragraph
  507. with a longer explanation of this change. Complete the meta-information
  508. with other relevant fields (see below for details). To make it easier, the
  509. information below has been extracted from the changelog. Adjust it or drop
  510. it.\n";
  511. $header->{'Description'} .= $ch_info->{'Changes'} . "\n";
  512. $header->{'Author'} = $ch_info->{'Maintainer'};
  513. $text = "$header";
  514. run_vendor_hook("extend-patch-header", \$text, $ch_info);
  515. $text .= "\n---
  516. The information above should follow the Patch Tagging Guidelines, please
  517. checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
  518. are templates for supplementary fields that you might want to add:
  519. Origin: <vendor|upstream|other>, <url of original patch>
  520. Bug: <url in upstream bugtracker>
  521. Bug-Debian: http://bugs.debian.org/<bugnumber>
  522. Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
  523. Forwarded: <no|not-needed|url proving that it has been forwarded>
  524. Reviewed-By: <name and email of someone who approved the patch>
  525. Last-Update: <YYYY-MM-DD>\n\n";
  526. return $text;
  527. }
  528. sub register_patch {
  529. my ($self, $dir, $patch_file, $patch_name) = @_;
  530. my $patch = File::Spec->catfile($dir, "debian", "patches", $patch_name);
  531. if (-s $patch_file) {
  532. copy($patch_file, $patch) ||
  533. syserr(_g("failed to copy %s to %s"), $patch_file, $patch);
  534. chmod(0666 & ~ umask(), $patch) ||
  535. syserr(_g("unable to change permission of `%s'"), $patch);
  536. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  537. open(APPLIED, '>>', $applied) || syserr(_g("cannot write %s"), $applied);
  538. print APPLIED "$patch\n";
  539. close(APPLIED) || syserr(_g("cannot close %s"), $applied);
  540. } elsif (-e $patch) {
  541. unlink($patch) || syserr(_g("cannot remove %s"), $patch);
  542. }
  543. return $patch;
  544. }
  545. # vim:et:sw=4:ts=8
  546. 1;