quilt.pm 5.2 KB

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