File Coverage

blib/lib/Module/MetaInfo/_Extractor.pm
Criterion Covered Total %
statement 76 78 97.4
branch 22 30 73.3
condition 5 9 55.5
subroutine 10 10 100.0
pod 2 5 40.0
total 115 132 87.1


line stmt bran cond sub pod time code
1             package Module::MetaInfo::_Extractor;
2             $VERSION = "0.01";
3 4     4   763 use warnings;
  4         7  
  4         156  
4 4     4   20 use strict;
  4         8  
  4         134  
5 4     4   20 use Carp;
  4         6  
  4         294  
6 4     4   21 use Cwd;
  4         7  
  4         234  
7 4     4   895 use Symbol;
  4         1246  
  4         3418  
8              
9             =head1 NAME
10              
11             Module::MetaInfo::_Extractor - Base class for perl modules to get metainfo
12              
13             =head1 USAGE
14              
15             use Module::MetaInfo::_Extractor;
16             $mod=new Module::MetaInfo::_Extractor(perl-module-file.tar.gz);
17             $desc=$mod->description();
18              
19             =head1 DESCRIPTION
20              
21             This module provides untility functions for C
22             classes which need to extract the perl module in order to get their
23             meta information from it.
24              
25             =head1 FUNCTIONS
26              
27             =cut
28              
29             my $scratch_dir="/tmp/perl-metainfo-temp."
30             . ( $ENV{LOGNAME} ? $ENV{LOGNAME} : ( $ENV{USER} ? $ENV{USER} : "dumb" ) );
31              
32             my $verbose=0;
33              
34             =head1 Module::MetaInfo::_Extractor::new()
35              
36             new creates the object and initialises it. The argument is the path
37             of the perl module distribution file.
38              
39             =cut
40              
41             sub new {
42 14     14 0 111 my $s = shift;
43 14         28 my $distname = shift;
44 14   33     182 my $class = ref($s) || $s;
45 14         46 my $self={};
46              
47 14         40 my $distfile = $distname;
48 14 50       105667 $distfile =~ m,^/, || ( $distfile = cwd . '/' . $distfile );
49 14         1098 $distname =~ s,^.*/,,;
50 14         85 my $package_name = $distname;
51 14         277 $package_name =~ s,(.tar.gz)|(.tgz),,;
52 14         291 $self->{distfile}=$distfile;
53 14         67 $self->{distname}=$distname;
54 14         155 $self->{package_name}=$package_name;
55 14         77 $self->{_scratch_dir}=\$scratch_dir;
56 14         76 $self->{_verbose}=\$verbose;
57 14         800 return bless $self, $class;
58             }
59              
60             =head1 $thing->::verbose() $thing->::scratch_dir()
61              
62             These functions affect class settings (or if called for an object,
63             only the settings of the object: afterwards that object will ignore
64             changes to the class settings).
65              
66             Currently implemented are verbose which prints debugging info and
67             scratch_dir which sets the directory to be used for unpacking perl
68             modules.
69              
70             =cut
71              
72             #N.B. $self->{scratch_dir} is a reference to the variable holding the
73             #location of he scratch directory.
74              
75             sub scratch_dir {
76 8     8 0 10595 my $self = shift;
77 8         30 my $val = shift;
78 8 50       624 confess "usage: thing->scratch_dir(level)" if @_;
79 8 100       46 if (ref($self)) {
80 2 100       12 return ${$self->{"_scratch_dir"}} unless defined $val;
  1         15  
81 1         3 $self->{"_scratch_dir"} = \$val; # just myself
82 1         2 return ${$self->{"_scratch_dir"}};
  1         11  
83             } else {
84 6 100       45 return $scratch_dir unless defined $val;
85 3         23 $scratch_dir = $val; # whole class
86 3         12 return $scratch_dir;
87             }
88 0         0 die "not reached";
89             }
90              
91             #N.B. $self->{verbose} is a reference to the variable holding the
92             #location of he scratch directory.
93             sub verbose {
94 8     8 0 331 my $self = shift;
95 8         49 my $val = shift;
96 8 50       286 confess "usage: thing->verbose(level)" if @_;
97 8 100       46 if (ref($self)) {
98 3 100       19 return ${$self->{"_verbose"}} unless defined $val;
  2         24  
99 1         4 $self->{"_verbose"} = \$val; # just myself
100 1         3 return ${$self->{"_verbose"}};
  1         12  
101             } else {
102 5 100       24 return $verbose unless defined $val;
103 3         11 $verbose = $val; # whole class
104 3         7 return $verbose;
105             }
106 0         0 die "not reached";
107             }
108              
109             =head2 $self->setup()
110              
111             Setup prepares us for getting meta information. In the current
112             implementation it does this by unpacking the distribution file. In a
113             'future version this function may do nothing and issue a warning, but
114             it will continute to exist into the forseeable future.
115              
116             The only reason to call this function now is to trap errors from it
117             separately or if you delete the setup directory and want it's contents
118             re-created.
119              
120             =cut
121              
122             sub setup {
123 5     5 1 15389 my $self=shift;
124 5         51455 my $old_dir=cwd;
125 5         83 my $scratch=${$self->{_scratch_dir}};
  5         54  
126 5 50       30 croak "scratch dir not defined " unless defined ${$self->{_scratch_dir}};
  5         56  
127 5 50 66     369 -e $scratch && (! -d _ )
128             && croak "scratch dir $scratch exists but is not a directory";
129 5 50 66     172 -e _ or mkdir $scratch_dir
130             or die "can't create scratch directory $scratch_dir" . $!;
131              
132             #FIXME: check for correct ownership of scratchdir?? probably just that
133             #we have write access since we will work inside our own sub directories
134             #inside it, however there could be a danger of race conditions if we use
135             #someone elses directory then they rename something down the tree??
136              
137 5         18 my $unpack_dir=${$self->{_scratch_dir}} . '/' . $self->{distname};
  5         56  
138             #FIXME: we should actually check that there is an unpacked module
139 5         86 $self->{expand_dir}=
140 5         13 ${$self->{_scratch_dir}} .'/'. $self->{distname}
141             .'/'. $self->{package_name};
142 5         42 $self->{setup}=1;
143             -d $unpack_dir
144 5 100       132 && do { warn "setup called but directory $unpack_dir exists"; return; };
  4         856  
  4         97  
145 1 50       22 -e $unpack_dir
146             && die "file exists where setup directory should be $unpack_dir";
147 1         58 mkdir $unpack_dir;
148             #FIXME: check exit status etc... think about all kinds of tar... use
149             #perl TAR module??
150 1         17243 system 'tar', 'xzCf', $unpack_dir, $self->{distfile};
151 1 50       144 -d $unpack_dir
152             || die "unpacking perl module didn't create the right name.";
153             }
154              
155             =head2 name
156              
157             returns the packages name, or at least an approximation
158              
159             =cut
160              
161             sub name {
162 1     1 1 93 my $self=shift;
163 1         17 return $self->{package_name};
164             }
165              
166              
167             =head1 COPYRIGHT
168              
169             You may distribute under the terms of either the GNU General
170             Public License or the Artistic License, as specified in the
171             Perl README.
172              
173             =head1 BUGS
174              
175             We trust the path to the scratch directory. Make sure that nobody
176             that you don't trust can control any of the directories up to and
177             including the scratch directory. There shoudld be an option to test
178             that the ownership and control is clear.
179              
180             =head1 AUTHOR
181              
182             Michael De La Rue.
183              
184             =head1 SEE ALSO
185              
186             L L
187              
188             =cut
189              
190             42;