File Coverage

blib/lib/PkgForge/Utils.pm
Criterion Covered Total %
statement 18 85 21.1
branch 0 50 0.0
condition 0 8 0.0
subroutine 6 10 60.0
pod 4 4 100.0
total 28 157 17.8


line stmt bran cond sub pod time code
1             package PkgForge::Utils; # -*- perl -*-
2 1     1   798 use strict;
  1         3  
  1         38  
3 1     1   6 use warnings;
  1         2  
  1         54  
4              
5             # $Id: Utils.pm.in 15529 2011-01-19 08:37:34Z squinney@INF.ED.AC.UK $
6             # $Source:$
7             # $Revision: 15529 $
8             # $HeadURL: https://svn.lcfg.org/svn/source/tags/PkgForge/PkgForge_1_4_8/lib/PkgForge/Utils.pm.in $
9             # $Date: 2011-01-19 08:37:34 +0000 (Wed, 19 Jan 2011) $
10              
11             our $VERSION = '1.4.8';
12              
13 1     1   944 use English qw(-no_match_vars);
  1         2766  
  1         5  
14 1     1   1436 use File::Find::Rule ();
  1         11047  
  1         25  
15 1     1   8 use File::Spec ();
  1         2  
  1         17  
16 1     1   1015 use IO::Dir ();
  1         34637  
  1         898  
17              
18             sub remove_tree {
19 0     0 1   my ( $top_dir, $options ) = @_;
20              
21 0   0       $options ||= {};
22              
23             # This is designed to be mostly compatible with remove_tree() in
24             # newer versions of File::Path
25              
26 0           my $verbose = $options->{verbose};
27 0           my $errors = $options->{error};
28 0           my $results = $options->{result};
29 0           my $keep_root = $options->{keep_root};
30              
31 0           my $count;
32              
33 0 0         if ( !-d $top_dir ) {
34 0           return 0;
35             }
36              
37 0           my @files =
38             File::Find::Rule->not( File::Find::Rule->directory )->in($top_dir);
39              
40 0           for my $file (@files) {
41 0           my $ok = unlink $file;
42 0 0         if ($ok) {
43 0           $count++;
44 0 0         print $file . "\n" if $verbose;
45 0 0         push @{$results}, $file if $results;
  0            
46             } else {
47 0 0         push @{$errors}, $file if $errors;
  0            
48             }
49             }
50              
51 0           my @dirs;
52 0 0         if ( -d $top_dir ) {
53 0           list_dirs( $top_dir, \@dirs );
54             }
55              
56 0           for my $dir (@dirs) {
57 0           my $ok = rmdir $dir;
58 0 0         if ($ok) {
59 0           $count++;
60 0 0         print $dir . "\n" if $verbose;
61 0 0         push @{$results}, $dir if $results;
  0            
62             } else {
63 0 0         push @{$errors}, $dir if $errors;
  0            
64             }
65             }
66              
67 0 0         if ( !$keep_root ) {
68 0           my $ok = rmdir $top_dir;
69 0 0         if ($ok) {
70 0           $count++;
71 0 0         print $top_dir . "\n" if $verbose;
72 0 0         push @{$results}, $top_dir if $results;
  0            
73             } else {
74 0 0         push @{$errors}, $top_dir if $errors;
  0            
75             }
76             }
77              
78 0           return $count;
79             }
80              
81             sub list_dirs {
82 0     0 1   my ( $dir, $list ) = @_;
83              
84 0 0         my $dh = IO::Dir->new($dir)
85             or die "Could not open $dir: $OS_ERROR\n";
86              
87 0           while ( defined( my $item = $dh->read ) ) {
88 0 0 0       if ( $item eq q{.} || $item eq q{..} ) {
89 0           next;
90             }
91              
92 0           my $path = File::Spec->catdir( $dir, $item );
93 0 0         if ( -d $path ) {
94 0           list_dirs( $path, $list );
95              
96 0           push @{$list}, $path;
  0            
97             }
98             }
99              
100 0           return;
101             }
102              
103             sub kinit {
104 0     0 1   my ( $keytab, $principal, $ccache ) = @_;
105              
106 0   0       $ccache ||= 'MEMORY:pkgforge_' . int(rand(10000));
107              
108 0           require Authen::Krb5;
109              
110 0 0         Authen::Krb5::init_context()
111             or die "Failed to initialise Krb5 context\n";
112              
113 0 0         my $client = Authen::Krb5::parse_name($principal)
114             or die Authen::Krb5::error() . " while parsing client principal\n";
115              
116 0 0         my $server = Authen::Krb5::parse_name( 'krbtgt/' . $client->realm )
117             or die Authen::Krb5::error() . " while parsing server principal\n";
118              
119 0 0         my $cc = Authen::Krb5::cc_resolve($ccache)
120             or die Authen::Krb5::error() . " while resolving ccache\n";
121              
122 0 0         $cc->initialize($client)
123             or die Authen::Krb5::error() . " while initializing ccache\n";
124              
125 0 0         my $kt=Authen::Krb5::kt_resolve($keytab)
126             or die Authen::Krb5::error() . " while resolving keytab\n";
127              
128 0 0         Authen::Krb5::get_in_tkt_with_keytab( $client, $server, $kt, $cc )
129             or die Authen::Krb5::error() . " while getting ticket\n";
130              
131 0           $ENV{KRB5CCNAME} = $ccache;
132              
133 0           return;
134             }
135              
136             sub job_resultsdir {
137 0     0 1   my ( $results_base, $uuid ) = @_;
138              
139 0           my $resultsdir = File::Spec->catdir( $results_base, $uuid );
140              
141 0           return $resultsdir;
142             }
143              
144              
145              
146             1;
147             __END__
148              
149             =head1 NAME
150              
151             PkgForge::Utils - General utilities for the LCFG Package Forge
152              
153             =head1 VERSION
154              
155             This documentation refers to PkgForge::Utils version 1.4.8
156              
157             =head1 SYNOPSIS
158              
159             use PkgForge::Utils;
160              
161             PkgForge::Utils::remove_tree($dir);
162              
163             =head1 DESCRIPTION
164              
165             This module provides various utility functions which are used
166             throughout the LCFG Package Forge software suite.
167              
168             =head1 SUBROUTINES/METHODS
169              
170             =over
171              
172             =item remove_tree($dir,[$options]);
173              
174             This will remove a tree of files and directories in a similar way to
175             the function with the same name in newer versions of the L<File::Path>
176             module. It takes a directory name and an optional reference to a hash
177             of options. It returns the number of files and directories which have
178             been deleted. The supported options are:
179              
180             =over
181              
182             =item verbose
183              
184             A boolean, when this is true the files and directories removed will be
185             printed to STDOUT, defaults to false.
186              
187             =item keep_root
188              
189             A boolean, when this is true the top-level directory itself will not
190             be removed, only the contents would be erased. This defaults to false.
191              
192             =item result
193              
194             A reference to an array into which the list of removed files and
195             directories will be added.
196              
197             =item error
198              
199             A reference to an array into which the list of any failures to remove
200             files and directories will be added.
201              
202             =back
203              
204             =item list_dirs( $dir, $list )
205              
206             This function will find all the sub-directories of the specified
207             directory. It puts them into an array (which is referenced in the
208             second required argument) which is ordered such that the
209             sub-directories are before the directory itself. This is used by
210             C<remove_tree>.
211              
212             =item kinit( $keytab, $principal, $ccache )
213              
214             This does the equivalent of kinit(1) using the specified keytab and
215             principal. Optionally you can specify the credentials cache to use, by
216             default it uses a memory cache. The KRB5CCNAME environment variable
217             will be set so that Kerberos-aware modules (such as DBI) will
218             automatically use the credentials cache.
219              
220             =item job_resultsdir( $results_base, $job_uuid )
221              
222             This computes and returns the job-specific results directory given the
223             base directory and the UUID for the job.
224              
225             =back
226              
227             =head1 DEPENDENCIES
228              
229             This module requires L<File::Find::Rule>. To use the C<kinit> method
230             you will also need the L<Authen::Krb5> to be installed.
231              
232             =head1 SEE ALSO
233              
234             L<PkgForge>
235              
236             =head1 PLATFORMS
237              
238             This is the list of platforms on which we have tested this
239             software. We expect this software to work on any Unix-like platform
240             which is supported by Perl.
241              
242             ScientificLinux5, Fedora13
243              
244             =head1 BUGS AND LIMITATIONS
245              
246             Please report any bugs or problems (or praise!) to bugs@lcfg.org,
247             feedback and patches are also always very welcome.
248              
249             =head1 AUTHOR
250              
251             Stephen Quinney <squinney@inf.ed.ac.uk>
252              
253             =head1 LICENSE AND COPYRIGHT
254              
255             Copyright (C) 2010-2011 University of Edinburgh. All rights reserved.
256              
257             This library is free software; you can redistribute it and/or modify
258             it under the terms of the GPL, version 2 or later.
259              
260             =cut