quilt.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Package::V3::quilt;
  16. use strict;
  17. use warnings;
  18. # Based on wig&pen implementation
  19. use base 'Dpkg::Source::Package::V2';
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Source::Patch;
  24. use Dpkg::IPC;
  25. use Dpkg::Vendor qw(get_current_vendor);
  26. use POSIX;
  27. use File::Basename;
  28. use File::Spec;
  29. use File::Path;
  30. our $CURRENT_MINOR_VERSION = "0";
  31. sub init_options {
  32. my ($self) = @_;
  33. $self->SUPER::init_options();
  34. # By default use quilt, unless it's not available
  35. $self->{'options'}{'without_quilt'} = (-x "/usr/bin/quilt") ? 0 : 1
  36. unless exists $self->{'options'}{'without_quilt'};
  37. }
  38. sub parse_cmdline_option {
  39. my ($self, $opt) = @_;
  40. return 1 if $self->SUPER::parse_cmdline_option($opt);
  41. if ($opt =~ /^--without-quilt$/) {
  42. $self->{'options'}{'without_quilt'} = 1;
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. sub get_autopatch_name {
  48. my ($self) = @_;
  49. return "debian-changes-" . $self->{'fields'}{'Version'};
  50. }
  51. sub get_series_file {
  52. my ($self, $dir) = @_;
  53. my $pd = File::Spec->catdir($dir, "debian", "patches");
  54. my $vendor = lc(get_current_vendor() || "debian");
  55. foreach (File::Spec->catfile($pd, "$vendor.series"),
  56. File::Spec->catfile($pd, "series")) {
  57. return $_ if -e $_;
  58. }
  59. return undef;
  60. }
  61. sub get_patches {
  62. my ($self, $dir, $skip_auto) = @_;
  63. my @patches;
  64. my $auto_patch = $self->get_autopatch_name();
  65. my $series = $self->get_series_file($dir);
  66. if (defined($series)) {
  67. open(SERIES, "<" , $series) || syserr(_g("cannot read %s"), $series);
  68. while(defined($_ = <SERIES>)) {
  69. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  70. s/(^|\s+)#.*$//; # Strip comment
  71. next unless $_;
  72. if (/^(\S+)\s+(.*)$/) {
  73. $_ = $1;
  74. if ($2 ne '-p1') {
  75. warning(_g("the series file (%s) contains unsupported " .
  76. "options ('%s', line %s), dpkg-source might " .
  77. "fail when applying patches."),
  78. $series, $2, $.) unless $skip_auto;
  79. }
  80. }
  81. next if $skip_auto and $_ eq $auto_patch;
  82. push @patches, $_;
  83. }
  84. close(SERIES);
  85. }
  86. return @patches;
  87. }
  88. sub run_quilt {
  89. my ($self, $dir, $params, %more_opts) = @_;
  90. $params = [ $params ] unless ref($params) eq "ARRAY";
  91. my $absdir = $dir;
  92. unless (File::Spec->file_name_is_absolute($absdir)) {
  93. $absdir = File::Spec->rel2abs($dir);
  94. }
  95. my $series = $self->get_series_file($dir);
  96. # Use default name if no series files exist yet
  97. $series = "$absdir/debian/patches/series" unless defined $series;
  98. unless (File::Spec->file_name_is_absolute($series)) {
  99. $series = File::Spec->rel2abs($series);
  100. }
  101. my %opts = (
  102. env => { QUILT_PATCHES => "$absdir/debian/patches",
  103. QUILT_SERIES => $series },
  104. 'chdir' => $dir,
  105. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', @$params ],
  106. %more_opts
  107. );
  108. my $pid = fork_and_exec(%opts);
  109. return $pid;
  110. }
  111. sub apply_patches {
  112. my ($self, $dir, $skip_auto) = @_;
  113. # Update debian/patches/series symlink if needed to allow quilt usage
  114. my $series = $self->get_series_file($dir);
  115. return unless $series; # No series, no patches
  116. my $basename = basename($series);
  117. if ($basename ne "series") {
  118. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  119. unlink($dest) if -l $dest;
  120. unless (-f _) { # Don't overwrite real files
  121. symlink($basename, $dest) ||
  122. syserr(_g("can't create symlink %s"), $dest);
  123. }
  124. }
  125. my @patches = $self->get_patches($dir, $skip_auto);
  126. return unless scalar(@patches);
  127. # Apply patches
  128. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  129. open(APPLIED, '>', $applied) || syserr(_g("cannot write %s"), $applied);
  130. my $now = time();
  131. my $pobj = {};
  132. my $panalysis = {};
  133. foreach my $patch (@patches) {
  134. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  135. $pobj->{$patch} = Dpkg::Source::Patch->new(filename => $path);
  136. if ($self->{'options'}{'without_quilt'}) {
  137. info(_g("applying %s"), $patch) unless $skip_auto;
  138. $pobj->{$patch}->apply($dir, timestamp => $now,
  139. force_timestamp => 1, create_dirs => 1,
  140. add_options => [ '-E' ]);
  141. print APPLIED "$patch\n";
  142. } else {
  143. $panalysis->{$patch} = $pobj->{$patch}->analyze($dir);
  144. foreach my $dir (keys %{$panalysis->{$patch}->{'dirtocreate'}}) {
  145. eval { mkpath($dir); };
  146. syserr(_g("cannot create directory %s"), $dir) if $@;
  147. }
  148. }
  149. }
  150. if (not $self->{'options'}{'without_quilt'}) {
  151. my %opts;
  152. $opts{"to_file"} = "/dev/null" if $skip_auto;
  153. info(_g("applying all patches with %s"), "quilt push -q " . $patches[-1]) unless $skip_auto;
  154. $self->run_quilt($dir, ['push', '-q', $patches[-1]],
  155. delete_env => ['QUILT_PATCH_OPTS'],
  156. wait_child => 1, %opts);
  157. foreach my $patch (@patches) {
  158. foreach my $fn (keys %{$panalysis->{$patch}->{'filepatched'}}) {
  159. utime($now, $now, $fn) || $! == ENOENT ||
  160. syserr(_g("cannot change timestamp for %s"), $fn);
  161. }
  162. print APPLIED "$patch\n";
  163. }
  164. }
  165. close(APPLIED);
  166. }
  167. sub prepare_build {
  168. my ($self, $dir) = @_;
  169. $self->SUPER::prepare_build($dir);
  170. # Skip .pc directories of quilt by default and ignore difference
  171. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  172. # stamp file created by ourselves
  173. my $func = sub {
  174. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  175. return 1 if $_[0] =~ m{^debian/patches/.dpkg-source-applied$};
  176. return 1 if $_[0] =~ /^.pc(\/|$)/;
  177. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  178. return 0;
  179. };
  180. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  181. }
  182. sub check_patches_applied {
  183. my ($self, $dir) = @_;
  184. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  185. my $auto_patch = $self->get_autopatch_name();
  186. my @patches ;
  187. # First we try to get a list of patches that are probably not napplied
  188. if (not $self->{'options'}{'without_quilt'}) {
  189. my $pipe;
  190. my $pid = $self->run_quilt($dir, ['unapplied'], error_to_file => '/dev/null',
  191. to_pipe => \$pipe);
  192. @patches = map { chomp; $_ } (<$pipe>);
  193. close ($pipe) || syserr("close on 'quilt unapplied' pipe");
  194. wait_child($pid, cmdline => "quilt unapplied", nocheck => 1);
  195. subprocerr("quilt unapplied") unless WIFEXITED($?);
  196. } else {
  197. @patches = $self->get_patches($dir);
  198. }
  199. # Then we check if it's applicable, and if yes, we make the
  200. # assumption that patches are not applied and need to be applied
  201. if (scalar(@patches)) {
  202. my $first_patch = File::Spec->catfile($dir, "debian", "patches", $patches[0]);
  203. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  204. if ($patch_obj->check_apply($dir)) {
  205. warning(_g("patches have not been applied, applying them now (use --no-preparation to override)"));
  206. $self->apply_patches($dir);
  207. }
  208. }
  209. }
  210. sub register_autopatch {
  211. my ($self, $dir) = @_;
  212. my $auto_patch = $self->get_autopatch_name();
  213. my @patches = $self->get_patches($dir);
  214. my $has_patch = (grep { $_ eq $auto_patch } @patches) ? 1 : 0;
  215. my $series = $self->get_series_file($dir);
  216. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  217. my $applied = File::Spec->catfile($dir, "debian", "patches", ".dpkg-source-applied");
  218. my $patch = File::Spec->catfile($dir, "debian", "patches", $auto_patch);
  219. my $absdir = $dir;
  220. unless (File::Spec->file_name_is_absolute($absdir)) {
  221. $absdir = File::Spec->rel2abs($dir);
  222. }
  223. if (-e $patch) {
  224. # Add auto_patch to series file
  225. if (not $has_patch) {
  226. # Use quilt to register only if it's wanted/available AND :
  227. # - either we have patches and quilt has been used (.pc dir exists)
  228. # - or we don't have patches, hence quilt couldn't be used
  229. if ((-d "$dir/.pc" or not scalar(@patches)) and
  230. not $self->{'options'}{'without_quilt'})
  231. {
  232. # Registering the new patch with quilt requires some
  233. # trickery: reverse-apply the patch, create a new quilt patch,
  234. # fold the patch into the quilt-managed one
  235. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  236. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  237. $self->run_quilt($dir, ['new', "$auto_patch"],
  238. wait_child => 1, to_file => '/dev/null');
  239. $self->run_quilt($dir, ['fold'],
  240. from_file => "$absdir/debian/patches/$auto_patch",
  241. wait_child => 1, to_file => '/dev/null');
  242. } else {
  243. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  244. print SERIES "$auto_patch\n";
  245. close(SERIES);
  246. }
  247. } else {
  248. # If quilt was used, ensure its meta-information are
  249. # synchronized with the updated patch
  250. if (-d "$dir/.pc" and not $self->{'options'}{'without_quilt'}) {
  251. # Some trickery needed: reverse-apply the patch, fold the
  252. # new patch into the quilt-managed one
  253. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  254. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  255. $self->run_quilt($dir, ['fold'],
  256. from_file => "$absdir/debian/patches/$auto_patch",
  257. wait_child => 1, to_file => '/dev/null');
  258. }
  259. }
  260. } else {
  261. # Remove auto_patch from series
  262. if ($has_patch) {
  263. if ($self->{'options'}{'without_quilt'}) {
  264. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  265. my @lines = <SERIES>;
  266. close(SERIES);
  267. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  268. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  269. close(SERIES);
  270. } else {
  271. $self->run_quilt($dir, ['delete', $auto_patch],
  272. wait_child => 1, to_file => '/dev/null');
  273. }
  274. }
  275. # Clean up empty series
  276. unlink($series) if not -s $series;
  277. }
  278. }
  279. # vim:et:sw=4:ts=8
  280. 1;