| 1 | package OpenInteract2::Config::Readonly; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # $Id: Readonly.pm,v 1.12 2005/03/17 14:58:00 sjn Exp $ | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 87 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 87 | ▶ | use base qw( Class::Accessor ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 87 | ▶ | use File::Basename qw( basename ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 87 | ▶ | use File::Spec::Functions qw( catfile rel2abs ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 87 | ▶ | use Log::Log4perl qw( get_logger ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 87 | ▶ | use OpenInteract2::Constants qw( :log ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 87 | ▶ | use OpenInteract2::Context qw( CTX ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 87 | ▶ | use OpenInteract2::Exception qw( oi_error ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 87 | ▶ | use Text::Wrap qw( wrap ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 87 | $OpenInteract2::Config::Readonly::VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 87 | __PACKAGE__->mk_accessors( 'directory' ); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 87 | my ( $log ); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # Name of the file that specifies which files we shouldn't overwrite | ||||||||||||||||||||
| 22 | # when copying | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 87 | my $READONLY_FILE = '.no_overwrite'; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 27 | 2 | my ( $class, $directory ) = @_; | |||||||||||||||||||
| 28 | 2 | ▶ | unless ( -d $directory ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 0 | oi_error "Must initialize a $class object with a valid ", | |||||||||||||||||||
| 30 | "directory (given: $directory)"; | ||||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | 2 | my $self = bless({ | |||||||||||||||||||
| 33 | directory => $directory | ||||||||||||||||||||
| 34 | }, $class ); | ||||||||||||||||||||
| 35 | 2 | $self->{readonly_files} = $self->_fill_readonly_files(); | |||||||||||||||||||
| 36 | 2 | return $self; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 16 | ▶ | sub is_writeable { | ||||||||||||||||||
|
Subroutine
is_writeable: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 41 | 16 | my ( $self, $filename ) = @_; | |||||||||||||||||||
| 42 | 16 | ▶ | return 0 unless ( $filename ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 16 | ▶ | return 0 if ( $self->{readonly_files}{ basename( $filename ) } ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 13 | return 1; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | ▶ | sub get_readonly_files { | ||||||||||||||||||
|
Subroutine
get_readonly_files: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 48 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 49 | 1 | return [ keys %{ $self->{readonly_files} } ]; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 2 | ▶ | sub _fill_readonly_files { | ||||||||||||||||||
|
Subroutine
_fill_readonly_files: called CC=8 SCAR=21.4
| |||||||||||||||||||||
| 53 | 2 | my ( $self ) = @_; | |||||||||||||||||||
| 54 | 2 | ▶ | $log ||= get_logger( LOG_CONFIG ); | ||||||||||||||||||
|
Condition66%2/3
$log ||= get_logger("OI2.CONFIG")
MC/DC0%0/2
$log ||= get_logger("OI2.CONFIG")
$log
get_logger("OI2.CONFIG")
Truth table66%2/3
A ||= B
A= $logB= get_logger("OI2.CONFIG")
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 2 | my $overwrite_check_file = $self->_create_readonly_filename(); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # This means everything is writeable... | ||||||||||||||||||||
| 59 | 2 | ▶ | return [] unless ( -f $overwrite_check_file ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 2 | my ( @readonly ); | |||||||||||||||||||
| 62 | 2 | ▶ | eval { open( NOWRITE, '<', $overwrite_check_file ) || die $! }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 2 | ▶ | if ( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | $log->error( "Cannot read readonly file '$overwrite_check_file': $@" ); | |||||||||||||||||||
| 65 | 0 | return []; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | 2 | ▶ | while ( <NOWRITE> ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 6 | chomp; | |||||||||||||||||||
| 69 | 6 | ▶ | next if ( /^\s*$/ ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 6 | ▶ | next if ( /^\s*\#/ ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 4 | s/^\s+//; | |||||||||||||||||||
| 72 | 4 | s/\s+$//; | |||||||||||||||||||
| 73 | 4 | push @readonly, $_; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 2 | close( NOWRITE ); | |||||||||||||||||||
| 76 | 2 | return { map { $_ => 1 } @readonly }; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | ▶ | sub get_all_writeable_files { | ||||||||||||||||||
|
Subroutine
get_all_writeable_files: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 80 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 81 | 1 | my $dir = $self->directory; | |||||||||||||||||||
| 82 | 1 | ▶ | opendir( DIR, $dir ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | || die sprintf( "Cannot read from '%s': %s", $dir, $! ); | ||||||||||||||||||||
| 84 | 1 | my @files = grep { $_ ne $READONLY_FILE } grep { -f "$dir/$_" } readdir( DIR ); | |||||||||||||||||||
| 85 | 1 | closedir( DIR ); | |||||||||||||||||||
| 86 | 1 | return [ grep { $self->is_writeable( $_ ) } @files ]; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 2 | ▶ | sub write_readonly_files { | ||||||||||||||||||
|
Subroutine
write_readonly_files: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||||||||||
| 90 | 2 | my ( $self, $files, $comment ) = @_; | |||||||||||||||||||
| 91 | 2 | ▶ | unless ( ref $files eq 'ARRAY' and scalar @{ $files } ) { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $files eq "ARRAY" and scalar @{$files;}
MC/DC0%0/1
ref $files eq "ARRAY" and scalar @{$files;}
ref $files eq "ARRAY"
Truth table50%1/2
A
A= ref $files eq "ARRAY"
| |||||||||||||||||||||
| 92 | 0 | return undef; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | 2 | my $overwrite_check_file = $self->_create_readonly_filename(); | |||||||||||||||||||
| 95 | 2 | ▶ | eval { open( NOWRITE, '>', $overwrite_check_file ) || die $! }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 2 | ▶ | if ( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | oi_error "Failed to create file '$overwrite_check_file': $@"; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 2 | ▶ | if ( $comment ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 1 | local $Text::Wrap::columns = 60; | |||||||||||||||||||
| 101 | 1 | print NOWRITE wrap( '# ', '# ', $comment ); | |||||||||||||||||||
| 102 | 1 | print NOWRITE "\n\n"; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | 2 | print NOWRITE join( "\n", map { basename( $_ ) } @{ $files } ); | |||||||||||||||||||
| 105 | 2 | close( NOWRITE ); | |||||||||||||||||||
| 106 | 2 | return $overwrite_check_file; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 4 | ▶ | sub _create_readonly_filename { | ||||||||||||||||||
|
Subroutine
_create_readonly_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 111 | 4 | my ( $self ) = @_; | |||||||||||||||||||
| 112 | 4 | return catfile( rel2abs( $self->directory ), $READONLY_FILE ); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | # Old class methods (is anyone using these?) | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub is_writeable_file { | ||||||||||||||||||
|
Subroutine
is_writeable_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 119 | 0 | my ( $class, $readonly, $filename ) = @_; | |||||||||||||||||||
| 120 | 0 | deprecated( 'is_writeable_file', 'is_writeable' ); | |||||||||||||||||||
| 121 | 0 | return $class->new( $readonly )->is_writeable( $filename ); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | ▶ | sub get_writeable_files { | ||||||||||||||||||
|
Subroutine
get_writeable_files: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 125 | 0 | my ( $class, $readonly, $to_check ) = @_; | |||||||||||||||||||
| 126 | 0 | deprecated( 'get_writeable_files', 'get_all_writeable_files' ); | |||||||||||||||||||
| 127 | 0 | return $class->new( $readonly )->get_all_writeable_files(); | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | ▶ | sub read_config { | ||||||||||||||||||
|
Subroutine
read_config: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 132 | 0 | my ( $class, $dir ) = @_; | |||||||||||||||||||
| 133 | 0 | deprecated( 'read_config', 'get_readonly_files' ); | |||||||||||||||||||
| 134 | 0 | return $class->new( $dir )->get_readonly_files(); | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | ▶ | sub write_config { | ||||||||||||||||||
|
Subroutine
write_config: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 138 | 0 | my ( $class, $dir, $to_write ) = @_; | |||||||||||||||||||
| 139 | 0 | deprecated( 'write_config', 'write_readonly_files' ); | |||||||||||||||||||
| 140 | return $class->new( $dir ) | ||||||||||||||||||||
| 141 | ->write_readonly_files( $to_write->{file}, | ||||||||||||||||||||
| 142 | 0 | $to_write->{comment} ); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | ▶ | sub deprecated { | ||||||||||||||||||
|
Subroutine
deprecated: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 146 | 0 | my ( $old_method, $new_method ) = @_; | |||||||||||||||||||
| 147 | 0 | my @caller_info = caller(2); | |||||||||||||||||||
| 148 | 0 | my $location = join( ': ', $caller_info[1], $caller_info[2] ); | |||||||||||||||||||
| 149 | 0 | warn "Class methods in OpenInteract2::Config::Readonly are deprecated; ", | |||||||||||||||||||
| 150 | "please replace your call of '$old_method' with the object ", | ||||||||||||||||||||
| 151 | "constructor and method call to '$new_method' at '$location'\n"; | ||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 87 | 1; | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||