quilt.pm 11 KB

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