quilt.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 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 $absdir = $dir;
  90. unless (File::Spec->file_name_is_absolute($absdir)) {
  91. $absdir = File::Spec->rel2abs($dir);
  92. }
  93. my %opts = (
  94. env => { QUILT_PATCHES => "$absdir/debian/patches" },
  95. 'chdir' => $dir,
  96. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', @$params ],
  97. %more_opts
  98. );
  99. my $pid = fork_and_exec(%opts);
  100. return $pid;
  101. }
  102. sub apply_patches {
  103. my ($self, $dir, $skip_auto) = @_;
  104. # Update debian/patches/series symlink if needed to allow quilt usage
  105. my $series = $self->get_series_file($dir);
  106. return unless $series; # No series, no patches
  107. my $basename = basename($series);
  108. if ($basename ne "series") {
  109. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  110. unlink($dest) if -l $dest;
  111. unless (-f _) { # Don't overwrite real files
  112. symlink($basename, $dest) ||
  113. syserr(_g("can't create symlink %s"), $dest);
  114. }
  115. }
  116. # Apply patches
  117. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  118. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  119. my $now = time();
  120. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  121. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  122. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  123. if (not $self->{'options'}{'without_quilt'}) {
  124. info(_g("applying %s with quilt"), $patch) unless $skip_auto;
  125. my $analysis = $patch_obj->analyze($dir);
  126. foreach my $dir (keys %{$analysis->{'dirtocreate'}}) {
  127. eval { mkpath($dir); };
  128. syserr(_g("cannot create directory %s"), $dir) if $@;
  129. }
  130. $self->run_quilt($dir, ['push', $patch],
  131. delete_env => ['QUILT_PATCH_OPTS'],
  132. wait_child => 1,
  133. to_file => '/dev/null');
  134. foreach my $fn (keys %{$analysis->{'filepatched'}}) {
  135. utime($now, $now, $fn) || $! == ENOENT ||
  136. syserr(_g("cannot change timestamp for %s"), $fn);
  137. }
  138. } else {
  139. info(_g("applying %s"), $patch) unless $skip_auto;
  140. $patch_obj->apply($dir, timestamp => $now,
  141. force_timestamp => 1, create_dirs => 1,
  142. add_options => [ '-E' ]);
  143. }
  144. print APPLIED "$patch\n";
  145. }
  146. close(APPLIED);
  147. }
  148. sub prepare_build {
  149. my ($self, $dir) = @_;
  150. $self->SUPER::prepare_build($dir);
  151. # Skip .pc directories of quilt by default and ignore difference
  152. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  153. # stamp file created by ourselves
  154. my $func = sub {
  155. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  156. return 1 if $_[0] =~ m{^debian/patches/.dpkg-source-applied$};
  157. return 1 if $_[0] =~ /^.pc(\/|$)/;
  158. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  159. return 0;
  160. };
  161. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  162. }
  163. sub check_patches_applied {
  164. my ($self, $dir) = @_;
  165. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  166. my $auto_patch = $self->get_autopatch_name();
  167. my @patches ;
  168. # First we try to get a list of patches that are probably not napplied
  169. if (not $self->{'options'}{'without_quilt'}) {
  170. my $pipe;
  171. my $pid = $self->run_quilt($dir, ['unapplied'], error_to_file => '/dev/null',
  172. to_pipe => \$pipe);
  173. @patches = map { chomp; $_ } (<$pipe>);
  174. close ($pipe) || syserr("close on 'quilt unapplied' pipe");
  175. wait_child($pid, cmdline => "quilt unapplied", nocheck => 1);
  176. subprocerr("quilt unapplied") unless WIFEXITED($?);
  177. } else {
  178. @patches = $self->get_patches($dir);
  179. }
  180. # Then we check if it's applicable, and if yes, we make the
  181. # assumption that patches are not applied and need to be applied
  182. if (scalar(@patches)) {
  183. my $first_patch = File::Spec->catfile($dir, "debian", "patches", $patches[0]);
  184. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  185. if ($patch_obj->check_apply($dir)) {
  186. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  187. $self->apply_patches($dir);
  188. }
  189. }
  190. }
  191. sub register_autopatch {
  192. my ($self, $dir) = @_;
  193. my $auto_patch = $self->get_autopatch_name();
  194. my @patches = $self->get_patches($dir);
  195. my $has_patch = (grep { $_ eq $auto_patch } @patches) ? 1 : 0;
  196. my $series = $self->get_series_file($dir);
  197. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  198. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  199. my $patch = File::Spec->catfile($dir, "debian", "patches", $auto_patch);
  200. my $absdir = $dir;
  201. unless (File::Spec->file_name_is_absolute($absdir)) {
  202. $absdir = File::Spec->rel2abs($dir);
  203. }
  204. if (-e $patch) {
  205. # Add auto_patch to series file
  206. if (not $has_patch) {
  207. # Use quilt to register only if it's wanted/available AND :
  208. # - either we have patches and quilt has been used (.pc dir exists)
  209. # - or we don't have patches, hence quilt couldn't be used
  210. if ((-d "$dir/.pc" or not scalar(@patches)) and
  211. not $self->{'options'}{'without_quilt'})
  212. {
  213. # Registering the new patch with quilt requires some
  214. # trickery: reverse-apply the patch, import it, apply it
  215. # again with quilt this time
  216. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  217. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  218. $self->run_quilt($dir, ['import', "$absdir/debian/patches/$auto_patch"],
  219. wait_child => 1, to_file => '/dev/null');
  220. $self->run_quilt($dir, ['push'], wait_child => 1, to_file => '/dev/null');
  221. } else {
  222. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  223. print SERIES "$auto_patch\n";
  224. close(SERIES);
  225. }
  226. open(APPLIED, ">>", $applied) || syserr(_g("cannot write %s"), $applied);
  227. print APPLIED "$auto_patch\n";
  228. close(APPLIED);
  229. }
  230. } else {
  231. # Remove auto_patch from series
  232. if ($has_patch) {
  233. if ($self->{'options'}{'without_quilt'}) {
  234. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  235. my @lines = <SERIES>;
  236. close(SERIES);
  237. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  238. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  239. close(SERIES);
  240. } else {
  241. $self->run_quilt($dir, ['delete', $auto_patch],
  242. wait_child => 1, to_file => '/dev/null');
  243. }
  244. }
  245. }
  246. }
  247. # vim:et:sw=4:ts=8
  248. 1;