/[pxelator]/lib/PXElator/file.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/PXElator/file.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 291 - (hide annotations)
Wed Aug 26 08:59:50 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 1621 byte(s)
test and fix mkdir
1 dpavlin 271 package file;
2    
3     use File::Slurp;
4     use autodie;
5     use Carp qw/carp confess/;
6     use File::Path qw//;
7 dpavlin 290 use Data::Dump qw/dump/;
8 dpavlin 271
9 dpavlin 290 my $debug = 1;
10    
11 dpavlin 271 sub mkpath {
12     my $file = shift;
13 dpavlin 291 my $dir = $1 if $file =~ s{(^.+)/[^/]+}{$1};
14     if ( $dir && ! -d $dir ) {
15     carp "# mkdir $dir";
16     File::Path::mkpath $dir;
17     }
18 dpavlin 271 }
19    
20     sub append {
21     my ( $file, $content ) = @_;
22    
23     if ( ! -e $file ) {
24     mkpath $file;
25     write_file $file, $content;
26     my $size = -s $file;
27 dpavlin 290 carp "## append created $size bytes in $file";
28 dpavlin 271 return $size;
29     }
30    
31     my $on_disk = read_file $file;
32    
33 dpavlin 290 my $relaxed_content = $content;
34     $relaxed_content =~ s{\s+}{\\s+}gs;
35 dpavlin 271
36     if ( $on_disk !~ m{$relaxed_content} ) {
37    
38     # $content =~ s{^[\n\r]+}{\n}s;
39     # $content =~ s{[\n\r]*$}{\n}s;
40    
41 dpavlin 290 if ( $on_disk =~ s{([\s+]exit[\s\d]*)$}{\n$content\n$1}s ) {
42     # warn "# insert $file\n$on_disk";
43     write_file $file, $on_disk;
44     } else {
45     # warn "# append $file\n$content\n";
46     open($fh, '>>', $file);
47     print $fh $content;
48     close($fh);
49     }
50 dpavlin 271
51 dpavlin 290 carp "## append to $file";
52     return -s $file;
53 dpavlin 271 }
54     }
55    
56     sub change {
57     my ( $file, $from, $to ) = @_;
58    
59     my $content = read_file $file;
60     if ( $content =~ s{$from}{$to}s ) {
61     write_file $file, $content;
62 dpavlin 290 carp "## change $file $from => $to" if $debug;
63 dpavlin 271 return 1;
64     } elsif ( $content !~ m{$to}s ) {
65 dpavlin 290 confess "can't find $from to change into $to in $file in ",dump( $content );
66 dpavlin 271 }
67     }
68    
69 dpavlin 277 sub replace {
70     my ( $file, $content ) = @_;
71     mkpath $file;
72     write_file $file, $content;
73     }
74 dpavlin 271
75 dpavlin 290 sub copy_once {
76     my ( $from, $to ) = @_;
77 dpavlin 291 die "no destination" unless $to;
78 dpavlin 290 return if -e $to;
79     mkpath $to;
80 dpavlin 291 carp "# copy_once $from => $to";
81     write_file $to, read_file($from);
82 dpavlin 290 }
83    
84 dpavlin 271 1;

  ViewVC Help
Powered by ViewVC 1.1.26