39
|
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
|
40
|
13 my $version = "0.2 (27.6.2006 ROC)";
|
39
|
14 my $help =
|
42
|
15 "use: changemeta [options] dir
|
39
|
16 options:
|
|
17 -debug show debugging info
|
|
18 -dry-run simulate, dont'do anything
|
40
|
19 -access=free set free access tag (use access=mpiwg for restricted access)
|
39
|
20 ";
|
42
|
21 logger("INFO", "changemeta $version");
|
39
|
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);
|
40
|
84 # remove access tag if it exists
|
39
|
85 if ($access_tag) {
|
|
86 $parent_tag->removeChild($access_tag);
|
|
87 }
|
|
88 } else {
|
|
89 $parent_tag = create_element_path('meta/access-conditions', $index_root, $namespace);
|
|
90 }
|
|
91
|
40
|
92 # add new access tag (if $access_Type is not empty)
|
39
|
93 if ($access_type eq "free") {
|
|
94 create_element_path('access@type=free', $parent_tag, $namespace);
|
|
95 } elsif (length $access_type > 0) {
|
|
96 my $acc_tag = create_element_path('access@type=institution', $parent_tag, $namespace);
|
|
97 create_text_path('name', $access_type, $acc_tag, $namespace);
|
|
98 }
|
|
99 $xml_changed++
|
|
100 }
|
|
101
|
|
102
|
|
103
|
|
104
|
|
105
|
|
106
|
|
107
|
|
108 #######################################################
|
|
109 # Main
|
|
110 #
|
|
111
|
|
112 my $index_doc;
|
|
113 my $index_root;
|
|
114
|
|
115 # load index.meta file
|
|
116 if (-f "$docdir/index.meta") {
|
|
117 ($index_doc, $index_root) = read_xml("$docdir/index.meta");
|
|
118 } else {
|
|
119 logger("ABORT", "index file \'$docdir/index.meta\' doesn't exist!");
|
|
120 exit 1;
|
|
121 }
|
|
122
|
|
123 if ($access_type) {
|
40
|
124 change_access($access_type, $index_root);
|
39
|
125 }
|
|
126
|
|
127
|
|
128 logger("INFO", "$warncnt warnings");
|
|
129 logger("INFO", "$errcnt errors");
|
|
130 if ($errcnt > 0) {
|
|
131 logger("ABORT", "there were errors!");
|
|
132 exit 1;
|
|
133 } elsif ($xml_changed) {
|
|
134 # write new index.meta file
|
|
135 if ($dry_run) {
|
|
136 logger('DEBUG', "would write $docdir/index.meta");
|
|
137 logger('DEBUG', $index_doc->toString(1));
|
|
138 } else {
|
40
|
139 write_xml($index_doc, "$docdir/index.meta");
|
39
|
140 }
|
|
141 logger("DONE", "changed index file successfully!");
|
|
142 } else {
|
|
143 logger("DONE", "didn't change index file");
|
|
144 }
|
|
145
|