[SCM] Debian Qt/KDE packaging tools branch, master, updated. debian/0.5.3-53-gbac2493

Modestas Vainius modax at alioth.debian.org
Fri Jan 29 23:51:18 UTC 2010


The following commit has been merged in the master branch:
commit ca7577aaf033c4390bdca55a6d7ff388927bcd06
Author: Modestas Vainius <modestas at vainius.eu>
Date:   Fri Jan 29 20:17:51 2010 +0200

    Improve and rename SymbolsHelper::Patch to SymbolsHelper::Patching.
    
    * SymbolsHelper::Patch is a class now (inside Patching.pm).
    * SymbolsHelper::Patching exports a couple of parse_*() functions to
      extract/parse patches and return them as SymbolsHelper::Patch objects.
---
 symbolshelper/Debian/PkgKde/SymbolsHelper/Patch.pm |  101 --------
 .../Debian/PkgKde/SymbolsHelper/Patching.pm        |  266 ++++++++++++++++++++
 2 files changed, 266 insertions(+), 101 deletions(-)

diff --git a/symbolshelper/Debian/PkgKde/SymbolsHelper/Patch.pm b/symbolshelper/Debian/PkgKde/SymbolsHelper/Patch.pm
deleted file mode 100644
index 9b3f2e7..0000000
--- a/symbolshelper/Debian/PkgKde/SymbolsHelper/Patch.pm
+++ /dev/null
@@ -1,101 +0,0 @@
-# Copyright (C) 2008-2010 Modestas Vainius <modax at debian.org>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>
-
-package Debian::PkgKde::SymbolsHelper::Patch;
-
-use strict;
-use warnings;
-use base 'Exporter';
-
-use File::Temp qw();
-use IO::Handle;
-use Dpkg::ErrorHandling;
-
-our @EXPORT = qw(patch_symbolfile);
-
-sub patch_symbolfile {
-    my ($filename, $patchfh) = @_;
-
-    # Copy current symbol file to temporary location
-    my ($patchedfh, $patchedfn) = File::Temp::tempfile();
-    open(my $fh, "<", $filename) or
-	error("unable to open symbol file at '$filename'");
-    while (<$fh>) {
-	print $patchedfh $_;
-    }
-    $patchedfh->flush();
-    close $fh;
-    close $patchedfh;
-
-    # Extract needed patch from the stream and adapt it to our needs
-    # (filenames to patch).
-    my ($patchsrc, $patcharch);
-    my $is_patch;
-    my $sameline = 0;
-    while($sameline || ($_ = <$patchfh>)) {
-	$sameline = 0;
-	if (defined $is_patch) {
-	    if (m/^(?:[+ -]|@@ )/) {
-		# Patch continues
-		print PATCH $_;
-		$is_patch++;
-	    } else {
-		# Patch ended
-		if (close(PATCH)) {
-		    # Successfully patched
-		    $patchsrc = undef;
-		    # $patcharch stays set
-		    # $is_patch stays set
-		    last;
-		} else {
-		    # Failed to patch. continue searching for another patch
-		    $sameline = 1;
-		    $patchsrc = undef;
-		    $patcharch = undef;
-		    $is_patch = undef;
-		    next;
-		}
-	    }
-	} elsif (defined $patchsrc) {
-	    if (m/^[+]{3}\s+\S+/) {
-		# Found the patch portion. Write the patch header
-		$is_patch = 0;
-		open(PATCH, "| patch --posix --force --quiet -r- -p0 >/dev/null 2>&1")
-		    or die "Unable to execute `patch` program";
-		print PATCH "--- ", $patchedfn, "\n";
-		print PATCH "+++ ", $patchedfn, "\n";
-	    } else {
-		$patchsrc = undef;
-		$patcharch = undef;
-	    }
-	} elsif (m/^[-]{3}\s+(\S+)(?:\s+\((\S+)\s+(\S+)\))?/) {
-	    $patchsrc = $1;
-	    $patcharch = $2;
-	}
-    }
-    # In case patch continued to the end of file, close it
-    my $symfile;
-    if(($patchsrc && close(PATCH)) || $is_patch) {
-	# Patching was successful. Parse new SymbolFile and return it
-	my %opts;
-	$opts{file} = $patchedfn;
-	$opts{arch} = $patcharch if defined $patcharch;
-	$symfile = Debian::PkgKde::SymbolsHelper::SymbolFile->new(%opts);
-    }
-
-    unlink($patchedfn);
-    return $symfile;
-}
-
diff --git a/symbolshelper/Debian/PkgKde/SymbolsHelper/Patching.pm b/symbolshelper/Debian/PkgKde/SymbolsHelper/Patching.pm
new file mode 100644
index 0000000..2b8b3c4
--- /dev/null
+++ b/symbolshelper/Debian/PkgKde/SymbolsHelper/Patching.pm
@@ -0,0 +1,266 @@
+# Copyright (C) 2008-2010 Modestas Vainius <modax at debian.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>
+
+package Debian::PkgKde::SymbolsHelper::Patching;
+
+use strict;
+use warnings;
+use base 'Exporter';
+
+use Dpkg::ErrorHandling;
+
+our @EXPORT = qw(parse_patches_from_handle parse_patches_from_file);
+
+sub parse_patches_from_handle {
+    my ($fh) = @_;
+    my $reparse_line;
+    my @patches;
+    my $patch;
+
+    while ($reparse_line || ($_ = <$fh>)) {
+	$reparse_line = 0;
+	if (defined $patch) {
+	    if ($patch->has_header()) {
+		if (m/^(?:[+ -]|@@ )/) {
+		    # Patch continues
+		    $patch->append_line($_);
+		} else {
+		    # Patch ended
+		    if ($patch->complete()) {
+			push @patches, $patch;
+		    } else {
+			warning("patch: '".$patch->get_name()."' is invalid");
+		    }
+		    $patch = undef;
+		    $reparse_line = 1;
+		    next;
+		}
+	    } elsif (defined $patch->{source}) {
+		if (m/^[+]{3}\s+(\S+)/) {
+		    # Found the patch header portion
+		    $patch->set_target($1);
+		} else {
+		    $patch = undef;
+		    $reparse_line = 1;
+		}
+	    }
+	} elsif (m/^[-]{3}\s+(\S+)(?:\s+\(([^_]+)_([^_]+)_([^_]+)\))?/) {
+	    $patch = Debian::PkgKde::SymbolsHelper::Patch->new();
+	    $patch->set_source($1);
+	    $patch->set_info($2, $3, $4);
+	}
+    }
+    if (defined $patch && $patch->complete()) {
+	push @patches, $patch;
+    } else {
+	warning("patch: '".$patch->get_name()."' is invalid");
+    }
+    return @patches;
+}
+
+sub parse_patches_from_file {
+    my ($filename) = @_;
+    open(my $fh, "<", $filename) or error("unable to open patch file '$filename'");
+    my @ret = parse_patches_from_handle($fh);
+    close $fh;
+    return @ret;
+}
+
+package Debian::PkgKde::SymbolsHelper::Patch;
+
+use strict;
+use warnings;
+
+use Dpkg::ErrorHandling;
+use Dpkg::IPC;
+
+sub new {
+    my $class = shift;
+    return bless {
+	file => undef,
+	source => undef,
+	target => undef,
+	package => undef,
+	version => undef,
+	arch => undef,
+	patch => undef,
+	hunk_minus => 0,
+	hunk_plus => 0,
+    }, $class;
+}
+
+sub set_source {
+    my ($self, $srcfile) = @_;
+    $self->{source} = $srcfile;
+}
+
+sub set_info {
+    my ($self, $package, $version, $arch) = @_;
+    $self->{package} = $package;
+    $self->{version} = $version;
+    $self->{arch} = $arch;
+}
+
+sub get_info {
+    my $self = shift;
+    return (
+	package => $self->{package},
+	version => $self->{version},
+	arch => $self->{arch},
+    );
+}
+
+sub has_info {
+    my $self = shift;
+    return defined $self->{package};
+}
+
+sub set_target {
+    my ($self, $target) = @_;
+    $self->{target} = $target;
+}
+
+sub has_header {
+    my $self = shift;
+    return defined $self->{source} && defined $self->{target};
+}
+
+sub get_name {
+    my $self = shift;
+    if ($self->{source}) {
+	if ($self->has_info()) {
+	    return sprintf("%s_%s_%s (%s)", $self->{package},
+		$self->{version}, $self->{arch}, $self->{source});
+	} else {
+	    return sprintf("--- %s +++ %s", $self->{source}, $self->{target});
+	}
+    } else {
+	return "<empty patch>";
+    }
+}
+
+sub is_valid {
+    my $self = shift;
+    return (defined $self->{hunk_minus} &&
+	$self->{hunk_minus} + $self->{hunk_plus} == 0);
+}
+
+sub open_patch_fh {
+    my ($self, $mode) = @_;
+    my $patch = $self->{patch};
+    if (!defined $patch) {
+	my $var;
+	$patch = $self->{patch} = \$var;
+    }
+    open(my $fh, $mode, $patch)
+	or systemerr("unable to open in-memory patch file");
+    return $fh;
+}
+
+sub append_line {
+    my ($self, $line) = @_;
+    my $fh = $self->{fh};
+    unless (defined $fh) {
+	$fh = $self->open_patch_fh(">");
+	$self->{fh} = $fh;
+    }
+    if (defined $self->{hunk_minus}) {
+	if ($line =~ /^@@\s*-\d+,(\d+)\s+[+]\d+,(\d+)\s*@@/) {
+	    if ($self->{hunk_minus} + $self->{hunk_plus} == 0) {
+		$self->{hunk_minus} = $1;
+		$self->{hunk_plus} = $2;
+	    } else {
+		# Bogus patch
+		$self->{hunk_minus} = undef;
+		$self->{hunk_plus} = undef;
+	    }
+	} elsif ($line =~ /^-/) {
+	    $self->{hunk_minus}--;
+	} elsif ($line =~ /^\+/) {
+	    $self->{hunk_plus}--;
+	} elsif ($line =~ /^ /) {
+	    $self->{hunk_minus}--;
+	    $self->{hunk_plus}--;
+	} else {
+	    warning("patch ignored. Invalid patch line: $line");
+	    $self->{hunk_minus} = undef;
+	    $self->{hunk_plus} = undef;
+	}
+    }
+    print $fh $line;
+}
+
+sub complete {
+    my $self = shift;
+    close $self->{fh};
+    delete $self->{fh};
+    return $self->is_valid();
+}
+
+sub dump {
+    my ($self, $outfh, $filename) = @_;
+    $filename = $self->{target} unless $filename;
+
+    print $outfh "--- ", $filename, "\n";
+    print $outfh "+++ ", $filename, "\n";
+
+    my $infh = $self->open_patch_fh("<");
+    while (<$infh>) {
+	print $outfh $_;
+    }
+    close $infh;
+}
+
+sub apply {
+    my ($self, $filename) = @_;
+
+    my $outfile = File::Temp->new(TEMPLATE => "${filename}_patch.out.XXXXXX");
+    my $to_patch_process;
+    my $pid = fork_and_exec(exec => [ "patch", "--posix", "--force",
+                                      "-r-", "-p0" ],
+                            from_pipe => \$to_patch_process,
+                            to_handle => $outfile,
+                            error_to_handle => $outfile,
+                            wait_child => 0
+    );
+    my $ret = $self->dump($to_patch_process, $filename);
+    close $to_patch_process;
+    wait_child($pid, nocheck => 1);
+    $ret &&= !$?;
+    if ($ret) {
+	$self->{applied} = $filename;
+    } else {
+	open(my $outputfd, "<", $outfile->filename)
+	    or syserr("unable to reopen temporary file");
+	my $output;
+	while (<$outputfd>) {
+	    $output .= $_;
+	}
+	close $outputfd;
+	chop $output;
+	$self->{apply_output} = $output;
+    }
+    return $ret;
+}
+
+sub is_applied {
+    my $self = shift;
+    return $self->{applied};
+}
+
+sub get_apply_output {
+    my $self = shift;
+    return $self->{apply_output};
+}

-- 
Debian Qt/KDE packaging tools



More information about the pkg-kde-commits mailing list