ExtLib.pm untested

filter: Statementstmt 0.0% 0 / 152 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 314 CC 27 SCAR 28.6
CC
27
Cov
0%
mkdir_p
56.0
rm_r
20.0
get_file_scp
20.0
CRAP 17.4
1 package VUser::ExtLib;
2 use warnings;
3 use strict;
4  
5  
6 # Copyright 2004 Randy Smith
7 # $Id: ExtLib.pm,v 1.20 2006-01-04 21:57:48 perlstalker Exp $
8  
9 our $VERSION = "0.3.0";
10  
11 use Exporter;
12 our @ISA = qw(Exporter);
13  
14 our @EXPORT = (); # Export nothing by default
15 our @EXPORT_OK = qw(add_line_to_file chown_ug check_bool
16 del_line_from_file edit_warning
17 generate_password mkdir_p repl_line_in_file
18 rm_r run_scripts_in_dir strip_ws touch
19 get_file_scp send_file_scp run_cmd_ssh
20 );
21 our %EXPORT_TAGS = (
22 config => [qw(check_bool strip_ws)],
23 files => [qw(add_line_to_file chown_ug
24 del_line_from_file
25 repl_line_in_file
26 rm_r mkdir_p touch)],
27 ssh => [qw(get_file_scp send_file_scp run_cmd_ssh)]
28 );
29  
30 sub version { $VERSION };
31  
32 sub add_line_to_file
33 {
34 my ($file, $line) = @_[0,1];
35  
36 open (FILE, ">>$file") or die "Can't open $file: $!\n";
37 print FILE "$line\n";
38 close FILE;
39 }
40  
41 sub chown_ug
42 {
43 my ($user, $group, @files) = @_;
44 my $uid = (getpwnam($user))[2]; # Get the numerical user ID
45 my $gid = (getgrnam($group))[2]; # Get the numerical group ID
46 return chown $uid, $gid, @files;
47 }
48  
49 sub check_bool
50 {
51 my $bool = shift;
52  
53 return 0 unless (defined $bool);
54  
55 $bool = strip_ws($bool);
56 if ($bool =~ /^(1|yes|true|ok(?:ay)?|sure|I guess so|of course)$/i) {
57 return 1;
58 } else {
59 return 0;
60 }
61 }
62  
63 sub del_line_from_file
64 {
65 my ($file, $line) = @_[0,1];
66  
67 while (-e "$file.tmp") { sleep(rand(int 3)); }
68  
69 open (FILE, $file) or die "Can't open $file: $!\n";
70 open (TMP, ">$file.tmp") or die "Can't open $file.tmp: $!\n";
71 while (<FILE>)
72 {
73 chomp;
74 print TMP "$_\n" unless /^\Q$line\E$/;
75 }
76 close FILE;
77 close TMP;
78  
79 rename ("$file.tmp", $file) or die "Can't rename $file.tmp to $file: $!\n";
80 }
81  
82 sub edit_warning
83 {
84 my $comment_char = shift || '#';
85  
86 my $warning = "$comment_char This file was written by vuser on ".scalar(localtime)."\n";
87 $warning .= "$comment_char DO NOT EDIT THIS FILE. Manual changes to this file will be lost.\n\n";
88 return $warning;
89 }
90  
91 sub generate_password
92 {
93 my $len = shift || 10;
94 my @valid = @_;
95 unless (@valid) {
96 @valid = (0..9, 'a'..'z', 'A'..'Z', '@', '#', '%', '^', '*');
97 }
98  
99 my $password = '';
100 for (1 .. $len) {
101 $password .= $valid[int (rand $#valid)];
102 }
103 return $password;
104 }
105  
106 # TODO: It may be possible to use File::Path::mkpath here to create the path
107 # but we would still have to chown the created files.
108 # Perhaps something like: for $dir (mkpath($path)) { chown($uid, $gid, $dir); }
109 sub mkdir_p
110 {
111 my ($dir, $mode, $user, $group) = @_;
112 $dir =~ s!/$!!;
113  
114 # If user is a uid, use it, otherwise it must be a
115 # name. We'll get the id from the system.
116 my $uid = ($user =~ /^\d+$/ ? $user : getpwnam($user));
117 my $gid = ($group =~ /^\d+$/ ? $group : getgrnam($group));
118  
119 die "Unknown user '$user'" if not defined $uid;
120 die "Unknown group '$group'" if not defined $gid;
121  
122 if( -e "$dir" )
123 {
124 return 1;
125 }
126  
127 my $parent = $dir;
128 $parent =~ s!/[^/]*$!!;
129  
130 if( !$parent ) { return 0; }
131 else
132 {
133 return mkdir_p( $parent, $mode, $uid, $gid )
134 && mkdir( $dir )
135 && chown( $uid, $gid, $dir );
136 }
137 }
138  
139 sub mvdir
140 {
141 my ($old_dir, $new_dir, $safe) = @_[0..2];
142  
143 use File::Path;
144 die "Directory already exists: $new_dir\n" if ($safe and -e $new_dir);
145  
146 eval { mkpath($new_dir); };
147 if ($@) {
148 print "Can't create dir: $new_dir: $@\n";
149 }
150  
151 rename( $old_dir, $new_dir) or die "Can't rename $old_dir to $new_dir: $!\n";
152 }
153  
154 sub repl_line_in_file
155 {
156 my ($file, $oline, $nline) = @_[0..2];
157  
158 while (-e "$file.tmp") { sleep(rand(int 3)); }
159  
160 open (FILE, $file) or die "Can't open $file: $!\n";
161 open (TMP, ">$file.tmp") or die "Can't open $file.tmp: $!\n";
162 while (<FILE>)
163 {
164 chomp;
165 if (/^\Q$oline\E$/)
166 {
167 print TMP "$nline\n";
168 }
169 else
170 {
171 print TMP "$_\n";
172 }
173
174 }
175 close FILE;
176 close TMP;
177  
178 rename "$file.tmp", $file or die "Can't rename $file.tmp to $file: $!\n";
179 }
180  
181 # TODO: replace the contents with a call to File::Path::rmtree
182 sub rm_r
183 {
184 my $dir = shift;
185  
186 die "No directory specified\n" unless $dir;
187  
188 opendir (DIR, $dir) or die "Unable to open $dir: $!";
189  
190 my @files = grep { ! /^\.\.?$/; } readdir DIR;
191 foreach my $file (@files) {
192 if (-d "$dir/$file") {
193 eval { rm_r("$dir/$file"); };
194 die "$@" if $@;
195 } else {
196 unlink "$dir/$file" or die "Unable to delete $dir/$file: $!";
197 }
198 }
199  
200 closedir DIR;
201  
202 rmdir $dir or die "Unable to delete dir: $!";
203 }
204  
205 sub run_scripts_in_dir
206 {
207 my $dir = shift;
208 my @args = @_;
209  
210 opendir (DIR, $dir) or die "Unable to open directory $dir: $!\n";
211  
212 my @scripts = ();
213 # Get all executable files (not directories) in the directory
214 @scripts = grep { not -d "$dir/$_" and -x _ } readdir DIR;
215  
216 closedir DIR;
217  
218 foreach my $script (@scripts) {
219 system ("$dir/$script", @args);
220 }
221 }
222  
223 sub strip_ws
224 {
225 my $string = shift;
226 return $string unless defined $string;
227 $string =~ s/^\s*(.*?)\s*$/$1/;
228 return $string;
229 }
230  
231 sub touch
232 {
233 my $file = shift;
234 my $time = shift || time();
235  
236 unless (-e $file) {
237 open (FILE, ">>$file") or die "Unable to open $file: $!\n";
238 close FILE;
239 }
240 utime($time, $time, $file) or die "Unable to change time on $file: $!\n";
241 }
242  
243 sub get_file_scp
244 {
245 my ($user, $host, $key, $remote_file, $local_file, @opts) = @_;
246  
247 my $exists = run_cmd_ssh ($user, $host, $key, "test -e $remote_file");
248 if ($exists != 0) {
249 run_cmd_ssh ($user, $host, $key, "touch $remote_file");
250 }
251  
252 my $rc = system ('scp', '-q', '-i', $key, @opts,
253 sprintf ('%s@%s:%s', $user, $host, $remote_file),
254 $local_file);
255 if ($rc == -1) {
256 die "Unable to run 'scp': $!";
257 }
258 $rc >>= 8;
259 if ($rc > 0) {
260 die "Error getting file. scp returned $rc\n";
261 }
262  
263 return;
264 }
265  
266 sub send_file_scp
267 {
268 my ($user, $host, $key, $local_file, $remote_file, @opts) = @_;
269  
270 my $rc = system ('scp', '-q', '-i', $key, @opts,
271 $local_file,
272 sprintf ('%s@%s:%s', $user, $host, $remote_file),
273 );
274 if ($rc == -1) {
275 die "Unable to run 'scp': $!";
276 }
277 $rc >>= 8;
278 if ($rc > 0) {
279 die "Error putting file. scp returned $rc\n";
280 }
281  
282 return;
283 }
284  
285 sub run_cmd_ssh
286 {
287 my ($user, $host, $key, $cmd, @ssh_opts) = @_;
288  
289 die "No command specified" unless defined $cmd;
290  
291 my $rc = system ('ssh', '-i', $key, @ssh_opts,
292 '-l', $user, $host, $cmd);
293 if ($rc == -1) {
294 die "Unable to run 'ssh': $!";
295 }
296 $rc >>= 8;
297 if ($rc == 255) {
298 die "Error running command. ssh returned $rc\n";
299 }
300  
301 return $rc;
302 }
303  
304 1;
305  
306 __END__