| 1 | package File::ShareDir::Tarball; | ||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | # ABSTRACT: Deal transparently with shared files distributed as tarballs | ||||||||||
| 4 | 2 | $File::ShareDir::Tarball::VERSION = '0.3.0'; | |||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use parent qw/ Exporter /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use File::ShareDir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use Archive::Tar; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use File::Temp qw/ tempdir /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use File::chdir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Memoize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | our @EXPORT_OK = qw{ | |||||||||
| 20 | dist_dir dist_file | ||||||||||
| 21 | }; | ||||||||||
| 22 | 2 | our %EXPORT_TAGS = ( | |||||||||
| 23 | all => [ @EXPORT_OK ], | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | 2 | my $shared_files_tarball = 'shared-files.tar.gz'; | |||||||||
| 27 | |||||||||||
| 28 | # we don't want to extract the same dirs again and | ||||||||||
| 29 | # again within a single program | ||||||||||
| 30 | 2 | memoize('dist_dir'); | |||||||||
| 31 | |||||||||||
| 32 | 2 | ▶ | sub dist_dir { | ||||||||
|
Subroutine
dist_dir: called CC=3 SCAR=11.1
| |||||||||||
| 33 | 2 | my $dist = shift; | |||||||||
| 34 | |||||||||||
| 35 | 2 | my $dir = File::ShareDir::dist_dir($dist); | |||||||||
| 36 | |||||||||||
| 37 | # no tarball? Assume regular shared dir | ||||||||||
| 38 | 2 | ▶ | return $dir unless -f "$dir/$shared_files_tarball"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 1 | my $archive = Archive::Tar->new; | |||||||||
| 41 | 1 | $archive->read("$dir/$shared_files_tarball"); | |||||||||
| 42 | |||||||||||
| 43 | # because that would be a veeery bad idea | ||||||||||
| 44 | croak "archive '$shared_files_tarball' contains files with absolute path, aborting" | ||||||||||
| 45 | 0 | ▶ | if grep { m#^/# } $archive->list_files; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 1 | my $tmpdir = tempdir( CLEANUP => 1 ); | |||||||||
| 48 | 1 | local $CWD = $tmpdir; | |||||||||
| 49 | |||||||||||
| 50 | 1 | $archive->extract; | |||||||||
| 51 | |||||||||||
| 52 | 1 | return $tmpdir; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | sub dist_file { | ||||||||
|
Subroutine
dist_file: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 56 | 0 | my $dist = File::ShareDir::_DIST(shift); | |||||||||
| 57 | 0 | my $file = File::ShareDir::_FILE(shift); | |||||||||
| 58 | |||||||||||
| 59 | 0 | my $path = dist_dir($dist).'/'.$file; | |||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | return undef unless -e $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | croak("Found dist_file '$path', but not a file") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | unless -f $path; | ||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | croak("File '$path', no read permissions") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | unless -r $path; | ||||||||||
| 68 | |||||||||||
| 69 | 0 | return $path; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 2 | 1; | |||||||||
| 73 | |||||||||||
| 74 | __END__ | ||||||||||