quilt.pm 8.9 KB

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