| 1 | package File::Monitor::Simple; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use File::Find::Rule; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use File::Modified; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use vars '$VERSION'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 1 | $VERSION = '0.99_1'; | |||||||||||
| 13 | |||||||||||||
| 14 | 1 | __PACKAGE__->mk_accessors( | |||||||||||
| 15 | qw/directory | ||||||||||||
| 16 | modified | ||||||||||||
| 17 | regex | ||||||||||||
| 18 | watch_list | ||||||||||||
| 19 | / | ||||||||||||
| 20 | ); | ||||||||||||
| 21 | |||||||||||||
| 22 | # directory : scalar location to monitor | ||||||||||||
| 23 | # modified : File::Modified object with current state | ||||||||||||
| 24 | # regex : File pattern to monitor. Really a string a not "qr" | ||||||||||||
| 25 | # watch_list : hashref of monitored files. Keys are names. Values are "1" | ||||||||||||
| 26 | |||||||||||||
| 27 | |||||||||||||
| 28 | sub new { | ||||||||||||
| 29 | 1 | ▶ | my ( $class, %args ) = @_; | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 30 | |||||||||||||
| 31 | 1 | my $self = {%args}; | |||||||||||
| 32 | |||||||||||||
| 33 | 1 | bless $self, $class; | |||||||||||
| 34 | |||||||||||||
| 35 | 1 | $self->_init; | |||||||||||
| 36 | |||||||||||||
| 37 | 1 | return $self; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | sub _init { | ||||||||||||
| 41 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | |||||||||||||
| 43 | 3 | my $watch_list = $self->_index_directory; | |||||||||||
| 44 | 3 | $self->watch_list($watch_list); | |||||||||||
| 45 | |||||||||||||
| 46 | $self->modified( | ||||||||||||
| 47 | File::Modified->new( | ||||||||||||
| 48 | # mtime works on directories, while 'MD5' doesn't | ||||||||||||
| 49 | method => 'mtime', | ||||||||||||
| 50 | 3 | files => [ keys %{$watch_list} ], | |||||||||||
| 51 | ) | ||||||||||||
| 52 | ); | ||||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | sub watch { | ||||||||||||
| 56 | 8 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
watch: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||
| 57 | |||||||||||||
| 58 | 8 | my @changes; | |||||||||||
| 59 | my @changed_files; | ||||||||||||
| 60 | |||||||||||||
| 61 | 8 | eval { @changes = $self->modified->changed }; | |||||||||||
| 62 | 8 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | # warn "error: $@"; | ||||||||||||
| 64 | # File::Modified will die if a file is deleted. | ||||||||||||
| 65 | 1 | my ($deleted_file) = $@ =~ /stat '(.+)'/; | |||||||||||
| 66 | 1 | ▶ | push @changed_files, $deleted_file || 'unknown file'; | ||||||||||
|
Condition50%1/2
$deleted_file || 'unknown file'
MC/DC0%0/1
$deleted_file || 'unknown file'
$deleted_file
Truth table50%1/2
A
A= $deleted_file
| |||||||||||||
| 67 | |||||||||||||
| 68 | # re-scan to remove the deleted file | ||||||||||||
| 69 | 1 | $self->_init; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 8 | ▶ | if (@changes) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | # update all mtime information | ||||||||||||
| 74 | 1 | $self->modified->update; | |||||||||||
| 75 | |||||||||||||
| 76 | # check if any files were changed | ||||||||||||
| 77 | 1 | ▶ | @changed_files = grep { defined $_ && -f $_ } @changes; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | |||||||||||||
| 79 | # Check if only directories were changed. This means | ||||||||||||
| 80 | # a new file was created. | ||||||||||||
| 81 | 1 | ▶ | if (@changed_files) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | # We found some changed files. | ||||||||||||
| 83 | # Don't bother to look for added files, too. | ||||||||||||
| 84 | } | ||||||||||||
| 85 | else { | ||||||||||||
| 86 | |||||||||||||
| 87 | # look through the new list for new files | ||||||||||||
| 88 | 1 | my $old_watch = $self->watch_list; | |||||||||||
| 89 | |||||||||||||
| 90 | # re-index to find new files | ||||||||||||
| 91 | 1 | $self->_init; | |||||||||||
| 92 | |||||||||||||
| 93 | 1 | @changed_files = grep { !defined $old_watch->{$_} } keys %{ $self->watch_list }; | |||||||||||
| 94 | |||||||||||||
| 95 | 1 | ▶ | return unless @changed_files; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | } | ||||||||||||
| 97 | } | ||||||||||||
| 98 | else { | ||||||||||||
| 99 | # no changes found; | ||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 8 | return @changed_files; | |||||||||||
| 103 | |||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | # =begin private | ||||||||||||
| 107 | # | ||||||||||||
| 108 | # =head2 _index_directory() | ||||||||||||
| 109 | # | ||||||||||||
| 110 | # $self->_index_directory; | ||||||||||||
| 111 | # | ||||||||||||
| 112 | # Looks in $self->directory and compares all files to $self->regex. | ||||||||||||
| 113 | # Matched files are registered for monitoring. | ||||||||||||
| 114 | # | ||||||||||||
| 115 | # =end private | ||||||||||||
| 116 | # | ||||||||||||
| 117 | # =cut | ||||||||||||
| 118 | |||||||||||||
| 119 | sub _index_directory { | ||||||||||||
| 120 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_index_directory: called CC=4 SCAR=14.0
| |||||||||||||
| 121 | |||||||||||||
| 122 | 3 | ▶ | my $dir = $self->directory || die "No directory specified"; | ||||||||||
|
Condition50%1/2
$self->directory || die('No directory specified')
MC/DC0%0/1
$self->directory || die('No directory specified')
$self->directory
Truth table50%1/2
A
A= $self->directory
| |||||||||||||
| 123 | 3 | ▶ | my $regex = $self->regex || '\.pm$'; | ||||||||||
|
Condition50%1/2
$self->regex || '\\.pm$'
MC/DC0%0/1
$self->regex || '\\.pm$'
$self->regex
Truth table50%1/2
A
A= $self->regex
| |||||||||||||
| 124 | |||||||||||||
| 125 | # Find all the directories in this directory, as well as any files matching the regex | ||||||||||||
| 126 | 3 | my $iterator = File::Find::Rule->any( | |||||||||||
| 127 | File::Find::Rule->file->name(qr/$regex/), | ||||||||||||
| 128 | File::Find::Rule->directory, | ||||||||||||
| 129 | )->start( $dir ); | ||||||||||||
| 130 | |||||||||||||
| 131 | 3 | my %list; | |||||||||||
| 132 | 3 | ▶ | while ( my $match = $iterator->match ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 4 | $list{$match} = 1 | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 3 | return \%list; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 1 | 1; | |||||||||||
| 140 | __END__ | ||||||||||||