annotate MPIWGStor.pm @ 13:f33fb7f1898e

added mapping for spanish lang
author casties
date Wed, 01 Jun 2005 14:24:19 +0200
parents 620aad237f57
children 8eee361fdec8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
1
30497c6a3eca Initial revision
casties
parents:
diff changeset
2 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
3 # Library with definitions and common routines for MPIWG storage system
30497c6a3eca Initial revision
casties
parents:
diff changeset
4 # scripts
30497c6a3eca Initial revision
casties
parents:
diff changeset
5 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
6
30497c6a3eca Initial revision
casties
parents:
diff changeset
7 package MPIWGStor;
30497c6a3eca Initial revision
casties
parents:
diff changeset
8
30497c6a3eca Initial revision
casties
parents:
diff changeset
9 use strict;
30497c6a3eca Initial revision
casties
parents:
diff changeset
10 use vars qw(@ISA @EXPORT $VERSION
30497c6a3eca Initial revision
casties
parents:
diff changeset
11 $debug %junk_files %index_files $index_file_perm $archived_index_file_perm
30497c6a3eca Initial revision
casties
parents:
diff changeset
12 $file_perm);
30497c6a3eca Initial revision
casties
parents:
diff changeset
13 use Exporter;
30497c6a3eca Initial revision
casties
parents:
diff changeset
14
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
15 $VERSION = 0.6; # ROC 26.5.2005
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
16
30497c6a3eca Initial revision
casties
parents:
diff changeset
17 @ISA = qw(Exporter);
30497c6a3eca Initial revision
casties
parents:
diff changeset
18
30497c6a3eca Initial revision
casties
parents:
diff changeset
19 @EXPORT = qw($debug %junk_files %index_files $index_file_perm $archived_index_file_perm
30497c6a3eca Initial revision
casties
parents:
diff changeset
20 $file_perm
30497c6a3eca Initial revision
casties
parents:
diff changeset
21 &parseargs &logger &stime &s2stime &ymd_date &split_file_path &sstrip
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
22 &valid_file_name &valid_dir_name &park_file &unpark_file
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
23 &read_xml &write_xml &add_text_element &create_element_path &create_text_path);
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
24
30497c6a3eca Initial revision
casties
parents:
diff changeset
25 # debug level
30497c6a3eca Initial revision
casties
parents:
diff changeset
26 $debug = 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
27
30497c6a3eca Initial revision
casties
parents:
diff changeset
28 # junk files
30497c6a3eca Initial revision
casties
parents:
diff changeset
29 %junk_files = (
30497c6a3eca Initial revision
casties
parents:
diff changeset
30 '.HSResource' => '.HSResource',
30497c6a3eca Initial revision
casties
parents:
diff changeset
31 '.HSancillary' => '.HSancillary',
30497c6a3eca Initial revision
casties
parents:
diff changeset
32 '.HSicon' => '.HSicon',
30497c6a3eca Initial revision
casties
parents:
diff changeset
33 'Network Trash Folder' => 'Network Trash Folder',
30497c6a3eca Initial revision
casties
parents:
diff changeset
34 'TheVolumeSettingsFolder' => 'TheVolumeSettingsFolder',
30497c6a3eca Initial revision
casties
parents:
diff changeset
35 '.DS_Store' => '.DS_Store'
30497c6a3eca Initial revision
casties
parents:
diff changeset
36 );
30497c6a3eca Initial revision
casties
parents:
diff changeset
37
30497c6a3eca Initial revision
casties
parents:
diff changeset
38 # filenames to not delete (but archive)
30497c6a3eca Initial revision
casties
parents:
diff changeset
39 %index_files = (
30497c6a3eca Initial revision
casties
parents:
diff changeset
40 'index.meta' => 'index.meta',
30497c6a3eca Initial revision
casties
parents:
diff changeset
41 'index.meta.old' => 'index.meta.old'
30497c6a3eca Initial revision
casties
parents:
diff changeset
42 );
30497c6a3eca Initial revision
casties
parents:
diff changeset
43
30497c6a3eca Initial revision
casties
parents:
diff changeset
44 # default permissions for normal index files (rw-rw-r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
45 $index_file_perm = 0664;
30497c6a3eca Initial revision
casties
parents:
diff changeset
46 # default permissions for archived index files (rw-r--r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
47 $archived_index_file_perm = 0644;
30497c6a3eca Initial revision
casties
parents:
diff changeset
48 # default permissions for other files (rw-rw-r--)
30497c6a3eca Initial revision
casties
parents:
diff changeset
49 $file_perm = 0664;
30497c6a3eca Initial revision
casties
parents:
diff changeset
50
30497c6a3eca Initial revision
casties
parents:
diff changeset
51
30497c6a3eca Initial revision
casties
parents:
diff changeset
52 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
53 # $param_hash = parseargs;
30497c6a3eca Initial revision
casties
parents:
diff changeset
54 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
55 # reads @ARGV and returns a hash with all options like "-option=value"
30497c6a3eca Initial revision
casties
parents:
diff changeset
56 # and the last part of the string as "path"
30497c6a3eca Initial revision
casties
parents:
diff changeset
57 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
58 sub parseargs {
30497c6a3eca Initial revision
casties
parents:
diff changeset
59 my %opts;
30497c6a3eca Initial revision
casties
parents:
diff changeset
60 foreach my $s (@ARGV) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
61 if ($s =~ /^-([^=]+)=*(.*)$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
62 $opts{$1} = ($2) ? $2 : $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
63 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
64 $opts{'path'} = $s;
30497c6a3eca Initial revision
casties
parents:
diff changeset
65 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
66 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
67 return \%opts;
30497c6a3eca Initial revision
casties
parents:
diff changeset
68 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
69
30497c6a3eca Initial revision
casties
parents:
diff changeset
70
30497c6a3eca Initial revision
casties
parents:
diff changeset
71 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
72 # logger($level, $message)
30497c6a3eca Initial revision
casties
parents:
diff changeset
73 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
74 # logs the $message (mostly to stdout)
30497c6a3eca Initial revision
casties
parents:
diff changeset
75 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
76 sub logger {
30497c6a3eca Initial revision
casties
parents:
diff changeset
77 my ($level, $message) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
78 if ($debug || ($level ne "DEBUG")) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
79 print "$level: $message\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
80 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
81 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
82
30497c6a3eca Initial revision
casties
parents:
diff changeset
83 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
84 # $stime = stime($utime)
30497c6a3eca Initial revision
casties
parents:
diff changeset
85 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
86 # format utime (seconds since epoch) into string
30497c6a3eca Initial revision
casties
parents:
diff changeset
87 # representation: "YYYY/MM/DD HH:MM:SS"
30497c6a3eca Initial revision
casties
parents:
diff changeset
88 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
89 sub stime {
30497c6a3eca Initial revision
casties
parents:
diff changeset
90 my ($utime) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
91 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
30497c6a3eca Initial revision
casties
parents:
diff changeset
92 localtime($utime);
30497c6a3eca Initial revision
casties
parents:
diff changeset
93 my $yy = $year + 1900;
30497c6a3eca Initial revision
casties
parents:
diff changeset
94 my $mm = $mon + 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
95 my $stime = sprintf("%d/%02d/%02d %02d:%02d:%02d",
30497c6a3eca Initial revision
casties
parents:
diff changeset
96 $yy, $mm, $mday, $hour, $min, $sec);
30497c6a3eca Initial revision
casties
parents:
diff changeset
97 return $stime;
30497c6a3eca Initial revision
casties
parents:
diff changeset
98 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
99
30497c6a3eca Initial revision
casties
parents:
diff changeset
100 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
101 # $stime = s2stime($stime2)
30497c6a3eca Initial revision
casties
parents:
diff changeset
102 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
103 # format db-like time (2003-09-19 12:43:32+02) into string
30497c6a3eca Initial revision
casties
parents:
diff changeset
104 # representation: "YYYY/MM/DD HH:MM:SS"
30497c6a3eca Initial revision
casties
parents:
diff changeset
105 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
106 sub s2stime {
30497c6a3eca Initial revision
casties
parents:
diff changeset
107 my ($s2time) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
108 my $stime = "";
30497c6a3eca Initial revision
casties
parents:
diff changeset
109 if ($s2time =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
110 my ($year,$mon,$mday,$hour,$min,$sec) = ($1, $2, $3, $4, $5, $6);
30497c6a3eca Initial revision
casties
parents:
diff changeset
111 $stime = sprintf("%d/%02d/%02d %02d:%02d:%02d",
30497c6a3eca Initial revision
casties
parents:
diff changeset
112 $year, $mon, $mday, $hour, $min, $sec);
30497c6a3eca Initial revision
casties
parents:
diff changeset
113 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
114 return $stime;
30497c6a3eca Initial revision
casties
parents:
diff changeset
115 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
116
30497c6a3eca Initial revision
casties
parents:
diff changeset
117 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
118 # $date = ymd_date($olddate)
30497c6a3eca Initial revision
casties
parents:
diff changeset
119 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
120 # converts a date string in mm/dd/yy format to yyyy/mm/dd format
30497c6a3eca Initial revision
casties
parents:
diff changeset
121 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
122 sub ymd_date {
30497c6a3eca Initial revision
casties
parents:
diff changeset
123 my ($mdy) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
124 my $ydm;
30497c6a3eca Initial revision
casties
parents:
diff changeset
125 if ($mdy =~ /(\d+)\/(\d+)\/(\d+)/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
126 my $m = $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
127 my $d = $2;
30497c6a3eca Initial revision
casties
parents:
diff changeset
128 my $y = $3;
30497c6a3eca Initial revision
casties
parents:
diff changeset
129 # extend yy to yyyy
30497c6a3eca Initial revision
casties
parents:
diff changeset
130 $y = ($y < 70) ? (2000 + $y) : (($y < 100) ? ($y + 1900) : $y);
30497c6a3eca Initial revision
casties
parents:
diff changeset
131 $ydm = sprintf "%d/%02d/%02d", $y, $m, $d;
30497c6a3eca Initial revision
casties
parents:
diff changeset
132 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
133 return $ydm;
30497c6a3eca Initial revision
casties
parents:
diff changeset
134 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
135
30497c6a3eca Initial revision
casties
parents:
diff changeset
136 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
137 # ($name, $path) = split_file_path($fn)
30497c6a3eca Initial revision
casties
parents:
diff changeset
138 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
139 # splits the file path $fn into
30497c6a3eca Initial revision
casties
parents:
diff changeset
140 # file name (the last path element) and path
30497c6a3eca Initial revision
casties
parents:
diff changeset
141 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
142 sub split_file_path {
30497c6a3eca Initial revision
casties
parents:
diff changeset
143 my ($fn) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
144
30497c6a3eca Initial revision
casties
parents:
diff changeset
145 if ($fn =~ /^(.*)\/([^\/]+)$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
146 return ($2, $1);
30497c6a3eca Initial revision
casties
parents:
diff changeset
147 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
148 return $fn;
30497c6a3eca Initial revision
casties
parents:
diff changeset
149 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
150
30497c6a3eca Initial revision
casties
parents:
diff changeset
151
30497c6a3eca Initial revision
casties
parents:
diff changeset
152 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
153 # $name = sstrip($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
154 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
155 # strips leading and trailing whitespace from $name
30497c6a3eca Initial revision
casties
parents:
diff changeset
156 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
157 sub sstrip {
30497c6a3eca Initial revision
casties
parents:
diff changeset
158 my ($name) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
159
30497c6a3eca Initial revision
casties
parents:
diff changeset
160 if ($name =~ /\s*(.*)\s*/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
161 return $1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
162 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
163 return $name;
30497c6a3eca Initial revision
casties
parents:
diff changeset
164 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
165
30497c6a3eca Initial revision
casties
parents:
diff changeset
166
30497c6a3eca Initial revision
casties
parents:
diff changeset
167 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
168 # $valid = valid_file_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
169 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
170 # checks if $name is a valid file name
30497c6a3eca Initial revision
casties
parents:
diff changeset
171 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
172 sub valid_file_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
173 return valid_name($_[0], 0);
30497c6a3eca Initial revision
casties
parents:
diff changeset
174 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
175
30497c6a3eca Initial revision
casties
parents:
diff changeset
176
30497c6a3eca Initial revision
casties
parents:
diff changeset
177 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
178 # $valid = valid_dir_name($name)
30497c6a3eca Initial revision
casties
parents:
diff changeset
179 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
180 # checks if $name is a valid directory name
30497c6a3eca Initial revision
casties
parents:
diff changeset
181 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
182 sub valid_dir_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
183 return valid_name($_[0], 1);
30497c6a3eca Initial revision
casties
parents:
diff changeset
184 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
185
30497c6a3eca Initial revision
casties
parents:
diff changeset
186
30497c6a3eca Initial revision
casties
parents:
diff changeset
187 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
188 # $valid = valid_name($name, $mode)
30497c6a3eca Initial revision
casties
parents:
diff changeset
189 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
190 # checks if $name is a valid file ($mode=0) or directory name (mode=1)
30497c6a3eca Initial revision
casties
parents:
diff changeset
191 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
192 sub valid_name {
30497c6a3eca Initial revision
casties
parents:
diff changeset
193 my ($name, $mode) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
194
30497c6a3eca Initial revision
casties
parents:
diff changeset
195 # whitespace?
30497c6a3eca Initial revision
casties
parents:
diff changeset
196 if ($name =~ /\s+/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
197 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
198 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
199 # invalid characters
30497c6a3eca Initial revision
casties
parents:
diff changeset
200 if ($name !~ /^[-\w.\/]+$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
201 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
202 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
203 # files have extension
30497c6a3eca Initial revision
casties
parents:
diff changeset
204 if ($mode == 0) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
205 if ($name !~ /\.[-\w]+$/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
206 return 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
207 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
208 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
209 return 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
210 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
211
30497c6a3eca Initial revision
casties
parents:
diff changeset
212
30497c6a3eca Initial revision
casties
parents:
diff changeset
213 #
10
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
214 # $newfilename = park_file($filename)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
215 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
216 # parks a file under a new name (*.bak)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
217 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
218 sub park_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
219 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
220 my $newfn = "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
221 if (-f $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
222 $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
223 if (! rename $filename, $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
224 logger("ABORT", "unable to rename file $filename!");
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
225 exit 1;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
226 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
227 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
228 return $newfn;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
229 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
230
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
231 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
232 # $filename = unpark_file($filename)
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
233 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
234 # unparks a file
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
235 #
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
236 sub unpark_file {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
237 my ($filename) = @_;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
238 my $newfn = "$filename.bak";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
239 if (-f $newfn) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
240 if (! rename $newfn, $filename) {
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
241 logger("ABORT", "unable to rename file $newfn!");
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
242 exit 1;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
243 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
244 return $filename;
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
245 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
246 return "";
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
247 }
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
248
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
249
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
250
4417be0e2f07 adapted to dsmc's problem with mount points
casties
parents: 0
diff changeset
251 #
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
252 # ($document, $rootnode) = read_xml($file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
253 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
254 # reads xml meta file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
255 # returns root node element
30497c6a3eca Initial revision
casties
parents:
diff changeset
256 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
257 sub read_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
258 my ($file) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
259 my $document;
30497c6a3eca Initial revision
casties
parents:
diff changeset
260
30497c6a3eca Initial revision
casties
parents:
diff changeset
261 my $parser = XML::LibXML->new();
30497c6a3eca Initial revision
casties
parents:
diff changeset
262 if (! $parser) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
263 logger("ABORT", "unable to create parser for metadata index file!!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
264 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
265 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
266
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
267 logger("DEBUG", "index file: $file");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
268 eval { $document = $parser->parse_file($file) };
30497c6a3eca Initial revision
casties
parents:
diff changeset
269 # catch parsing errors
30497c6a3eca Initial revision
casties
parents:
diff changeset
270 if ($@) {
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
271 logger("ABORT", "XML syntax error in file $file!!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
272 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
273 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
274 my $rootnode = $document->documentElement;
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
275 logger("DEBUG", "root element: \'" . $rootnode->nodeName . "\'");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
276
30497c6a3eca Initial revision
casties
parents:
diff changeset
277 return ($document, $rootnode);
30497c6a3eca Initial revision
casties
parents:
diff changeset
278 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
279
30497c6a3eca Initial revision
casties
parents:
diff changeset
280
30497c6a3eca Initial revision
casties
parents:
diff changeset
281 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
282 # write_xml($document, $file)
30497c6a3eca Initial revision
casties
parents:
diff changeset
283 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
284 # saves the XML tree of $document to the file $file
30497c6a3eca Initial revision
casties
parents:
diff changeset
285 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
286 sub write_xml {
30497c6a3eca Initial revision
casties
parents:
diff changeset
287 my ($document, $filename) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
288 # keep backup copy
30497c6a3eca Initial revision
casties
parents:
diff changeset
289 if (-f $filename) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
290 if (! rename $filename, "$filename.old") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
291 logger("ABORT", "unable to change file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
292 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
293 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
294 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
295 # write new file
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
296 my ($fn, $dir) = split_file_path($filename);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
297 if (-d $dir && $document->toFile($filename)) {
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
298 logger("INFO", "written new file $filename");
30497c6a3eca Initial revision
casties
parents:
diff changeset
299 chmod $index_file_perm, $filename;
30497c6a3eca Initial revision
casties
parents:
diff changeset
300 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
301 logger("ABORT", "unable to write file $filename!");
30497c6a3eca Initial revision
casties
parents:
diff changeset
302 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
303 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
304 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
305
12
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
306 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
307 # $elem = add_text_element($node, $name, $value, $namespace)
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
308 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
309 # creates an XML element with the name $name and the text content
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
310 # $value attached to the node $node and returns it.
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
311 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
312 sub add_text_element {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
313 my ($node, $name, $value, $namespace) = @_;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
314
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
315 my $doc = $node->ownerDocument;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
316 my $text = $doc->createTextNode($value);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
317 my $newnode = $node->addNewChild($namespace, $name);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
318 $newnode->addChild($text);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
319
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
320 return $newnode;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
321 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
322
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
323 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
324 # $elem = create_element_path($path, $root, $namespace)
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
325 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
326 # creates and returns a DOM element at the given path from the
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
327 # given root. path is e.g. meta/bib@type=book. elements are separated
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
328 # by /, an additional attribute can be specified after the @.
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
329 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
330 sub create_element_path {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
331 my ($path, $root, $namespace) = @_;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
332 my $attribute = "";
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
333 # get attribute
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
334 if ($path =~ /^(.+)@(.+)$/) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
335 $attribute = $2;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
336 $path = $1;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
337 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
338 my $point = $root;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
339 for my $p (split /\//, $path) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
340 # check if the next path element exists
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
341 my $n = ($point->findnodes($p))[0];
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
342 if ($n) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
343 $point = $n;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
344 } else {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
345 # create if it doesn't exist
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
346 $point = $point->addNewChild($namespace, $p);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
347 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
348 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
349 # add the attribute
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
350 if ($attribute) {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
351 my $dom = $root->getOwner();
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
352 my ($attkey, $attval) = split /=/, $attribute; #/ silly fontlock...
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
353 $point->addChild($dom->createAttributeNS($namespace, $attkey, $attval));
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
354 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
355 return $point;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
356 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
357
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
358 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
359 # $elem = create_text_path($path, $text, $root, $namespace)
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
360 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
361 # creates and returns a DOM text element with the given content at the
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
362 # given path from the given root.
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
363 #
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
364 sub create_text_path {
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
365 my ($path, $text, $root, $namespace) = @_;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
366 my $elem = create_element_path($path, $root, $namespace)->appendTextNode($text);
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
367 return $elem;
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
368 }
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
369
620aad237f57 new tool makemeta-lib.pl to create index meta entries from the libraries online sources database
casties
parents: 10
diff changeset
370
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
371
30497c6a3eca Initial revision
casties
parents:
diff changeset
372 # module init
30497c6a3eca Initial revision
casties
parents:
diff changeset
373 return 1;