ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/AddDir.pm
Revision: 1.14.2.2.2.1
Committed: Thu Mar 13 12:54:52 2008 UTC (17 years, 1 month ago) by muzaffar
Content type: text/plain
Branch: SCRAM_V2_0
CVS Tags: V2_2_2, V2_2_2_pre4, V2_2_2_pre3, V2_2_2_pre2, V2_2_2_pre1, V2_2_2-pre1, V2_2_1, forV2_2_1, V2_2_0, sm100112, V2_1_4, V2_1_3, V2_1_2, V2_1_1, V2_1_0, V2_0_6, V2_0_5, V2_0_4, V2_0_4_relcand2, V2_0_4_relcand1, V2_0_3, V2_0_3_relcand3, V2_0_3_relcand2, V2_0_3_relcand1, V2_0_2, V2_0_2_relcand1, V2_0_1, V2_0_1_relcand4, V2_0_1_relcand3, V2_0_1_relcand2, V2_0_1_relcand1, V2_0_0_relcand4, V2_0_0, V2_0_0_relcand3, V2_0_0_relcand2, V2_0_0_relcand1
Changes since 1.14.2.2: +87 -1 lines
Log Message:
scram v2.0 for multiple arch support and big lib stuff

File Contents

# User Rev Content
1 sashby 1.10 =head1 NAME
2    
3     Utilities::AddDir - Utility functions for creating or copying directories.
4    
5     =head1 SYNOPSIS
6    
7     &Utilities::AddDir::adddir($dir);
8     &Utilities::AddDir::copydir($src,$dest);
9     &Utilities::AddDir::copydirwithskip($src,$dest,@files_to_skip);
10    
11     =head1 METHODS
12    
13     =over
14    
15     =cut
16    
17 muzaffar 1.14.2.1 package Utilities::AddDir;
18 williamc 1.2 require 5.001;
19 williamc 1.1 require Exporter;
20     use Cwd;
21     @ISA = qw(Exporter);
22 muzaffar 1.14.2.2.2.1 @EXPORT = qw(adddir copydir copydirwithskip copydirexp getfileslist fixpath);
23 williamc 1.4
24 sashby 1.10 =item C<adddir($dir)>
25    
26     Create a new directory.
27    
28     =cut
29 williamc 1.1
30 muzaffar 1.14.2.2.2.1
31     sub fixpath {
32     my $dir=shift;
33     my @parts=();
34     my $p="/";
35     if($dir!~/^\//){$p="";}
36     foreach my $part (split /\//, $dir)
37     {
38     if($part eq ".."){pop @parts;}
39     elsif(($part ne "") && ($part ne ".")){push @parts, $part;}
40     }
41     return "$p".join("/",@parts);
42     }
43    
44 williamc 1.3 sub adddir {
45 williamc 1.1 my $indir=shift;
46     my $startdir=cwd;
47     my @dir=split /\//, $indir;
48    
49     if ( $indir=~/^\// ) {
50     chdir "/";
51     shift @dir;
52     }
53     umask 02;
54     foreach $dirname ( @dir ) {
55     next if ( $dirname eq "" );
56     if ( ! -e $dirname ) {
57 sashby 1.9 mkdir ( $dirname , 0755) ||
58 williamc 1.1 die "cannot make directory ".$dirname." $!\n";
59     print $i." ".$dirname."\n" if $debug;
60     }
61     chdir $dirname;
62     }
63     chdir $startdir;
64 williamc 1.4 }
65    
66 sashby 1.10
67     =item C<copydir($src, $dest)>
68    
69     Copy a directory $src and contents to $dest.
70    
71     =cut
72    
73 sashby 1.6 sub copydir
74     {
75     my $src=shift;
76     my $dest=shift;
77    
78     use DirHandle;
79     use File::Copy;
80    
81     adddir($dest);
82     my $dh=DirHandle->new($src);
83    
84     if (defined $dh)
85     {
86     my @allfiles=$dh->read();
87    
88     my $file;
89     foreach $file ( @allfiles )
90     {
91 williamc 1.4 next if $file=~/^\.\.?/;
92 sashby 1.6 if ( -d $src."/".$file )
93     {
94     copydir($src."/".$file,$dest."/".$file);
95     }
96     else
97     {
98     copy($src."/".$file,$dest."/".$file);
99 sashby 1.12 if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
100 sashby 1.6 }
101 williamc 1.4 }
102 sashby 1.6 undef $dh;
103     }
104     else
105     {
106 elmer 1.14 die "Attempt to open a non-existent directory ($src). Exiting\n";
107 sashby 1.6 }
108     }
109 sashby 1.8
110 sashby 1.10 =item C<copydirwithskip($src, $dest, @files_to_skip)>
111    
112     Recursively copy a directory $src to $dest. All files
113     in @files_to_skip will be skipped.
114    
115     =cut
116    
117 sashby 1.8 sub copydirwithskip
118     {
119     my $src=shift;
120     my $dest=shift;
121 muzaffar 1.13 my $filetoskip=shift || [];
122 sashby 1.8
123     use DirHandle;
124     use File::Copy;
125    
126     adddir($dest);
127    
128     my $dh=DirHandle->new($src);
129    
130     if (defined $dh)
131     {
132     my @allfiles=$dh->read();
133    
134     my $file;
135     foreach $file ( @allfiles )
136     {
137     next if $file=~/^\.\.?/;
138     # Skip backup files and x~ files:
139 muzaffar 1.13 next if $file =~ /.*\.bak$/;
140 sashby 1.8 next if $file =~ /.*~$/;
141    
142 muzaffar 1.13 my $skip=0;
143     foreach my $fskip (@$filetoskip)
144     {
145     my $fullfile = "${src}/${file}";
146     if ($fullfile eq $fskip)
147     {
148     $skip = 1;
149     last;
150     }
151     }
152     if ($skip)
153 sashby 1.8 {
154     next;
155     }
156    
157     if ( -d $src."/".$file )
158     {
159 muzaffar 1.13 copydirwithskip($src."/".$file,$dest."/".$file,$filetoskip);
160 sashby 1.8 }
161     else
162     {
163     copy($src."/".$file,$dest."/".$file);
164 sashby 1.12 if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
165 sashby 1.8 }
166     }
167     undef $dh;
168     }
169     else
170     {
171 elmer 1.14 die "Attempt to open a non-existent directory ($src). Exiting\n";
172 sashby 1.8 }
173     }
174 muzaffar 1.14.2.2.2.1
175     sub copydirexp
176     {
177     my $src=shift;
178     my $dest=shift;
179     my $exp=shift || ".+";
180     my $op=shift || 0;
181    
182     if ($exp=~/^!(.+)/){$exp=$1; $op=1;}
183    
184     use DirHandle;
185     use File::Copy;
186     adddir($dest);
187     my $dh=DirHandle->new($src);
188    
189     if (defined $dh)
190     {
191     my @allfiles=$dh->read();
192    
193     my $file;
194     foreach $file ( @allfiles )
195     {
196     next if $file=~/^\.\.?/;
197     if ( -d $src."/".$file )
198     {
199     copydirexp($src."/".$file,$dest."/".$file,$exp,$op);
200     }
201     else
202     {
203     my $skip=1;
204     if ($file=~/$exp/)
205     {
206     if ($op == 0){$skip=0;}
207     }
208     elsif ($op == 1){$skip=0;}
209     if (!$skip)
210     {
211     copy($src."/".$file,$dest."/".$file);
212     if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
213     }
214     }
215     }
216     undef $dh;
217     }
218     else
219     {
220     die "Attempt to open a non-existent directory ($src). Exiting\n";
221     }
222     }
223    
224     sub getfileslist ()
225     {
226     my $dir=shift;
227     my $data=shift || [];
228     my $base=shift || $dir;
229     my $ref;
230     opendir($ref,$dir) || die "ERROR: Can not open directory for reading: $dir";
231     foreach my $f (readdir($ref))
232     {
233     next if $f=~/^\.\.?/;
234     $f = "${dir}/${f}";
235     if (-d $f){&getfileslist($f,$data,$base);}
236     else
237     {
238     $f=~s/^$base\///;
239     push @$data,$f;
240     }
241     }
242     closedir($ref);
243     return $data;
244     }
245    
246 muzaffar 1.14.2.2 1;
247    
248     __END__
249 sashby 1.10
250     =back
251    
252     =head1 AUTHOR
253    
254     Originally written by Christopher Williams.
255    
256     =head1 MAINTAINER
257    
258 sashby 1.11 Shaun ASHBY
259 sashby 1.10
260     =cut
261