File Coverage

blib/lib/Software/Packager/Object.pm
Criterion Covered Total %
statement 58 89 65.1
branch 23 40 57.5
condition 1 3 33.3
subroutine 10 10 100.0
pod 7 7 100.0
total 99 149 66.4


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Software::Packager::Object - Generic object data storage
4              
5             =head1 SYNOPSIS
6              
7             use Software::Packager::Object;
8              
9             =head1 DESCRIPTION
10              
11             This module is used by Software::Packager for holding data for a each item
12             added to the a software package. It provides an easy way of accessing the data
13             for each object to be installed.
14             This module is designed to be easly sub classed and / or extended.
15              
16             =head1 SUB-CLASSING
17              
18             To extend or sub-class this module create a new module along the lines of
19              
20             package Foo;
21              
22             use Software::Packager::Object;
23             use vars qw(@ISA);
24             @ISA = qw( Software::Packager::Object );
25              
26             ########################
27             # _check_data we don't care about anything other that DESTINATION and FOO_DATA;
28             sub _check_data
29             {
30             my $self = shift;
31             my %data = @_;
32              
33             return undef unless $self->{'DESTINATION'};
34             return undef unless $self->{'FOO_DATA'};
35              
36             # now set the data for the object
37             foreach my $key (keys %data)
38             {
39             my $function = lc $key;
40             return undef unless $self->$function($data{$key});
41             }
42             }
43              
44             ########################
45             # foo_data returns the foo value fo this object.
46             sub foo_data
47             {
48             my $self = shift;
49             return $self->{'FOO_DATA'};
50             }
51             1;
52             __END__
53              
54              
55             Of course I would have created the module with a package of
56             Software::Packager::Object::Foo but that's you choice.
57              
58             =head1 FUNCTIONS
59              
60             =cut
61              
62             package Software::Packager::Object;
63              
64             ####################
65             # Standard Modules
66 2     2   9 use strict;
  2         4  
  2         85  
67             # Custom modules
68              
69             ####################
70             # Variables
71 2     2   11 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  2         4  
  2         2354  
72             @ISA = qw();
73             @EXPORT = qw();
74             @EXPORT_OK = qw();
75             $VERSION = 0.08;
76              
77             ####################
78             # Functions
79              
80             ################################################################################
81             # Function: new()
82              
83             =head2 B
84              
85             my $object = new Software::Packager::Object(%object_data);
86              
87             This function creates and returns a new Software::Packager::Object object which
88             is used to access the data in the passed hash. This passed data is passed on and
89             checked for problems by the _check_data() method.
90              
91             The hash of data passed should contain at least the following
92            
93             %hash = (
94             'TYPE' => 'file type',
95             'SOURCE' => 'source file location. Not required for directories.',
96             'DESTINATION' => 'destination location',
97             'USER' => 'user to install as',
98             'GROUP' => 'group to install as',
99             'MODE' => 'permissions to install the file with',
100             );
101              
102             =cut
103             sub new
104             {
105 16     16 1 22 my $class = shift;
106 16         37 my %data = @_;
107              
108 16         41 my $self = bless {}, $class;
109 16 50       40 return undef unless $self->_check_data(%data);
110              
111 16         49 return $self;
112             }
113              
114             ################################################################################
115             # Function: _check_data()
116              
117             =head2 B<_check_data()>
118              
119             $self->_check_data(%data);
120              
121             This function checks that the data for this object is okay and returns true if
122             there are problems with the data then undef is returned.
123              
124             TYPE If the type is a file then the value of SOURCE must be a real
125             file. If the type is a soft/hard link then the source and
126             destination must both be present.
127             SOURCE nothing special to check, see TYPE
128             DESTINATION nothing special to check, see TYPE
129             MODE Defaults to 0755 for directories and 0644 for files.
130             USER Defaults to the current user
131             GROUP Defaults to the current users primary group
132              
133             =cut
134             sub _check_data
135             {
136 16     16   16 my $self = shift;
137 16         45 my %data = @_;
138              
139             # The object type must be set.
140 16 50       45 unless (exists $data{'TYPE'})
141             {
142 0         0 warn "Error: The object type is not set. This value is required.\n";
143 0         0 return undef;
144             }
145            
146             # Now do some checks depending on the object type
147 16 100       69 if ($data{'TYPE'} =~ /^file$/i)
    50          
148             {
149 14 50       273 unless (-f $data{'SOURCE'})
150             {
151 0         0 warn "Error: The value for SOURCE is not set! This is a required value for file objects.\n";
152 0         0 return undef;
153             }
154             }
155             elsif ($data{'TYPE'} =~ /link/i)
156             {
157 2 50 33     15 unless ($data{'SOURCE'} and $data{'DESTINATION'})
158             {
159 0         0 warn "Error: Either SOURCE of DESTINATION are not set! both are required for link objects.\n";
160 0         0 warn "Error: SOURCE=\"$data{'SOURCE'}\" DESTINATION=\"$data{'DESTINATION'}\"\n";
161 0         0 return undef;
162             }
163             }
164              
165             # now set the data for the object
166 16         49 foreach my $key (keys %data)
167             {
168 62         84 my $function = lc $key;
169 62 50       139 unless ($self->$function($data{$key}))
170             {
171             #warn "Error: There is an error with the value of $key.\n";
172 0         0 return undef;
173             }
174             }
175              
176 16         56 return 1;
177             }
178              
179             ################################################################################
180             # Function: type()
181              
182             =head2 B
183              
184             $object->type($value);
185             $type = $object->type();
186              
187             This method sets or returns the type of this object.
188             When the object type is being set then the value passed will be checked.
189              
190             Valid object types are:
191              
192             File: A standard file.
193             Directory: A directory.
194             Hardlink: A file link.
195             Softlink: A symbolic link.
196             Install: An installation file used by the installer.
197             Config: A configuration file.
198             Volatile: A volatile file.
199             Pipe: A named pipe.
200             Charater: A charater special device.
201             Block: A block special device.
202             Multiplex: A multiplexed special device.
203              
204             =cut
205             sub type
206             {
207 66     66 1 76 my $self = shift;
208 66         66 my $value = shift;
209              
210 66 100       87 if ($value)
211             {
212             # first check that the type is a valid file type.
213 16 50       49 unless ($value =~ /^file$|^directory$|^hardlink$|^softlink$|^install$|^config$|^volatile$|^pipe$|^charater$|^block$|^multiplex$|^installdir$/i)
214             {
215 0         0 warn "Error: Unknown object type \"$value\".\n";
216 0         0 warn " The object type should be one of:\n";
217 0         0 warn "File: A standard file.\n";
218 0         0 warn "Directory: A directory.\n";
219 0         0 warn "Hardlink: A file link.\n";
220 0         0 warn "Softlink: A symbolic link.\n";
221 0         0 warn "Install: An installation file used by the installer.\n";
222 0         0 warn "Config: A configuration file.\n";
223 0         0 warn "Volatile: A volatile file.\n";
224 0         0 warn "Pipe: A named pipe.\n";
225 0         0 warn "Charater: A charater special device.\n";
226 0         0 warn "Block: A block special device.\n";
227 0         0 warn "Multiplex: A multiplexed special device.\n";
228 0         0 return undef;
229             }
230              
231 16         63 $self->{'TYPE'} = $value;
232             }
233             else
234             {
235 50         224 return $self->{'TYPE'};
236             }
237             }
238              
239             ################################################################################
240             # Function: source()
241              
242             =head2 B
243              
244             $object->source($value);
245             $source = $object->source();
246              
247             This method sets or returns the source location for this object.
248              
249             =cut
250             sub source
251             {
252 32     32 1 47 my $self = shift;
253 32         33 my $value = shift;
254              
255 32 100       52 if ($value)
256             {
257 16         57 $self->{'SOURCE'} = $value;
258             }
259             else
260             {
261 16         54 return $self->{'SOURCE'};
262             }
263             }
264              
265             ################################################################################
266             # Function: destination()
267              
268             =head2 B
269              
270             $object->destination($value);
271             $destination = $object->destination();
272              
273             This method sets or returns the destination location for this object.
274              
275             =cut
276             sub destination
277             {
278 520     520 1 550 my $self = shift;
279 520         498 my $value = shift;
280              
281 520 100       774 if ($value)
282             {
283 16         64 $self->{'DESTINATION'} = $value;
284             }
285             else
286             {
287 504         1768 return $self->{'DESTINATION'};
288             }
289             }
290              
291             ################################################################################
292             # Function: mode()
293              
294             =head2 B
295              
296             $object->mode($value);
297             $mode = $object->mode();
298              
299             This method sets or returns the installation mode for this object.
300              
301             NOTE: The mode is stored in octal but that doesn't mean that you are using it
302             in octal if you are trying to use the return value in a chmod command then do
303             something like.
304              
305             $mode = oct($object->mode());
306             chmod($mode, $object->destination());
307              
308             Do lots of tests!
309              
310             If the mode is not set then default values are set. Directories are set to 0755
311             everything else defaults to the mode the object source has.
312              
313             =cut
314             sub mode
315             {
316 28     28 1 38 my $self = shift;
317 28         31 my $value = shift;
318              
319 28 100       42 if ($value)
320             {
321 14         75 $self->{'MODE'} = $value;
322             }
323             else
324             {
325             # set some defaults if nothing is set
326 14 50       33 unless ($self->{'MODE'})
327             {
328 0 0       0 if ($self->{'TYPE'} eq 'directory')
329             {
330 0         0 $self->{'MODE'} = '0755';
331             }
332             else
333             {
334 0 0       0 if (-e $self->source())
335             {
336 0         0 my @stats = stat $self->source();
337 0         0 $self->{'MODE'} = sprintf "%04o", $stats[2] & 07777;
338             }
339             else
340             {
341 0         0 $self->{'MODE'} = '0644';
342             }
343             }
344             }
345              
346             # symbolic links are always 0777
347 14 50       32 if ($self->{'TYPE'} =~ /softlink/i)
348             {
349 0         0 $self->{'MODE'} = '0777';
350             }
351              
352 14         60 return $self->{'MODE'};
353             }
354             }
355              
356             ################################################################################
357             # Function: user()
358              
359             =head2 B
360              
361             $object->user($value);
362             $user = $object->user();
363              
364             This method sets or returns the user id that this object should be installed as.
365             If the user is not set for the object then the user defaults to the current
366             user.
367              
368             If this becomes a problem it can be changed to be the owner of the object.
369              
370             =cut
371             sub user
372             {
373 14     14 1 28 my $self = shift;
374 14         15 my $value = shift;
375              
376 14 50       28 if ($value)
377             {
378 0         0 $self->{'USER'} = $value;
379             }
380             else
381             {
382 14 50       35 unless ($self->{'USER'})
383             {
384 14         63 $self->{'USER'} = $<;
385             }
386 14         47 return $self->{'USER'};
387             }
388             }
389              
390             ################################################################################
391             # Function: group()
392              
393             =head2 B
394              
395             This method sets or returns the group id that this object should be installed
396             as.
397             If the group is not set for the object then the group defaults to the current
398             primary group.
399              
400             If this becomes a problem it can be changed to be the group of the object.
401              
402             =cut
403             sub group
404             {
405 14     14 1 19 my $self = shift;
406 14         14 my $value = shift;
407              
408 14 50       24 if ($value)
409             {
410 0         0 $self->{'GROUP'} = $value;
411             }
412             else
413             {
414 14 50       31 unless ($self->{'GROUP'})
415             {
416 14         87 my $groups = $(;
417 14         56 my ($group, @rest) = split / /, $groups;
418 14         34 $self->{'GROUP'} = $group;
419             }
420 14         41 return $self->{'GROUP'};
421             }
422             }
423              
424             1;
425             __END__