quilt.pm 11 KB

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