quilt.pm 12 KB

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