source: main/trunk/package-kits/linux/perllib/Greenstone/Helpers.pm@ 29536

Last change on this file since 29536 was 29536, checked in by Jeremy Symon, 9 years ago

Working on a more flexible generation system. Only working for pacman so far, so if you want to generate packages you'll need to check out an old revision. The reason behind this change is that I discovered that the greenstone user database really needs to be owned by the tomcat user rather than root (things such as edit mode don't work properly otherwise), and setting file ownership requires .install files, which was not really possible with the existing system. At this point it would certainly have been faster to just write all the scripts by hand, but this way I get to learn perl.

File size: 1.5 KB
Line 
1package Greenstone::Helpers;
2
3use strict;
4use warnings;
5use utf8;
6use File::Copy 'cp';
7use base 'Exporter';
8
9our $VERSION = 1.00;
10our @EXPORT = qw(lsfiles empty comment trim escape hashdump);
11
12sub lsfiles {
13 my ($dir, $files) = @_;
14 my $all = $files =~ /^all$/i;
15 my %hash;
16 if (not $all) {
17 for my $file (split ',', $files) {
18 $hash{$file} = 1;
19 }
20 }
21 my @list;
22 opendir DIR, $dir or die "Could not open '$dir': $!";
23 while (my $entry = readdir DIR) {
24 next if (not -f "$dir/$entry");
25 if ($all) {
26 push @list, $entry;
27 } elsif (exists $hash{$entry}) {
28 push @list, $entry;
29 delete $hash{$entry};
30 }
31 }
32 closedir DIR;
33 if (keys %hash) {
34 die "Could not find [ " . join (', ', sort keys %hash) . " ]";
35 }
36 return @list;
37}
38
39sub empty {
40 shift =~ /^\s*$/ and return 1;
41}
42
43sub comment {
44 shift =~ /^\s*#/ and return 1;
45}
46
47sub trim {
48 for (@_) {
49 s/^\s+|\s+$//g;
50 }
51 return @_;
52}
53
54sub escape {
55 for (@_) {
56 s/\\n/\n/g;
57 s/\\ / /g;
58 s/\\\\/\\/g;
59 }
60 return @_;
61}
62
63
64
65sub hashdump {
66 my $hash = shift;
67 for my $key (keys %$hash) {
68 my $val = $hash->{$key};
69 if (ref $val eq 'ARRAY') {
70 print "'$key' =\n";
71 for my $i (@$val) {
72 print "\t'$i'\n";
73 }
74 } else {
75 print "'$key' = '$val'\n";
76 }
77 }
78 print "\n\n";
79}
80
811;
Note: See TracBrowser for help on using the repository browser.