[Rt-commit] r4938 - in rtir/branches/1.9-EXPERIMENTAL: . inc/Module/Install

ruz at bestpractical.com ruz at bestpractical.com
Mon Apr 3 08:57:33 EDT 2006


Author: ruz
Date: Mon Apr  3 08:57:32 2006
New Revision: 4938

Added:
   rtir/branches/1.9-EXPERIMENTAL/inc/Module/Install/Substitute.pm
Modified:
   rtir/branches/1.9-EXPERIMENTAL/   (props changed)
   rtir/branches/1.9-EXPERIMENTAL/Makefile.PL
   rtir/branches/1.9-EXPERIMENTAL/t/rtir-test.pl

Log:
 r1226 at cubic-pc:  cubic | 2006-04-03 17:05:01 +0400
 * use M::I::Substitute to get rid of RTHOME in tests


Modified: rtir/branches/1.9-EXPERIMENTAL/Makefile.PL
==============================================================================
--- rtir/branches/1.9-EXPERIMENTAL/Makefile.PL	(original)
+++ rtir/branches/1.9-EXPERIMENTAL/Makefile.PL	Mon Apr  3 08:57:32 2006
@@ -10,6 +10,15 @@
 requires('Business::Hours');
 requires('Business::SLA');
 requires('Net::Whois::RIPE');
-&auto_install;
+auto_install();
 
-&WriteAll;
+my ($lp) = $INC{'RT.pm'} =~ /^(.*)[\\\/]/;
+my $lib_path = join( ' ', "$RT::LocalPath/lib", $lp );
+
+substitute( {
+        RT_LIB_PATH => $lib_path,
+    },
+    't/rtir-test.pl'
+);
+
+WriteAll();

Added: rtir/branches/1.9-EXPERIMENTAL/inc/Module/Install/Substitute.pm
==============================================================================
--- (empty file)
+++ rtir/branches/1.9-EXPERIMENTAL/inc/Module/Install/Substitute.pm	Mon Apr  3 08:57:32 2006
@@ -0,0 +1,128 @@
+#line 1
+package Module::Install::Substitute;
+
+use vars qw(@ISA);
+use Module::Install::Base; @ISA = qw(Module::Install::Base);
+
+use strict;
+use warnings;
+
+$Module::Install::Substitute::VERSION = '0.02';
+
+require File::Temp;
+require File::Spec;
+require Cwd;
+
+#line 64
+
+sub substitute
+{
+	my $self = shift;
+	$self->{__subst} = shift;
+	$self->{__option} = {};
+	if( UNIVERSAL::isa( $_[0], 'HASH' ) ) {
+		my $opts = shift;
+		while( my ($k,$v) = each( %$opts ) ) {
+			$self->{__option}->{ lc( $k ) } = $v || '';
+		}
+	}
+	$self->_parse_options;
+
+	my @file = @_;
+	foreach my $f (@file) {
+		$self->_rewrite_file( $f );
+	}
+
+	return;
+}
+
+sub _parse_options
+{
+	my $self = shift;
+	my $cwd = Cwd::getcwd();
+	foreach my $t ( qw(from to) ) {
+        $self->{__option}->{$t} = $cwd unless $self->{__option}->{$t};
+		my $d = $self->{__option}->{$t};
+		die "Couldn't read directory '$d'" unless -d $d && -r _;
+	}
+}
+
+sub _rewrite_file
+{
+	my ($self, $file) = @_;
+	my $source = File::Spec->catfile( $self->{__option}{from}, $file );
+	$source .= $self->{__option}{sufix} if $self->{__option}{sufix};
+	unless( -f $source && -r _ ) {
+		print STDERR "Couldn't find file '$source'\n";
+		return;
+	}
+	my $dest = File::Spec->catfile( $self->{__option}{to}, $file );
+	return $self->__rewrite_file( $source, $dest );
+}
+
+sub __rewrite_file
+{
+	my ($self, $source, $dest) = @_;
+
+	my $mode = (stat($source))[2];
+
+	open my $sfh, "<$source" or die "Couldn't open '$source' for read";
+	print "Open input '$source' file for substitution\n";
+
+	my ($tmpfh, $tmpfname) = File::Temp::tempfile('mi-subst-XXXX', UNLINK => 1);
+	$self->__process_streams( $sfh, $tmpfh, ($source eq $dest)? 1: 0 );
+	close $sfh;
+
+	seek $tmpfh, 0, 0 or die "Couldn't seek in tmp file";
+
+	open my $dfh, ">$dest" or die "Couldn't open '$dest' for write";
+	print "Open output '$dest' file for substitution\n";
+
+	while( <$tmpfh> ) {
+		print $dfh $_;
+	}
+	close $dfh;
+	chmod $mode, $dest or "Couldn't change mode on '$dest'";
+}
+
+sub __process_streams
+{
+	my ($self, $in, $out, $replace) = @_;
+	
+	my @queue = ();
+	my $subst = $self->{'__subst'};
+	my $re_subst = join('|', map {"\Q$_"} keys %{ $subst } );
+
+	while( my $str = <$in> ) {
+		if( $str =~ /^###\s*(before|replace|after)\: ?(.*)$/s ) {
+			my ($action, $nstr) = ($1,$2);
+			$nstr =~ s/\@($re_subst)\@/$subst->{$1}/ge;
+
+			$action = 'before' if !$replace && $action eq 'replace';
+			if( $action eq 'before' ) {
+				die "no line before 'before' action" unless @queue;
+				# overwrite prev line;
+				pop @queue;
+				push @queue, $nstr;
+				push @queue, $str;
+			} elsif( $action eq 'replace' ) {
+				push @queue, $nstr;
+			} elsif( $action eq 'after' ) {
+				push @queue, $str;
+				push @queue, $nstr;
+				# skip one line;
+				<$in>;
+			}
+		} else {
+			push @queue, $str;
+		}
+		while( @queue > 3 ) {
+			print $out shift(@queue);
+		}
+	}
+	while( scalar @queue ) {
+		print $out shift(@queue);
+	}
+}
+
+1;

Modified: rtir/branches/1.9-EXPERIMENTAL/t/rtir-test.pl
==============================================================================
--- rtir/branches/1.9-EXPERIMENTAL/t/rtir-test.pl	(original)
+++ rtir/branches/1.9-EXPERIMENTAL/t/rtir-test.pl	Mon Apr  3 08:57:32 2006
@@ -12,14 +12,8 @@
 use Test::WWW::Mechanize;
 use HTTP::Cookies;
 
-BEGIN {
-  unless ($ENV{RTHOME}) {
-    die "\n\nYou must set the RTHOME environment variable to the root of your RT install.\n\n";
-  }
-}
-
-use lib "$ENV{RTHOME}/lib";
-use lib "$ENV{RTHOME}/local/lib";
+### after: use lib qw(@RT_LIB_PATH@);
+use lib qw(/opt/rt3/local/lib /opt/rt3/lib);
 
 use RT;
 ok(RT::LoadConfig);


More information about the Rt-commit mailing list