File Coverage

blib/lib/Software/Packager/Tar.pm
Criterion Covered Total %
statement 99 106 93.4
branch 21 40 52.5
condition n/a
subroutine 16 16 100.0
pod 4 6 66.6
total 140 168 83.3


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Software::Packager::Tar
4              
5             =head1 SYNOPSIS
6              
7             use Software::Packager;
8             my $packager = new Software::Packager('tar');
9              
10             =head1 DESCRIPTION
11              
12             This module is used to create tar files with the required structure
13             as specified by the list of object added to the packager.
14              
15             =head1 FUNCTIONS
16              
17             =cut
18              
19             package Software::Packager::Tar;
20              
21             ####################
22             # Standard Modules
23 1     1   5 use strict;
  1         2  
  1         34  
24 1     1   6 use Archive::Tar;
  1         2  
  1         43  
25 1     1   5 use File::Path;
  1         1  
  1         41  
26 1     1   797 use File::Copy;
  1         2302  
  1         50  
27 1     1   6 use File::Find;
  1         2  
  1         46  
28 1     1   6 use File::Basename;
  1         2  
  1         71  
29 1     1   25 use Cwd;
  1         3  
  1         60  
30             # Custom modules
31 1     1   6 use Software::Packager;
  1         2  
  1         36  
32              
33             ####################
34             # Variables
35 1     1   6 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  1         2  
  1         6881  
36             @ISA = qw( Software::Packager );
37             @EXPORT = qw();
38             @EXPORT_OK = qw();
39             $VERSION = 0.04;
40              
41             ####################
42             # Functions
43              
44             ################################################################################
45             # Function: new()
46              
47             =head2 B
48              
49             This method creates and returns a new class object.
50              
51             =cut
52             sub new
53             {
54 1     1 1 3 my $class = shift;
55 1         4 my $self = bless {}, $class;
56              
57 1         13 return $self;
58             }
59              
60             ################################################################################
61             # Function: package_name()
62              
63             =head2 B
64              
65             This method is used to format the package name and return it in the format
66             required for tar packages.
67             This method overrides the package_name method of Software::Packager.
68              
69             =cut
70             sub package_name
71             {
72 6     6 1 199 my $self = shift;
73 6         8 my $name = shift;
74              
75 6 100       17 if ($name)
76             {
77 1         3 $self->{'PACKAGE_NAME'} = $name;
78 1         4 return $self->{'PACKAGE_NAME'};
79             }
80             else
81             {
82 5         12 my $package_name = $self->{'PACKAGE_NAME'};
83 5         24 $package_name .= "-" . $self->version();
84              
85 5         109 return $package_name;
86             }
87             }
88              
89             ################################################################################
90             # Function: package()
91              
92             =head2 B
93              
94             This method overrides the base API and implements the required functionality
95             to create Tar software packages.
96             It calls teh following method in order setup, create_package and cleanup.
97              
98             =cut
99             sub package
100             {
101 1     1 1 2 my $self = shift;
102              
103 1 50       3 return undef unless $self->setup();
104 1 50       7 return undef unless $self->create_package();
105 1 50       6 return undef unless $self->cleanup();
106              
107 1         110 return 1;
108             }
109              
110             ################################################################################
111             # Function: setup()
112              
113             =head2 B
114              
115             This function sets up the temporary structure for the package.
116              
117             =cut
118             sub setup
119             {
120 1     1 1 2 my $self = shift;
121 1         8 my $cwd = getcwd();
122 1         5 my $tmp_dir = $self->tmp_dir();
123 1         4 my $package_build_dir = "$tmp_dir/" . $self->package_name();
124              
125             # process directories
126 1 50       15 unless (-d $package_build_dir)
127             {
128 1 50       354 mkpath($package_build_dir, 0, 0755) or
129             warn "Error: Problems were encountered creating directory \"$package_build_dir\": $!\n";
130             }
131 1         28 chdir $package_build_dir;
132              
133             # process directories
134 1         12 my @directories = $self->get_directory_objects();
135 1         4 foreach my $object (@directories)
136             {
137 0         0 my $destination = $object->destination();
138 0         0 my $user = $object->user();
139 0         0 my $group = $object->group();
140 0         0 my $mode = $object->mode();
141 0 0       0 unless (-d $destination)
142             {
143 0 0       0 mkpath($destination, 0, $mode) or
144             warn "Error: Problems were encountered creating directory \"$destination\": $!\n";
145             }
146             }
147              
148             # process files
149 1         10 my @files = $self->get_file_objects();
150 1         3 foreach my $object (@files)
151             {
152 14         41 my $source = $object->source();
153 14         37 my $destination = $object->destination();
154 14         396 my $dir = dirname($destination);
155 14 100       205 unless (-d $dir)
156             {
157 4 50       617 mkpath($dir, 0, 0755) or
158             warn "Error: Problems were encountered creating directory \"$dir\": $!\n";
159             }
160 14 50       44 copy($source, $destination) or
161             warn "Error: Problems were encountered coping \"$source\" to \"$destination\": $!\n";
162              
163 14         4420 my $user_id = $object->user();
164 14         39 my $group_id = $object->group();
165 14 50       67 $user_id = getpwnam($object->user()) unless $user_id =~ /\d/;
166 14 50       37 $group_id = getgrnam($object->group()) unless $group_id =~ /\d/;
167 14 50       325 chown($user_id, $group_id, $destination) or
168             warn "Error: Problems were encountered changing ownership: $!\n";
169              
170 14         43 my $mode = oct($object->mode());
171 14 50       303 chmod($mode, $destination) or
172             warn "Error: Problems were encountered changing permissions: $!\n";
173             }
174              
175             # process links
176 1         14 my @links = $self->get_link_objects();
177 1         13 foreach my $object (@links)
178             {
179 2         7 my $source = $object->source();
180 2         6 my $destination = $object->destination();
181 2         5 my $type = $object->type();
182              
183 2 100       15 if ($type =~ /hard/i)
    50          
184             {
185 1         147 eval link "$source", "$destination";
186 1 50       9 warn "Warning: Hard links not supported on this operatiing system: $@\n" if $@;
187             }
188             elsif ($type =~ /soft/i)
189             {
190 1         98 eval symlink "$source", "$destination";
191 1 50       7 warn "Warning: Soft links not supported on this operatiing system: $@\n" if $@;
192             }
193             else
194             {
195 0         0 warn "Error: Not sure what type of link to create soft or hard.";
196             }
197             }
198              
199 1         19 chdir $cwd;
200 1         6 return 1;
201             }
202              
203             ################################################################################
204             # Function: create_package()
205             # Description: This function creates the package
206             # Arguments: none.
207             # Return: true if ok else undef.
208             #
209             sub create_package
210             {
211 1     1 0 2 my $self = shift;
212 1         6 my $tmp_dir = $self->tmp_dir();
213 1         6 my $tar_file = $self->output_dir();
214 1         4 $tar_file .= "/" . $self->package_name();
215 1         2 $tar_file .= ".tar";
216              
217             # create the object
218 1         8 my $cwd = getcwd();
219 1         8 chdir $tmp_dir;
220 1         12 my $tar = new Archive::Tar();
221              
222             # Add everything to the archive.
223 1         13 my @files;
224 1     22   6 find sub {push @files, $File::Find::name;}, $self->package_name();
  22         724  
225 1 50       12 $tar->add_files(@files) or
226             warn "Error: Problems were encountered creating the archive: $!\n", $tar->error(), "\n";
227              
228             # write the sucker.
229 1         7569 $tar->write($tar_file);
230 1         7491 chdir $cwd;
231              
232 1         58 return 1;
233             }
234              
235             ################################################################################
236             # Function: cleanup()
237             # Description: This function removes the temporary structure for the package.
238             # Arguments: none.
239             # Return: true if ok else undef.
240             #
241             sub cleanup
242             {
243 1     1 0 2 my $self = shift;
244 1         7 my $tmp_dir = $self->tmp_dir();
245              
246             # there has to be a better way to to this!
247 1         10625 system("chmod -R 0777 $tmp_dir");
248 1         4885 rmtree($tmp_dir, 0, 0);
249 1         23 return 1;
250             }
251              
252             1;
253             __END__