V2.pm 29 KB

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