quilt.pm 10 KB

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