V2.pm 27 KB

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