comparison changemeta.pl @ 39:2ee05ce0c0d2

first version of changemeta script
author casties
date Tue, 27 Jun 2006 20:06:41 +0200
parents
children 2afcb4ff750e
comparison
equal deleted inserted replaced
38:7bf843ac256b 39:2ee05ce0c0d2
1 #!/usr/local/bin/perl -w
2
3 use strict;
4 use XML::LibXML;
5
6 use lib '/usr/local/mpiwg/archive_devel';
7 use MPIWGStor;
8
9 # make output unbuffered
10 $|=1;
11
12 # program version
13 my $version = "0.1 (27.6.2006 ROC)";
14 my $help =
15 "use: checkmeta [options] dir
16 options:
17 -debug show debugging info
18 -dry-run simulate, dont'do anything
19 -access=free adds free access tag (use access=mpiwg for restricted access)
20 ";
21 logger("INFO", "checkmeta $version");
22
23
24 #######################################################
25 # internal parameters
26 #
27
28 # read command line parameters
29 my $args = MPIWGStor::parseargs;
30 if (! scalar(%$args)) {
31 print $help, "\n";
32 exit 1;
33 }
34
35 # debug level
36 $debug = (exists $$args{'debug'}) ? $$args{'debug'} : 0;
37
38 # simulate action only
39 my $dry_run = (exists $$args{'dry-run'}) ? $$args{'dry-run'} : 0;
40 logger('DEBUG', "dry-run: $dry_run");
41
42 # access type
43 my $access_type = (exists $$args{'access'}) ? $$args{'access'} : "";
44 logger('DEBUG', "access: $access_type");
45
46 # index.meta namespace (not really implemented!)
47 my $namespace = "";
48
49
50 my $xml_changed = 0;
51 my $errcnt = 0;
52 my $warncnt = 0;
53
54 #######################################################
55 # check parameters that were passed to the program
56 #
57 my $docdir = $$args{'path'};
58 if (! $docdir) {
59 logger("ABORT", "no document directory given!");
60 exit 1;
61 }
62 # strip double slashes
63 $docdir = sstrip($docdir, 1);
64 if (! -d $docdir) {
65 logger("ABORT", "document directory \'$docdir\' doesn't exist!");
66 exit 1;
67 }
68
69
70 #######################################################
71 # subroutines
72 #
73
74 #
75 # sets access tag to given access_type
76 # removes access tag if access_type is empty
77 #
78 sub change_access {
79 my ($access_type, $index_root) = @_;
80
81 my $parent_tag = $index_root->findnodes('//meta/access-conditions')->get_node(1);
82 if ($parent_tag) {
83 my $access_tag = $parent_tag->findnodes('access')->get_node(1);
84 if ($access_tag) {
85 $parent_tag->removeChild($access_tag);
86 }
87 } else {
88 $parent_tag = create_element_path('meta/access-conditions', $index_root, $namespace);
89 }
90
91 if ($access_type eq "free") {
92 create_element_path('access@type=free', $parent_tag, $namespace);
93 } elsif (length $access_type > 0) {
94 my $acc_tag = create_element_path('access@type=institution', $parent_tag, $namespace);
95 create_text_path('name', $access_type, $acc_tag, $namespace);
96 }
97 $xml_changed++
98 }
99
100
101
102
103
104
105
106 #######################################################
107 # Main
108 #
109
110 my $index_doc;
111 my $index_root;
112
113 # load index.meta file
114 if (-f "$docdir/index.meta") {
115 ($index_doc, $index_root) = read_xml("$docdir/index.meta");
116 } else {
117 logger("ABORT", "index file \'$docdir/index.meta\' doesn't exist!");
118 exit 1;
119 }
120
121 if ($access_type) {
122 change_access($index_root);
123 }
124
125
126 logger("INFO", "$warncnt warnings");
127 logger("INFO", "$errcnt errors");
128 if ($errcnt > 0) {
129 logger("ABORT", "there were errors!");
130 exit 1;
131 } elsif ($xml_changed) {
132 # write new index.meta file
133 if ($dry_run) {
134 logger('DEBUG', "would write $docdir/index.meta");
135 logger('DEBUG', $index_doc->toString(1));
136 } else {
137 #write_xml($index_doc, "$docdir/index.meta");
138 }
139 logger("DONE", "changed index file successfully!");
140 } else {
141 logger("DONE", "didn't change index file");
142 }
143