quilt.pm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_0::quilt;
  14. use strict;
  15. use warnings;
  16. # Based on wig&pen implementation
  17. use base 'Dpkg::Source::Package::V2_0';
  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. sub init_options {
  27. my ($self) = @_;
  28. $self->SUPER::init_options();
  29. $self->{'options'}{'without_quilt'} = 0
  30. unless exists $self->{'options'}{'without_quilt'};
  31. }
  32. sub parse_cmdline_option {
  33. my ($self, $opt) = @_;
  34. return 1 if $self->SUPER::parse_cmdline_option($opt);
  35. if ($opt =~ /^--without-quilt$/) {
  36. $self->{'options'}{'without_quilt'} = 1;
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. sub get_autopatch_name {
  42. my ($self) = @_;
  43. return "debian-changes-" . $self->{'fields'}{'Version'} . ".diff";
  44. }
  45. sub get_series_file {
  46. my ($self, $dir) = @_;
  47. my $pd = File::Spec->catdir($dir, "debian", "patches");
  48. # TODO: replace "debian" with the current distro name once we have a
  49. # way to figure it out
  50. foreach (File::Spec->catfile($pd, "debian.series"),
  51. File::Spec->catfile($pd, "series")) {
  52. return $_ if -e $_;
  53. }
  54. return undef;
  55. }
  56. sub get_patches {
  57. my ($self, $dir, $skip_auto) = @_;
  58. my @patches;
  59. my $auto_patch = $self->get_autopatch_name();
  60. my $series = $self->get_series_file($dir);
  61. if (defined($series)) {
  62. open(SERIES, "<" , $series) || syserr(_g("cannot read %s"), $series);
  63. while(defined($_ = <SERIES>)) {
  64. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  65. s/\s#.*$//; # Strip trailing comment
  66. next unless $_;
  67. next if $skip_auto and $_ eq $auto_patch;
  68. push @patches, $_;
  69. }
  70. close(SERIES);
  71. }
  72. return @patches;
  73. }
  74. sub apply_patches {
  75. my ($self, $dir, $skip_auto) = @_;
  76. # Check if quilt is available
  77. my $have_quilt = (-x "/usr/bin/quilt") ? 1 : 0;
  78. # Update debian/patches/series symlink if needed to allow quilt usage
  79. my $series = $self->get_series_file($dir);
  80. return unless $series; # No series, no patches
  81. my $basename = basename($series);
  82. if ($basename ne "series") {
  83. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  84. unlink($dest) if -l $dest;
  85. unless (-f _) { # Don't overwrite real files
  86. symlink($basename, $dest) ||
  87. syserr(_g("can't create symlink %s"), $dest);
  88. }
  89. }
  90. # Apply patches
  91. my $now = time();
  92. foreach my $patch ($self->get_patches($dir, $skip_auto)) {
  93. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  94. my $patch_obj = Dpkg::Source::Patch->new(filename => $path);
  95. if ($have_quilt and not $self->{'options'}{'without_quilt'}) {
  96. info(_g("applying %s with quilt"), $patch) unless $skip_auto;
  97. my $analysis = $patch_obj->analyze($dir);
  98. foreach my $dir (keys %{$analysis->{'dirtocreate'}}) {
  99. eval { mkpath($dir); };
  100. syserr(_g("cannot create directory %s"), $dir) if $@;
  101. }
  102. my %opts = (
  103. env => { QUILT_PATCHES => 'debian/patches' },
  104. delete_env => [ 'QUILT_PATCH_OPTS' ],
  105. 'chdir' => $dir,
  106. 'exec' => [ 'quilt', '--quiltrc', '/dev/null', 'push', $patch ],
  107. wait_child => 1,
  108. to_file => '/dev/null',
  109. );
  110. fork_and_exec(%opts);
  111. foreach my $fn (keys %{$analysis->{'filepatched'}}) {
  112. utime($now, $now, $fn) || $! == ENOENT ||
  113. syserr(_g("cannot change timestamp for %s"), $fn);
  114. }
  115. } else {
  116. info(_g("applying %s"), $patch) unless $skip_auto;
  117. $patch_obj->apply($dir, timestamp => $now,
  118. force_timestamp => 1, create_dirs => 1,
  119. add_options => [ '-E' ]);
  120. }
  121. }
  122. }
  123. sub prepare_build {
  124. my ($self, $dir) = @_;
  125. $self->SUPER::prepare_build($dir);
  126. # Skip .pc directories of quilt by default and ignore difference
  127. # on debian/patches/series symlinks
  128. my $func = sub {
  129. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  130. return 1 if $_[0] =~ /^.pc(\/|$)/;
  131. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  132. return 0;
  133. };
  134. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  135. }
  136. sub register_autopatch {
  137. my ($self, $dir) = @_;
  138. my $auto_patch = $self->get_autopatch_name();
  139. my $has_patch = (grep { $_ eq $auto_patch } $self->get_patches($dir)) ? 1 : 0;
  140. my $series = $self->get_series_file($dir);
  141. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  142. if (-e "$dir/debian/patches/$auto_patch") {
  143. # Add auto_patch to series file
  144. if (not $has_patch) {
  145. open(SERIES, ">>", $series) || syserr(_g("cannot write %s"), $series);
  146. print SERIES "$auto_patch\n";
  147. close(SERIES);
  148. }
  149. } else {
  150. # Remove auto_patch from series
  151. if ($has_patch) {
  152. open(SERIES, "<", $series) || syserr(_g("cannot read %s"), $series);
  153. my @lines = <SERIES>;
  154. close(SERIES);
  155. open(SERIES, ">", $series) || syserr(_g("cannot write %s"), $series);
  156. print(SERIES $_) foreach grep { not /^\Q$auto_patch\E\s*$/ } @lines;
  157. close(SERIES);
  158. }
  159. }
  160. }
  161. # vim:et:sw=4:ts=8
  162. 1;