[Rt-commit] r5262 - in RT-Extension-CommandByEmail: . t

ruz at bestpractical.com ruz at bestpractical.com
Fri May 19 13:56:47 EDT 2006


Author: ruz
Date: Fri May 19 13:56:46 2006
New Revision: 5262

Added:
   RT-Extension-CommandByEmail/inc/Module/Install/Substitute.pm
Modified:
   RT-Extension-CommandByEmail/Makefile.PL
   RT-Extension-CommandByEmail/t/utils.pl

Log:
* make tests RT path independed, use M::I::Substitute
* die if we couldn't find mailgate

Modified: RT-Extension-CommandByEmail/Makefile.PL
==============================================================================
--- RT-Extension-CommandByEmail/Makefile.PL	(original)
+++ RT-Extension-CommandByEmail/Makefile.PL	Fri May 19 13:56:46 2006
@@ -12,4 +12,19 @@
 # RT may be require it too, but put it here to be sure 
 requires('UNIVERSAL::require');
 
+my ($lp) = ($INC{'RT.pm'} =~ /^(.*)[\\\/]/);
+my $lib_path = join( ' ', "$RT::LocalPath/lib", $lp );
+my $sbin_path = $RT::SbinPath || "$RT::BasePath/sbin" || "/opt/rt3/sbin";
+my $bin_path = $RT::BinPath || "$RT::BasePath/bin" || "/opt/rt3/bin";
+
+substitute(
+    {
+        RT_LIB_PATH  => $lib_path,
+        RT_BIN_PATH  => $bin_path,
+        RT_SBIN_PATH => $sbin_path,
+    },
+    qw(t/utils.pl),
+);
+
+
 WriteAll();

Added: RT-Extension-CommandByEmail/inc/Module/Install/Substitute.pm
==============================================================================
--- (empty file)
+++ RT-Extension-CommandByEmail/inc/Module/Install/Substitute.pm	Fri May 19 13:56:46 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: RT-Extension-CommandByEmail/t/utils.pl
==============================================================================
--- RT-Extension-CommandByEmail/t/utils.pl	(original)
+++ RT-Extension-CommandByEmail/t/utils.pl	Fri May 19 13:56:46 2006
@@ -5,7 +5,7 @@
 
 
 BEGIN {
-### after:  push @INC, qw(@RT_LIB_PATH@);
+### after:     push @INC, qw(@RT_LIB_PATH@);
     push @INC, qw(/opt/rt3/local/lib /opt/rt3/lib);
 }
 
@@ -14,11 +14,13 @@
 
 use IPC::Open2;
 
-### after:  push @INC, qw(@RT_LIB_PATH@);
+### after: our $mailgate = '@RT_BIN_PATH@/rt-mailgate';
 our $mailgate = '/opt/rt3/bin/rt-mailgate';
 $mailgate .= ' --debug';
 $mailgate .= ' --url '. $RT::WebURL;
 
+die "Couldn't find mailgate ($mailgate) command" unless -f $mailgate;
+
 sub run_gate {
     my %args = (
         message => '',


More information about the Rt-commit mailing list