source: main/trunk/greenstone3/devel/bin/script/gs3-check-interface-text-keys.pl@ 28901

Last change on this file since 28901 was 28901, checked in by kjdon, 10 years ago

script I wrote to check usage of interface keys. Was ages ago so I can't remember exactly what it does. Maybe finds all the unused keys??

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1#!/usr/bin/perl -w
2
3package xxx;
4
5BEGIN {
6 die "GSDL3HOME not set\n" unless defined $ENV{'GSDL3HOME'};
7 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
8 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
9}
10
11use util;
12
13use strict;
14no strict 'refs'; # allow filehandles to be variables and vice versa
15no strict 'subs'; # allow barewords (eg STDERR) as function arguments
16
17my $keys_hash ={};
18my $keys_not_found = {};
19
20&main();
21
22sub main {
23
24
25 my ($interface_name) = $ARGV[0];
26 print STDERR "interface _name = $interface_name\n";
27
28 &load_interface_dictionary($interface_name);
29 &load_interface_dictionary($interface_name."2");
30
31 my $transform_dir = &util::filename_cat($ENV{'GSDL3HOME'}, "interfaces", $interface_name, "transform");
32
33 &scan_directory($transform_dir);
34
35 &output_results($interface_name);
36}
37
38sub load_interface_dictionary {
39 my ($interface_name) = @_;
40
41 my $dictionary_file = &util::filename_cat($ENV{'GSDL3HOME'}, 'WEB-INF', 'classes', "interface_$interface_name.properties");
42 print STDERR "dict file = $dictionary_file\n";
43 if (!-e $dictionary_file) {
44 die "Error, $dictionary_file does not exist\n";
45 }
46
47 unless (open(FIN, "<$dictionary_file")) {
48 die "Error, couldn't read in $dictionary_file\n";
49 }
50
51
52 while (my $line = <FIN>) {
53 next if $line =~ /^#/;
54 my ($key) = $line =~ /^([^=]*)=/;
55 print STDERR "key = $key\n" if defined $key;
56 $keys_hash->{$key} = 0 if defined $key;
57 }
58 close(FIN);
59
60}
61
62sub scan_directory {
63 my ($src_dir) = @_;
64 print STDERR "scan dir $src_dir\n";
65 opendir(DIR, "$src_dir");
66 my @files= readdir(DIR);
67 close(DIR);
68
69 foreach my $file (@files) {
70 # process all except . and ..
71 next if($file eq "." || $file eq ".." || $file eq ".svn");
72 # make absolute
73 $file = &util::filename_cat($src_dir, $file);
74 if (-d $file) {
75 &scan_directory($file);
76 } else {
77 &scan_file($file);
78 }
79 }
80
81}
82
83sub scan_file {
84 my ($src_file) = @_;
85 print STDERR "scan file $src_file\n";
86 open(XIN, $src_file);
87 my $num=0;
88 while (my $line = <XIN>) {
89 $num++;
90# my ($params) = ($line =~ /util:getInterfaceText\((.*)\)/);#
91# if (defined $params ) {
92# print STDERR "params = $params\n";
93# my @ps = split(/,/, $params);
94# my $kk = @ps[2];
95# print STDERR "other key = $kk\n";
96# }
97 my @matches = ($line =~ /util:getInterfaceText(?:WithDOM)?\((.*?)\)/g);
98 foreach my $m (@matches) {
99 my @params = split(/,/, $m);
100 my $kk = @params[2];
101 print STDERR "old kk = $kk, ";
102 $kk =~ s/^\s+//;
103 $kk =~ s/\s+$//;
104 $kk =~ s/^'//;
105 $kk =~ s/'$//;
106 print STDERR "new kk = $kk\n";
107 if (defined $keys_hash->{$kk}) {
108 $keys_hash->{$kk}++;
109 print STDERR "found key $kk\n";
110 } else {
111 $keys_not_found->{$kk} = "$src_file, line $num";
112 print STDERR "not found key $kk, $src_file, line $num\n";
113 }
114
115 }
116 }
117
118 close(XIN);
119}
120
121sub output_results {
122 my ($interface_name) = @_;
123 my $out_file = $interface_name."_notdefined.txt";
124 open(ROUT, ">$out_file");
125
126 foreach my $k (keys(%{$keys_not_found})) {
127 print ROUT "$k, $keys_not_found->{$k}\n";
128 }
129 close (ROUT);
130 $out_file = $interface_name."_notused.txt";
131 open (ROUT, ">$out_file");
132
133 foreach my $k (sort keys(%{$keys_hash})) {
134 if ($keys_hash->{$k} == 0) {
135 print ROUT "$k\n";
136 }
137 }
138 close ROUT;
139}
1401;
Note: See TracBrowser for help on using the repository browser.