File Coverage

blib/lib/Package/Utils.pm
Criterion Covered Total %
statement 53 115 46.0
branch 5 34 14.7
condition 0 6 0.0
subroutine 10 19 52.6
pod 10 10 100.0
total 78 184 42.3


line stmt bran cond sub pod time code
1             package Package::Utils;
2             require Exporter;
3             our @ISA = qw(Exporter);
4             our @EXPORT = qw(escapeSpecialChars getScriptFileContent getFileContents getUserMap getUserId getGroupMap getGroupId getUniqElement getInputFileLine trim); # Symbols to be exported by default
5              
6              
7 2     2   21355 use warnings;
  2         4  
  2         72  
8 2     2   11 use strict;
  2         5  
  2         75  
9              
10 2     2   12 use Cwd;
  2         3  
  2         132  
11 2     2   985 use File::stat;
  2         12695  
  2         16  
12 2     2   126 use File::Path;
  2         4  
  2         99  
13 2     2   11 use File::Find;
  2         5  
  2         91  
14 2     2   12 use File::Basename;
  2         4  
  2         133  
15 2     2   11 use File::Spec;
  2         5  
  2         2901  
16              
17             =head1 NAME
18              
19             Package::Util - The great new Package::Util!
20              
21             =head1 SYNOPSIS
22              
23             Quick summary of what the module does.
24              
25             Perhaps a little code snippet.
26              
27             use Package::Util;
28              
29             my $foo = Package::Util->new();
30             ...
31              
32             =head1 EXPORT
33              
34             A list of functions that can be exported. You can delete this section
35             if you don't export anything, such as for a purely object-oriented module.
36              
37             =head1 FUNCTIONS
38              
39             =head2 escapeSpecialChars
40             =cut
41              
42             sub escapeSpecialChars {
43 0     0 1 0 my $line = shift;
44 0         0 $line =~ s/(\$)/\\$1/g;
45 0         0 $line =~ s/(\()/\\$1/g;
46 0         0 $line =~ s/(\))/\\$1/g;
47              
48             # since a bug with \\
49             #$line=~s/(\&))/\\$1/g;
50             #$line=~s/(\\)/\\\\/g;
51 0         0 return $line;
52             }
53              
54              
55             =head2 getScriptFileContent
56             =cut
57              
58             sub getScriptFileContent {
59 0     0 1 0 my $filename = shift;
60 0 0       0 return "" if $filename eq "";
61 0 0       0 return "" unless -f $filename;
62 0         0 my @lines = getFileContents($filename);
63              
64             # remove first line
65 0         0 my $interp;
66 0 0 0     0 if ( defined( $lines[0] ) && $lines[0] =~ /^#!/ ) {
67              
68             #$lines[0] =~ s/#!(.*)/$1 << SCRIPT_END/;
69 0         0 $lines[0] = "";
70              
71             #push @lines, "SCRIPT_END";
72             }
73              
74 0         0 return join '', @lines;
75             }
76              
77             =head2 getFileContents
78              
79             =cut
80             sub getFileContents {
81 0     0 1 0 my $filename = shift;
82              
83 0 0       0 open( SOURCE, "< $filename" )
84             or die "Couldn't open $filename for reading: $!\n";
85 0         0 my @lines = ;
86              
87 0         0 close(SOURCE);
88 0         0 return @lines;
89             }
90              
91             =head2 getUserMap
92              
93             =cut
94             sub getUserMap {
95 1     1 1 2 my $file = "/etc/passwd";
96              
97 1 50       58 open( FD, $file ) or die "$file : $!";
98 1         62 my @lines = ;
99 1         16 close(FD);
100 1         3 my %localUserMap = ();
101 1         2 foreach my $line (@lines) {
102 20         42 chomp( $line = $line );
103 20         178 my ( $user, $passwd, $uid, $gid, $desc, $home, $shell ) =
104             split( ":", $line );
105 20 50       78 next unless defined $user;
106 20 100       61 $desc = 'default comment' if ( $desc eq '' );
107 20         84 $localUserMap{$uid}{'name'} = $user;
108 20         91 $localUserMap{$uid}{'gid'} = $gid;
109 20         53 $localUserMap{$uid}{'uid'} = $uid;
110 20         52 $localUserMap{$uid}{'comment'} = $desc;
111 20         65 $localUserMap{$uid}{'shell'} = $shell;
112 20         91 $localUserMap{$uid}{'home'} = $home;
113             }
114              
115 1         25 return %localUserMap;
116             }
117              
118             =head2 getUserId
119              
120             =cut
121             sub getUserId {
122 0     0 1 0 my $search_name = shift;
123 0         0 my $file = "/etc/passwd";
124              
125 0 0       0 open( FD, $file ) or die "$file : $!";
126 0         0 my @lines = ;
127 0         0 close(FD);
128 0         0 my %localUserMap = ();
129 0         0 foreach my $line (@lines) {
130 0         0 chomp( $line = $line );
131 0         0 my ( $user, $passwd, $uid, $gid, $desc, $home, $shell ) =
132             split( ":", $line );
133 0 0       0 next unless defined $user;
134 0 0       0 return $uid if ( $user eq $search_name );
135 0 0       0 return $uid if ( $uid eq $search_name );
136             }
137              
138 0         0 return 0;
139             }
140              
141             =head2 getGroupMap
142              
143             =cut
144             sub getGroupMap {
145 1     1 1 2 my $file = "/etc/group";
146              
147 1 50       40 open( FD, $file ) or die "$file : $!";
148 1         130 my @lines = ;
149 1         17 close(FD);
150 1         3 my %localGroupMap = ();
151 1         2 foreach my $line (@lines) {
152 44         82 chomp( $line = $line );
153 44         236 my ( $group, $passwd, $gid, $members ) = split( ":", $line );
154 44         174 $localGroupMap{$gid}{'gid'} = $gid;
155 44         121 $localGroupMap{$gid}{'name'} = $group;
156 44         154 $localGroupMap{$gid}{'members'} = $members;
157             }
158              
159 1         40 return %localGroupMap;
160             }
161              
162              
163             =head2 getGroupId
164              
165             =cut
166             sub getGroupId {
167 0     0 1   my $search_group = shift;
168 0           my $file = "/etc/group";
169              
170 0 0         open( FD, $file ) or die "$file : $!";
171 0           my @lines = ;
172 0           close(FD);
173 0           my %localGroupMap = ();
174 0           foreach my $line (@lines) {
175 0           chomp( $line = $line );
176 0           my ( $group, $password, $gid, $members ) = split( ":", $line );
177 0 0         return $gid if ( $group eq $search_group );
178 0 0         return $gid if ( $gid eq $search_group );
179             }
180              
181 0           return 0;
182             }
183              
184              
185             =head2 getUniqElement
186              
187             =cut
188             sub getUniqElement {
189 0     0 1   my %seen = ();
190 0           return grep { !$seen{$_}++ } shift;
  0            
191             }
192              
193              
194             =head2 getInputFileLine
195              
196             =cut
197             sub getInputFileLine {
198 0     0 1   my $directory = shift;
199 0           my @result = ();
200 0           my $line = "";
201 0 0 0       if ( defined($directory) && ( -d $directory ) ) {
202             find sub {
203 0     0     my $line = $File::Find::name;
204 0 0         $line .= "/" if -d;
205 0           push @result, $line;
206 0           }, ($directory);
207             }
208             else {
209              
210 0           while ( defined( $line = <> ) ) {
211 0           $line = trim($line);
212 0           push @result, $line;
213             }
214             }
215 0           return @result;
216             }
217              
218             =head2 trim
219              
220             =cut
221             sub trim {
222 0     0 1   my $string = shift;
223 0           $string =~ s/^\s+//;
224 0           $string =~ s/\s+$//;
225 0           return $string;
226             }
227              
228              
229             =head1 AUTHOR
230              
231             Jean-Marie RENOUARD, C<< >>
232              
233             =head1 BUGS
234              
235             Please report any bugs or feature requests to C, or through
236             the web interface at L. I will be notified, and then you'll
237             automatically be notified of progress on your bug as I make changes.
238              
239             =head1 SUPPORT
240              
241             You can find documentation for this module with the perldoc command.
242              
243             perldoc Package::Util
244              
245              
246             You can also look for information at:
247              
248             =over 4
249              
250             =item * RT: CPAN's request tracker
251              
252             L
253              
254             =item * AnnoCPAN: Annotated CPAN documentation
255              
256             L
257              
258             =item * CPAN Ratings
259              
260             L
261              
262             =item * Search CPAN
263              
264             L
265              
266             =back
267              
268              
269             =head1 ACKNOWLEDGEMENTS
270              
271              
272             =head1 COPYRIGHT & LICENSE
273              
274             Copyright 2007 Jean-Marie RENOUARD, all rights reserved.
275              
276             This program is free software; you can redistribute it and/or modify it
277             under the same terms as Perl itself.
278              
279              
280             =cut
281              
282             1; # End of Package::Util