quilt.pm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_0::quilt;
  14. use strict;
  15. use warnings;
  16. # Based on wig&pen implementation
  17. use base 'Dpkg::Source::Package::V2_0';
  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. sub init_options {
  27. my ($self) = @_;
  28. $self->SUPER::init_options();
  29. $self->{'options'}{'without_quilt'} = 0
  30. unless exists $self->{'options'}{'without_quilt'};
  31. }
  32. sub parse_cmdline_option {
  33. my ($self, $opt) = @_;
  34. return 1 if $self->SUPER::parse_cmdline_option($opt);
  35. if ($opt =~ /^--without-quilt$/) {
  36. $self->{'options'}{'without_quilt'} = 1;
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. sub get_autopatch_name {
  42. my ($self) = @_;
  43. return "debian-changes-" . $self->{'fields'}{'Version'} . ".diff";
  44. }
  45. sub get_series_file {
  46. my ($self, $dir) = @_;
  47. my $pd = File::Spec->catdir($dir, "debian", "patches");
  48. # TODO: replace "debian" with the current distro name once we have a
  49. # way to figure it out
  50. foreach (File::Spec->catfile($pd, "debian.series"),
  51. File::Spec->catfile($pd, "series")) {
  52. return $_ if -e $_;
  53. }
  54. return undef;
  55. }
  56. sub get_patches {
  57. my ($self, $dir, $skip_auto) = @_;
  58. my @patches;
  59. my $auto_patch = $self->get_autopatch_name();
  60. my $series = $self->get_series_file($dir);
  61. if (defined($series)) {
  62. open(SERIES, "<" , $series) || syserr(_g("cannot read %s"), $series);
  63. while(defined($_ = <SERIES>)) {
  64. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  65. s/\s#.*$//; # Strip trailing comment
  66. next unless $_;
  67. next if $skip_auto and $_ eq $auto_patch;
  68. push @patches, $_;
  69. }
  70. close(SERIES);
  71. }
  72. return @patches;
  73. }
  74. sub apply_patches {
  75. my ($self, $dir, $skip_auto) = @_;
  76. # Check if quilt is available
  77. my $have_quilt = (-x "/usr/bin/quilt") ? 1 : 0;
  78. # Update debian/patches/series symlink if needed to allow quilt usage
  79. my $series = $self->get_series_file($dir);
  80. return unless $series; # No series, no patches
  81. my $basename = basename($series);
  82. if ($basename ne "series") {
  83. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  84. unlink($dest) if -l $dest;
  85. unless (-f _) { # Don't overwrite real files
  86. symlink($basename, $dest) ||
  87. syserr(_g("can't create symlink %s"), $dest);
  88. }
  89. }
  90. # Apply patches
  91. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  92. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  93. my $now = time();
  94. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  95. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  96. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  97. if ($have_quilt and not $self->{'options'}{'without_quilt'}) {
  98. info(_g("applying %s with quilt"), $patch) unless $skip_auto;
  99. my $analysis = $patch_obj->analyze($dir);
  100. foreach my $dir (keys %{$analysis->{'dirtocreate'}}) {
  101. eval { mkpath($dir); };
  102. syserr(_g("cannot create directory %s"), $dir) if $@;
  103. }
  104. my %opts = (
  105. env => { QUILT_PATCHES => 'debian/patches' },
  106. delete_env => [ 'QUILT_PATCH_OPTS' ],
  107. 'chdir' => $dir,
  108. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', 'push', $patch ],
  109. wait_child => 1,
  110. to_file => '/dev/null',
  111. );
  112. fork_and_exec(%opts);
  113. foreach my $fn (keys %{$analysis->{'filepatched'}}) {
  114. utime($now, $now, $fn) || $! == ENOENT ||
  115. syserr(_g("cannot change timestamp for %s"), $fn);
  116. }
  117. } else {
  118. info(_g("applying %s"), $patch) unless $skip_auto;
  119. $patch_obj->apply($dir, timestamp => $now,
  120. force_timestamp => 1, create_dirs => 1,
  121. add_options => [ '-E' ]);
  122. }
  123. print APPLIED "$patch\n";
  124. }
  125. close(APPLIED);
  126. }
  127. sub prepare_build {
  128. my ($self, $dir) = @_;
  129. $self->SUPER::prepare_build($dir);
  130. # Skip .pc directories of quilt by default and ignore difference
  131. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  132. # stamp file created by ourselves
  133. my $func = sub {
  134. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  135. return 1 if $_[0] =~ m{^debian/patches/.dpkg-source-applied$};
  136. return 1 if $_[0] =~ /^.pc(\/|$)/;
  137. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  138. return 0;
  139. };
  140. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  141. }
  142. sub check_patches_applied {
  143. my ($self, $dir) = @_;
  144. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  145. my $quiltdir = File::Spec->catdir($dir, ".pc");
  146. if (-d $quiltdir) {
  147. my $auto_patch = $self->get_autopatch_name();
  148. my $pipe;
  149. my %opts = (env => { QUILT_PATCHES => 'debian/patches' },
  150. delete_env => [ 'QUILT_PATCH_OPTS' ],
  151. 'chdir' => $dir,
  152. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', 'unapplied' ],
  153. error_to_file => "/dev/null",
  154. to_pipe => \$pipe);
  155. my $pid = fork_and_exec(%opts);
  156. # We skip auto_patch as this one might be applied but not by
  157. # quilt... but by the user who made changes live in the tree
  158. # and whose changes lead to this patch addition by a previous
  159. # dpkg-source run.
  160. my @patches = grep { chomp; $_ ne $auto_patch } (<$pipe>);
  161. close ($pipe) || syserr("close on 'quilt unapplied' pipe");
  162. wait_child($pid, cmdline => "quilt unapplied", nocheck => 1);
  163. if (@patches) {
  164. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  165. $opts{'wait_child'} = 1;
  166. $opts{'to_file'} = "/dev/null";
  167. delete $opts{'to_pipe'};
  168. foreach my $patch (@patches) {
  169. info(_g("applying %s with quilt"), $patch);
  170. $opts{'exec'} = [ 'quilt', '--quiltrc', '/dev/null', 'push', $patch ];
  171. fork_and_exec(%opts);
  172. }
  173. }
  174. return;
  175. }
  176. unless (-e $applied) {
  177. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  178. $self->apply_patches($dir);
  179. }
  180. }
  181. sub register_autopatch {
  182. my ($self, $dir) = @_;
  183. my $auto_patch = $self->get_autopatch_name();
  184. my $has_patch = (grep { $_ eq $auto_patch } $self->get_patches($dir)) ? 1 : 0;
  185. my $series = $self->get_series_file($dir);
  186. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  187. if (-e "$dir/debian/patches/$auto_patch") {
  188. # Add auto_patch to series file
  189. if (not $has_patch) {
  190. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  191. print SERIES "$auto_patch\n";
  192. close(SERIES);
  193. }
  194. } else {
  195. # Remove auto_patch from series
  196. if ($has_patch) {
  197. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  198. my @lines = <SERIES>;
  199. close(SERIES);
  200. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  201. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  202. close(SERIES);
  203. }
  204. }
  205. }
  206. # vim:et:sw=4:ts=8
  207. 1;