| 1 | package CPAN::Repository::Role::File; | ||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:GETTY'; | |||||||||
| 3 | # ABSTRACT: Role for file functions | ||||||||||
| 4 | 3 | $CPAN::Repository::Role::File::VERSION = '0.009'; | |||||||||
| 5 | 3 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use File::Path qw( make_path ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use File::Spec::Functions ':ALL'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use IO::Zlib; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | requires qw( file_parts generate_content ); | |||||||||
| 13 | |||||||||||
| 14 | 3 | has repository_root => ( | |||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | required => 1, | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 3 | has generate_uncompressed => ( | |||||||||
| 20 | is => 'ro', | ||||||||||
| 21 | lazy => 1, | ||||||||||
| 22 | builder => '_build_generate_uncompressed', | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 4 | ▶ | sub _build_generate_uncompressed { 1 } | ||||||||
|
Subroutine
_build_generate_uncompressed: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | sub path_inside_root { | ||||||||||
| 28 | 4 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
path_inside_root: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 29 | 4 | return join("/",$self->file_parts); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | sub compressed_path_inside_root { | ||||||||||
| 33 | 0 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
compressed_path_inside_root: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 34 | 0 | return join("/",$self->file_parts).".gz"; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | sub full_filename { | ||||||||||
| 38 | 33 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
full_filename: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | 33 | return catfile( splitdir($self->repository_root), $self->file_parts ); | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 25 | ▶ | sub full_compressed_filename { shift->full_filename.".gz" } | ||||||||
|
Subroutine
full_compressed_filename: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 43 | |||||||||||
| 44 | sub exist { | ||||||||||
| 45 | 12 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
exist: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 46 | 12 | ▶ | return 0 unless -f $self->full_compressed_filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 6 | return 1; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub save { | ||||||||||
| 51 | 8 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
save: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||
| 52 | 8 | my @pps = $self->file_parts; | |||||||||
| 53 | 8 | pop(@pps); | |||||||||
| 54 | 8 | ▶ | $self->mkdir( splitdir( $self->repository_root ), @pps ) unless -d catdir( $self->repository_root, @pps ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 8 | my $content = $self->generate_content; | |||||||||
| 56 | 8 | ▶ | my $gz = IO::Zlib->new($self->full_compressed_filename, "w") or die "cant write to ".$self->full_compressed_filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 8 | print $gz $content; | |||||||||
| 58 | 8 | $gz->close; | |||||||||
| 59 | 8 | ▶ | if ($self->generate_uncompressed) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 8 | ▶ | my $txt = IO::File->new($self->full_filename, "w") or die "cant write to ".$self->full_filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 8 | print $txt $content; | |||||||||
| 62 | 8 | $txt->close; | |||||||||
| 63 | } | ||||||||||
| 64 | 8 | return 1; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | sub get_file_lines { | ||||||||||
| 68 | 5 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
get_file_lines: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 69 | 5 | ▶ | my $gz = IO::Zlib->new($self->full_compressed_filename, "r") or die "cant read ".$self->full_compressed_filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 5 | return <$gz>; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub mkdir { | ||||||||||
| 74 | 2 | ▶ | my ( $self, @path ) = @_; | ||||||||
|
Subroutine
mkdir: called CC=4 SCAR=21.7
Pod
undocumented
| |||||||||||
| 75 | 2 | make_path(catdir(@path),{ error => \my $err }); | |||||||||
| 76 | 2 | ▶ | if (@$err) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | for my $diag (@$err) { | |||||||||
| 78 | 0 | my ($file, $message) = %$diag; | |||||||||
| 79 | 0 | ▶ | if ($file eq '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | die "general error: $message\n"; | |||||||||
| 81 | } else { | ||||||||||
| 82 | 0 | die "problem making path $file: $message\n"; | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 3 | 1; | |||||||||
| 89 | |||||||||||
| 90 | __END__ | ||||||||||
| 91 | |||||||||||
| 92 | =pod | ||||||||||
| 93 | |||||||||||
| 94 | =head1 NAME | ||||||||||
| 95 | |||||||||||
| 96 | CPAN::Repository::Role::File - Role for file functions | ||||||||||
| 97 | |||||||||||
| 98 | =head1 VERSION | ||||||||||
| 99 | |||||||||||
| 100 | version 0.009 | ||||||||||
| 101 | |||||||||||
| 102 | =encoding utf8 | ||||||||||
| 103 | |||||||||||
| 104 | =head1 SUPPORT | ||||||||||
| 105 | |||||||||||
| 106 | IRC | ||||||||||
| 107 | |||||||||||
| 108 | Join #duckduckgo on irc.freenode.net. Highlight Getty for fast reaction :). | ||||||||||
| 109 | |||||||||||
| 110 | Repository | ||||||||||
| 111 | |||||||||||
| 112 | http://github.com/Getty/p5-cpan-repository | ||||||||||
| 113 | Pull request and additional contributors are welcome | ||||||||||
| 114 | |||||||||||
| 115 | Issue Tracker | ||||||||||
| 116 | |||||||||||
| 117 | http://github.com/Getty/p5-cpan-repository/issues | ||||||||||
| 118 | |||||||||||
| 119 | =head1 AUTHOR | ||||||||||
| 120 | |||||||||||
| 121 | Torsten Raudssus <torsten@raudss.us> L<http://raudss.us/> | ||||||||||
| 122 | |||||||||||
| 123 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 124 | |||||||||||
| 125 | This software is copyright (c) 2012 by DuckDuckGo, Inc. L<http://duckduckgo.com/>. | ||||||||||
| 126 | |||||||||||
| 127 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 128 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||