diff options
author | Zeus Panchenko <zeus@camb.us> | 2010-10-15 14:44:29 +0000 |
---|---|---|
committer | Zeus Panchenko <zeus@camb.us> | 2010-10-15 14:44:29 +0000 |
commit | dadfcb9f00e2a6a5ad8a1a5cd2fa42ea7da1d525 (patch) | |
tree | 7be9215a32411ea059a7fc67f985b5dac5844ba9 | |
parent | 9af0752f9971767f69d57ef822dc007de10fc15f (diff) | |
download | renrot-git-renrot_rfile.tar.gz renrot-git-renrot_rfile.tar.bz2 |
initial tree injectgit-renrot_rfile
git-svn-id: file:///svnroot/renrot/branches/renrot_rfile@594 fe2816f4-e837-0410-b10a-f608c9d244a1
-rwxr-xr-x | modules/renrot_dir.pm | 47 | ||||
-rwxr-xr-x | modules/renrot_msg.pm | 86 | ||||
-rwxr-xr-x | rfile | 83 |
3 files changed, 216 insertions, 0 deletions
diff --git a/modules/renrot_dir.pm b/modules/renrot_dir.pm new file mode 100755 index 0000000..701b9c4 --- /dev/null +++ b/modules/renrot_dir.pm @@ -0,0 +1,47 @@ +use strict; + +package renrot_dir; + +################################################### +# Usage : $a = new renrot_dir; +# Purpose : allocator and initializer +# Returns : initialized class +# Parameters : none +# Throws : no exceptions +# Comments : none +# See Also : n/a +sub new { + my ($pkg, $dir, $ext) = @_; + my @files = (); + return (bless {dir => $dir, + ext => $ext, + files => \@files}, $pkg); +} + +################################################### +# Usage : none +# Purpose : destructor +# Returns : none +# Parameters : none +# Throws : no exceptions +# Comments : none +# See Also : n/a +sub DESTROY { + my $obj = shift; + #print "\$obj->{", $obj->{dir}, "} has been destroied.\n"; +} + +################################################### +# Usage : $obj->filename($base, $ext) +# Purpose : full file name compilator: base . ext = baseext +# Returns : ful filename +# Parameters : 1. basename; 2. extention of ".ext" format +# Throws : no exceptions +# Comments : none +# See Also : n/a +sub file_name { + my ($obj, $base, $ext) = @_; + return $obj->{filename} = $base . $ext; +} + +1; diff --git a/modules/renrot_msg.pm b/modules/renrot_msg.pm new file mode 100755 index 0000000..2b16f88 --- /dev/null +++ b/modules/renrot_msg.pm @@ -0,0 +1,86 @@ +use strict; + +use Term::ANSIColor; + +package msg; + +sub new { + my $obj = shift; + my $use_color = shift; + my $quiet = shift; + my $verbose = shift; + my %colors = ( + debug => {value => 'green'}, + error => {value => 'magenta'}, + fatal => {value => 'red'}, + info => {value => 'bold'}, + process => {value => 'white'}, + warning => {value => 'cyan'}, + ); + return (bless {use_color => $use_color, + quiet => $quiet, + verbose => $verbose, + colors => \%colors, + }, $obj); + #$obj->Dbg(4,"msg class has been created.\n"); +} +# destructor +sub DESTROY { + my $obj = shift; + #$obj->Dbg(4,"msg class has been destroied.\n"); +} +# Prints colored message to STDERR +sub coloredprn { + my $obj = shift; + my $facility = shift; + if ($obj->{use_color} != 0) { + if (defined $facility and defined $obj->{colors}{$facility}) { + print STDERR Term::ANSIColor::colored [$obj->{colors}{$facility}{value}], @_; + return; + } + } + print STDERR @_; # fallback to normal print +} +# processing message +sub Proc { + my $obj = shift; + return if ($obj->{quiet} != 0); + + if ($obj->{use_color} != 0) { + if (defined $obj->{colors}{'process'}) { + print Term::ANSIColor::colored [$obj->{colors}{'process'}{value}], @_; + return; + } + } + print @_; # fallback to normal print +} +# information message +sub Info { + my $obj = shift; + $obj->coloredprn('info', @_); +} +# warning message +sub Warn { + my $obj = shift; + $obj->coloredprn('warning', "Warning: ", @_); +} +# error message +sub Err { + my $obj = shift; + $obj->coloredprn('error', "ERROR: ", @_); +} +# fatal message +sub Fatal { + my $obj = shift; + $obj->coloredprn('fatal', "FATAL: ", @_); +} +# debug message +sub Dbg { + my $obj = shift; + my $level = shift; + if ($obj->{verbose} >= $level) { + $obj->coloredprn('debug', "DEBUG[$level]: ", @_); + } +} + +1; @@ -0,0 +1,83 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use diagnostics; + +use Getopt::Long; + +my @dir = (); +my $ext = q{}; +my $verbose = 3; +my %cfgOpts = ('use color' => 1); +my $quiet = 0; + +my $get_opt = GetOptions ( + "dir|d=s" => \@dir, + "ext|e=s" => \$ext, + "v+" => \$verbose, + ); + +require "modules/renrot_dir.pm"; +require "modules/renrot_msg.pm"; + +my $msg = new msg($cfgOpts{'use color'},$quiet,$verbose); + +if (not $ext) { + $msg->Fatal("You have define EXT!\n"); + exit 1; +} elsif ($ext !~ m/^\..*$/) { + $msg->Dbg(4, "ext:\t",$ext,"\n"); + $ext = "." . $ext; +} + +if (not @dir) { + @dir = ("."); +} + +use File::Find; +use File::Basename; + +my %dir_st = (); + +###################################################################### +# Usage : pars_files +# Purpose : to prepare hash of objects with file details +# Returns : none +# Parameters : none +# Throws : no exceptions +# Comments : it use global variables to fill +# See Also : package renrot_dir +sub pars_files { + my $EXT = (fileparse($File::Find::name, $ext))[2]; + if ( -f $File::Find::name and $EXT eq $ext) { + if (not $dir_st{$File::Find::dir}) { + $dir_st{$File::Find::dir} = new renrot_dir(); + $dir_st{$File::Find::dir}->{dir} = $File::Find::dir; + $dir_st{$File::Find::dir}->{ext} = $EXT; + } + push @{$dir_st{$File::Find::dir}->{files}}, basename($_,$EXT); + } +} + +find \&pars_files, @dir; + +my @files = (); +foreach my $dir (sort keys %dir_st) { + $msg->Proc("\nDIR: ", $dir, "\n"); + for (my $i = 0,@files = @{$dir_st{$dir}->{files}}; $i < scalar @files; $i++) { + $msg->Proc("\tFILE: ", $files[$i], "\t\tEXT: ", + $dir_st{$dir}->{ext}, "\t FULLNAME: ", + $dir_st{$dir}->file_name($files[$i],$dir_st{$dir}->{ext}),"\n"); + #if ( $i == 0 ) { + # chdir $dir_st{$dir}->{dir}; + #} + #open(FH, $dir_st{$dir}->file_name($files[$i],$dir_st{$dir}->{ext})) +# or die "Can't open ",$dir_st{$dir}->file_name($files[$i],$dir_st{$dir}->{ext}),": $!"; + #while (<FH>) { + # $msg->Proc( $_,"\n"); + #} + #close(FH); + } + undef @files; +} |