quilt.pm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # Copyright 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Source::Package::V3::quilt;
  14. use strict;
  15. use warnings;
  16. # Based on wig&pen implementation
  17. use base 'Dpkg::Source::Package::V2';
  18. use Dpkg;
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling qw(error syserr warning usageerr subprocerr info);
  21. use Dpkg::Source::Patch;
  22. use Dpkg::IPC;
  23. use POSIX;
  24. use File::Basename;
  25. use File::Spec;
  26. use File::Path;
  27. our $CURRENT_MINOR_VERSION = "0";
  28. sub init_options {
  29. my ($self) = @_;
  30. $self->SUPER::init_options();
  31. # By default use quilt, unless it's not available
  32. $self->{'options'}{'without_quilt'} = (-x "/usr/bin/quilt") ? 0 : 1
  33. unless exists $self->{'options'}{'without_quilt'};
  34. }
  35. sub parse_cmdline_option {
  36. my ($self, $opt) = @_;
  37. return 1 if $self->SUPER::parse_cmdline_option($opt);
  38. if ($opt =~ /^--without-quilt$/) {
  39. $self->{'options'}{'without_quilt'} = 1;
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. sub get_autopatch_name {
  45. my ($self) = @_;
  46. return "debian-changes-" . $self->{'fields'}{'Version'};
  47. }
  48. sub get_series_file {
  49. my ($self, $dir) = @_;
  50. my $pd = File::Spec->catdir($dir, "debian", "patches");
  51. # TODO: replace "debian" with the current distro name once we have a
  52. # way to figure it out
  53. foreach (File::Spec->catfile($pd, "debian.series"),
  54. File::Spec->catfile($pd, "series")) {
  55. return $_ if -e $_;
  56. }
  57. return undef;
  58. }
  59. sub get_patches {
  60. my ($self, $dir, $skip_auto) = @_;
  61. my @patches;
  62. my $auto_patch = $self->get_autopatch_name();
  63. my $series = $self->get_series_file($dir);
  64. if (defined($series)) {
  65. open(SERIES, "<" , $series) || syserr(_g("cannot read %s"), $series);
  66. while(defined($_ = <SERIES>)) {
  67. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  68. s/\s#.*$//; # Strip trailing comment
  69. next unless $_;
  70. if (/^(\S+)\s+(.*)$/) {
  71. $_ = $1;
  72. if ($2 ne '-p1') {
  73. warning(_g("the series file (%s) contains unsupported " .
  74. "options ('%s', line %s), dpkg-source might " .
  75. "fail when applying patches."),
  76. $series, $2, $.) unless $skip_auto;
  77. }
  78. }
  79. next if $skip_auto and $_ eq $auto_patch;
  80. push @patches, $_;
  81. }
  82. close(SERIES);
  83. }
  84. return @patches;
  85. }
  86. sub run_quilt {
  87. my ($self, $dir, $params, %more_opts) = @_;
  88. $params = [ $params ] unless ref($params) eq "ARRAY";
  89. my %opts = (
  90. env => { QUILT_PATCHES => 'debian/patches' },
  91. 'chdir' => $dir,
  92. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', @$params ],
  93. %more_opts
  94. );
  95. my $pid = fork_and_exec(%opts);
  96. return $pid;
  97. }
  98. sub apply_patches {
  99. my ($self, $dir, $skip_auto) = @_;
  100. # Update debian/patches/series symlink if needed to allow quilt usage
  101. my $series = $self->get_series_file($dir);
  102. return unless $series; # No series, no patches
  103. my $basename = basename($series);
  104. if ($basename ne "series") {
  105. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  106. unlink($dest) if -l $dest;
  107. unless (-f _) { # Don't overwrite real files
  108. symlink($basename, $dest) ||
  109. syserr(_g("can't create symlink %s"), $dest);
  110. }
  111. }
  112. # Apply patches
  113. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  114. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  115. my $now = time();
  116. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  117. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  118. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  119. if (not $self->{'options'}{'without_quilt'}) {
  120. info(_g("applying %s with quilt"), $patch) unless $skip_auto;
  121. my $analysis = $patch_obj->analyze($dir);
  122. foreach my $dir (keys %{$analysis->{'dirtocreate'}}) {
  123. eval { mkpath($dir); };
  124. syserr(_g("cannot create directory %s"), $dir) if $@;
  125. }
  126. $self->run_quilt($dir, ['push', $patch],
  127. delete_env => ['QUILT_PATCH_OPTS'],
  128. wait_child => 1,
  129. to_file => '/dev/null');
  130. foreach my $fn (keys %{$analysis->{'filepatched'}}) {
  131. utime($now, $now, $fn) || $! == ENOENT ||
  132. syserr(_g("cannot change timestamp for %s"), $fn);
  133. }
  134. } else {
  135. info(_g("applying %s"), $patch) unless $skip_auto;
  136. $patch_obj->apply($dir, timestamp => $now,
  137. force_timestamp => 1, create_dirs => 1,
  138. add_options => [ '-E' ]);
  139. }
  140. print APPLIED "$patch\n";
  141. }
  142. close(APPLIED);
  143. }
  144. sub prepare_build {
  145. my ($self, $dir) = @_;
  146. $self->SUPER::prepare_build($dir);
  147. # Skip .pc directories of quilt by default and ignore difference
  148. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  149. # stamp file created by ourselves
  150. my $func = sub {
  151. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  152. return 1 if $_[0] =~ m{^debian/patches/.dpkg-source-applied$};
  153. return 1 if $_[0] =~ /^.pc(\/|$)/;
  154. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  155. return 0;
  156. };
  157. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  158. }
  159. sub check_patches_applied {
  160. my ($self, $dir) = @_;
  161. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  162. my $auto_patch = $self->get_autopatch_name();
  163. my @patches ;
  164. # First we try to get a list of patches that are probably not napplied
  165. if (not $self->{'options'}{'without_quilt'}) {
  166. my $pipe;
  167. my $pid = $self->run_quilt($dir, ['unapplied'], error_to_file => '/dev/null',
  168. to_pipe => \$pipe);
  169. @patches = map { chomp; $_ } (<$pipe>);
  170. close ($pipe) || syserr("close on 'quilt unapplied' pipe");
  171. wait_child($pid, cmdline => "quilt unapplied", nocheck => 1);
  172. subprocerr("quilt unapplied") unless WIFEXITED($?);
  173. } else {
  174. @patches = $self->get_patches($dir);
  175. }
  176. # Then we check if it's applicable, and if yes, we make the
  177. # assumption that patches are not applied and need to be applied
  178. if (scalar(@patches)) {
  179. my $first_patch = File::Spec->catfile($dir, "debian", "patches", $patches[0]);
  180. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  181. if ($patch_obj->check_apply($dir)) {
  182. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  183. $self->apply_patches($dir);
  184. }
  185. }
  186. }
  187. sub register_autopatch {
  188. my ($self, $dir) = @_;
  189. my $auto_patch = $self->get_autopatch_name();
  190. my $has_patch = (grep { $_ eq $auto_patch } $self->get_patches($dir)) ? 1 : 0;
  191. my $series = $self->get_series_file($dir);
  192. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  193. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  194. my $patch = File::Spec->catfile($dir, "debian", "patches", $auto_patch);
  195. if (-e $patch) {
  196. # Add auto_patch to series file
  197. if (not $has_patch) {
  198. if ($self->{'options'}{'without_quilt'}) {
  199. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  200. print SERIES "$auto_patch\n";
  201. close(SERIES);
  202. } else {
  203. # Registering the new patch with quilt requires some
  204. # trickery: reverse-apply the patch, import it, apply it
  205. # again with quilt this time
  206. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  207. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  208. $self->run_quilt($dir, ['import', "debian/patches/$auto_patch"],
  209. wait_child => 1, to_file => '/dev/null');
  210. $self->run_quilt($dir, ['push'], wait_child => 1, to_file => '/dev/null');
  211. }
  212. open(APPLIED, ">>", $applied) || syserr(_g("cannot write %s"), $applied);
  213. print APPLIED "$auto_patch\n";
  214. close(APPLIED);
  215. }
  216. } else {
  217. # Remove auto_patch from series
  218. if ($has_patch) {
  219. if ($self->{'options'}{'without_quilt'}) {
  220. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  221. my @lines = <SERIES>;
  222. close(SERIES);
  223. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  224. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  225. close(SERIES);
  226. } else {
  227. $self->run_quilt($dir, ['delete', $auto_patch],
  228. wait_child => 1, to_file => '/dev/null');
  229. }
  230. }
  231. }
  232. }
  233. # vim:et:sw=4:ts=8
  234. 1;