10
use vars qw(@ISA @EXPORT @EXPORT_OK);
14
@EXPORT_OK = qw(gen_config_file gen_wanted write_to_file);
16
use vars qw($test_config_file %test_config @test_roles $test_hostname);
17
use vars qw($TEST_TMPDIR);
18
push @EXPORT, qw($test_config_file %test_config @test_roles $test_hostname);
20
# Because all the scripts chdir('/'), we need to know the cwd for our configs
21
my $TEST_DIR = getcwd;
22
$TEST_TMPDIR = getcwd."/tmp";
23
$test_hostname = hostname;
25
$test_config_file = "$TEST_DIR/slack.conf";
27
'source' => "$TEST_DIR/testsource",
28
'role-list' => "$TEST_DIR/roles.conf",
29
'cache' => "$TEST_TMPDIR/cache",
30
'stage' => "$TEST_TMPDIR/stage",
31
'root' => "$TEST_TMPDIR/root",
32
'backup-dir' => "$TEST_TMPDIR/backups",
36
@test_roles = sort qw(role1 role2.sub role3.sub.sub);
38
sub gen_config_file ($$) {
39
my ($template_file, $file) = @_;
41
open(TEMPLATE, "<", "$template_file")
42
or die "Could not open template file $template_file: $!";
43
open(FILE, ">", $file)
44
or die "Could not open output file $file: $!";
47
s/__TEST_DIR__/$TEST_DIR/g;
48
s/__TEST_TMPDIR__/$TEST_TMPDIR/g;
49
s/__HOSTNAME__/$test_hostname/g;
50
s/__ROLES__/join(" ", @test_roles)/ge;
54
or die "Could not close template file $template_file: $!";
56
or die "Could not close output file $file: $!";
59
# Transform globs into regexes, since I can't find a function to check
60
# glob matches on strings.
61
sub glob_to_regex ($) {
63
$pat =~ s#/$##; # strip trailing slashes
64
$pat =~ s#([./^\$()+])#\\$1#g; # escape re metachars
65
$pat =~ s#([?*])#.$1#g; # convert glob metachars
69
# This is to help with comparing lists of files in two directory trees.
71
# Returns a wanted function for File::Find which will maintain a file list
72
# in a hash that looks like:
73
# filename => filetype
74
# where valid filetypes are:
79
# and which will skip files rsync is known to skip.
80
# Symlinks are dereferenced because that's what we tell rsync to do, too.
82
# Takes as arguments a basename which will be stripped off file names
83
# and a hash reference (in which to maintain the file list above)
85
my ($base, $hashref) = @_;
89
# Suppress spurious warning about the # and , characters below
91
# Straight out of the rsync manpage section for --cvs-exclude
93
RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS .make.state
94
.nse_depinfo *~ #* .#* ,* _$* *$ *.old *.bak *.BAK *.orig *.rej
95
.del-* *.a *.olb *.o *.obj *.so *.exe *.Z *.elc *.ln core .svn/
98
@cvs_exclude = map {glob_to_regex($_)} @cvs_exclude;
101
# Prune out files in the CVS exclude list used by rsync
102
for my $pat (@cvs_exclude) {
104
$File::Find::prune = 1;
119
my $filename = $File::Find::name;
120
# Try to strip off the base
121
return unless ($filename =~ s#^$base/##);
122
$hashref->{$filename} = $filetype;
126
sub write_to_file ($$) {
127
my ($file, $text) = @_;
129
open($fh, '>', $file)
130
or die "Could not open $file for writing: $!";
132
or die "Could not write to $file: $!";
134
or die "Could not close $file: $!";