SPECS: sed_to_patch.pl (NEW) - i'm so lazy :( let's automatize pat...
sparky
sparky at pld-linux.org
Wed Aug 2 01:35:35 CEST 2006
Author: sparky Date: Tue Aug 1 23:35:35 2006 GMT
Module: SPECS Tag: HEAD
---- Log message:
- i'm so lazy :( let's automatize patch generation from sed's - but no comments !
---- Files affected:
SPECS:
sed_to_patch.pl (NONE -> 1.1) (NEW)
---- Diffs:
================================================================
Index: SPECS/sed_to_patch.pl
diff -u /dev/null SPECS/sed_to_patch.pl:1.1
--- /dev/null Wed Aug 2 01:35:35 2006
+++ SPECS/sed_to_patch.pl Wed Aug 2 01:35:30 2006
@@ -0,0 +1,274 @@
+#!/usr/bin/perl
+#
+# Licensed under GPL
+# (c) 2006 PLD Linux Distribution
+#
+# TODO:
+# - comments
+# - support cvs branches
+#
+use strict;
+use warnings;
+
+my @patches;
+
+
+my $spec = shift @ARGV or die "No spec file specified\n";
+my $patch_name = shift @ARGV || "";
+(my $pkgname = $spec) =~ s/\.spec$//;
+
+REPEAT:
+my @seds = ();
+open F_IN, $spec or die "Can't open $spec: $!\n";
+
+while (<F_IN> !~ /\%prep/) {};
+while (<F_IN>) {
+ chomp;
+ last if $_ eq '%build';
+ $_ .= <F_IN> while s/\\$//;
+ push @seds, $_ if /^sed.*\s-i\s/;
+}
+close F_IN;
+
+die "No sed's found\n" unless @seds;
+
+sub dml {
+ my $out = "Patch name: <input id=name caption=\"${$_[2]}\"><br>\n";
+ my $n = 0;
+ foreach my $l (@{$_[0]}) {
+ $_ = $l;
+ s/\s+/ /g;
+ $out .= "<check id=$n> ";
+ $n++;
+ # split line
+ while (s/^(.{60})//) {
+ $out .= $1 . "<br>\n ";
+ }
+ $out .= $_ . "<br>\n";
+ }
+ $out .= "<button>";
+
+ open DML_IN, "-|", "dml", "-t", "sed to patch", "$out";
+ while (<DML_IN>) {
+ ${$_[2]} = $1 if /^name="(.*)"$/;
+ push @{$_[1]}, $_[0]->[$1] if /^([0-9]+)="yes"/;
+ }
+ close DML_IN || die "child died\n";
+ ${$_[2]} =~ s/^\s+//;
+ ${$_[2]} =~ s/\s+$//;
+ ${$_[2]} =~ s/\s+/_/g;
+}
+
+my @outseds = ();
+
+do {
+ dml(\@seds, \@outseds, \$patch_name);
+} while (not length $patch_name or not @outseds);
+
+die "$pkgname-$patch_name.patch already exists"
+ if -r "../SOURCES/$pkgname-$patch_name.patch";
+
+open F_IN, $spec or die;
+open F_OUT, "> sed_to_patch.pl.spec" or
+ die "Can't create temporary file: $!\n";
+while (<F_IN>) {
+ print F_OUT $_;
+ last if /\%prep/;
+}
+while (<F_IN>) {
+ chomp;
+ die "Can't find sed line\n" if $_ eq '%build';
+ $_ .= <F_IN> while s/\\$//;
+ if (/^sed.*\s-i\s/) {
+ last if ($_ eq $outseds[0]);
+ }
+ print F_OUT $_."\n";
+}
+print F_OUT "echo \$PWD > $ENV{PWD}/sed_to_patch.srcdir\nexit 1\n";
+print F_OUT $_ while (<F_IN>);
+close F_IN;
+close F_OUT;
+
+unlink "sed_to_patch.srcdir";
+system( qw(./builder -bp sed_to_patch.pl.spec) );
+open F_IN, "sed_to_patch.srcdir" or die;
+my $builddir = <F_IN>;
+close F_IN;
+die unless length $builddir;
+chomp $builddir;
+
+die "$pkgname-$patch_name.patch already exists"
+ if -r "../SOURCES/$pkgname-$patch_name.patch";
+
+system(qw(rm -rf), $builddir.".".$patch_name);
+my @cp = (qw(cp -a), $builddir, $builddir.".".$patch_name);
+{
+ local $" = "|";
+ system(@cp);
+ die "Can't copy(@cp): $!\n" if $?;
+}
+
+my $specsdir = $ENV{PWD};
+
+chdir($builddir.".".$patch_name) or die;
+
+foreach my $sed (@outseds) {
+ print "exe: $sed\n";
+ system("$sed");
+ die "sed failed" if $?;
+}
+
+(my $rpmBUILD = $builddir) =~ s#/BUILD/.*#/BUILD#;
+chdir $rpmBUILD;
+(my $srcdir = $builddir) =~ s#.*/BUILD/+##;
+system("diff -Nur '$srcdir' '$srcdir.$patch_name' " .
+ ">> $specsdir/../SOURCES/$pkgname-$patch_name.patch");
+
+chdir($specsdir);
+
+open F_IN, $spec or die;
+my @spec = <F_IN>;
+close F_IN;
+
+open F_OUT, ">", $spec.".new";
+my $patch_num = 0;
+while ($_ = shift @spec) {
+ $patch_num = ($1+1) if /^Patch([0-9]+):/;
+ if (/^(URL:|BuildRequires:|Requires|Provides:|Obsoletes:|BuildRoot:)/) {
+ unshift @spec, $_;
+ last;
+ }
+ die if /^\%description/;
+ print F_OUT $_;
+}
+print F_OUT "Patch$patch_num:\t";
+print F_OUT "\t" if $patch_num < 10;
+print F_OUT "\%{name}-$patch_name.patch\n";
+
+while ($_ = shift @spec) {
+ print F_OUT $_;
+ last if /\%prep/;
+}
+
+# find last %patch, or %setup;
+my $last_patch = "";
+foreach (@spec) {
+ $last_patch = $_ if /%(setup|patch)/;
+ last if /\%build/;
+}
+die unless length $last_patch;
+
+while ($_ = shift @spec) {
+ if ($_ eq $last_patch) {
+ print F_OUT $_;
+ $_ = "\%patch$patch_num -p1\n";
+ } elsif (/^sed/) {
+ my @sed_command = ($_);
+ while (s/\\$//) {
+ my $l = shift @spec;
+ push @sed_command, $l;
+ $_ .= $l;
+ }
+ chomp;
+ my $found = 0;
+ foreach my $s (@outseds) {
+ $found = 1 if $_ eq $s;
+ }
+ unless ($found) {
+ foreach (@sed_command) {
+ print F_OUT $_;
+ }
+ }
+ next;
+ } elsif (/\%build/) {
+ print F_OUT $_;
+ last;
+ }
+ print F_OUT $_;
+}
+
+while ($_ = shift @spec) {
+ print F_OUT $_;
+}
+close F_OUT;
+unlink "sed_to_patch.pl.spec";
+
+sub diffcol {
+ $_ = $_[0];
+ chomp;
+ s//[44m^[[49m/g;
+ s//[44m^G[49m/g;
+ s/^(Index:|diff|---|\+\+\+) /[32m$1/;
+ s/^@@ /[33m@@ /;
+ s/^-/[35m-/;
+ s/^\+/[36m+/;
+ s/
/[44m^M[49m/g;
+ #s/ / /g;
+ s/(\S)(\s+)$/$1[41m$2[49m/;
+ s/$/[0m/;
+ print $_."\n";
+ }
+
+open PATCH, "../SOURCES/$pkgname-$patch_name.patch";
+while (<PATCH>) {
+ diffcol($_);
+}
+close PATCH;
+open PATCH, "diff -u $spec $spec.new |";
+while (<PATCH>) {
+ diffcol($_);
+}
+close PATCH;
+my $ans;
+do {
+ print "\n\nOK [yn] ?";
+ $ans = <STDIN>;
+ chomp $ans;
+} until ($ans =~ /^[yYnN]$/);
+exit if ($ans =~ /[nN]/);
+rename($spec, $spec.".orig-s2p");
+rename($spec.".new", $spec);
+
+push @patches, "$pkgname-$patch_name.patch";
+if (@seds > @outseds) {
+ do {
+ print "\nchange more [yn] ?";
+ $ans = <STDIN>;
+ chomp $ans;
+ } until ($ans =~ /^[yYnN]$/);
+ goto REPEAT if ($ans =~ /[yY]/);
+}
+
+do {
+ print "\nedit spec [yn] ?";
+ $ans = <STDIN>;
+ chomp $ans;
+} until ($ans =~ /^[yYnN]$/);
+system("vim", $spec) if ($ans =~ /[yY]/);
+
+do {
+ print "\nadapter [yn] ?";
+ $ans = <STDIN>;
+ chomp $ans;
+} until ($ans =~ /^[yYnN]$/);
+system("./adapter", $spec) if ($ans =~ /[yY]/);
+
+open PATCH, "cvs diff -u $spec |";
+while (<PATCH>) {
+ diffcol($_);
+}
+close PATCH;
+
+do {
+ print "\n\nsend [yn] ?";
+ $ans = <STDIN>;
+ chomp $ans;
+} until ($ans =~ /^[yYnN]$/);
+exit if ($ans =~ /[nN]/);
+
+chdir("../SOURCES");
+$" = " ";
+system("cvs add @patches");
+system("cvs ci @patches");
+chdir($specsdir);
+system("cvs ci $spec");
================================================================
More information about the pld-cvs-commit
mailing list