File: xref.pl

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (339 lines) | stat: -rwxr-xr-x 7,337 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/perl
use Data::Dumper;
use File::Find ();
use File::Basename;
use DBI;

use strict;

my $host="wrep-db-misc-prod";
my $user="ecmwf";
my $pass="";

my $dbh  = DBI->connect("dbi:mysql:;host=$host",$user,$pass) or die $DBI::errstr;

sub execute_query {
	my @lines = split(/;/,$_[0]);

	foreach (@lines)
	{
		next unless($_ =~ /\w/);
		my $s = $dbh->prepare($_);
		$s->execute;
		if($_=~/\s*select\b/)
		{
			while(my @x = $s->fetchrow_array)
			{
				print "@x\n";
			}
		}
	}
}

my $DEFINITIONS = "../definitions";
my @definitions;
my %definitions;


sub createdb { 
	print "creating xref database...\n";
	my $qh=$dbh->prepare("create database xref");
	$qh->execute() or die $DBI::errstr;
	$dbh->disconnect();
	$dbh  = DBI->connect("dbi:mysql:database=xref;host=$host",$user,$pass) or die $DBI::errstr;
	my $query="create table xref (
								  keyname varchar(100),
								  type varchar(100),
								  size int not null,
								  position int not null,
								  msgtype varchar(50),
								  file varchar(250),
								  flag varchar(50),
								  ref varchar(100));
		  alter table xref add primary key (keyname,type,size,position,msgtype,file,flag,ref);
		  alter table xref add index (keyname);
		  alter table xref add index (type);
		  alter table xref add index (size);
		  alter table xref add index (position);
		  alter table xref add index (msgtype);
		  alter table xref add index (file);
		  alter table xref add index (flag);
		  alter table xref add index (ref);";
	execute_query($query);
}

my $q=$dbh->prepare("use xref");
$q->{HandleError}=\&createdb;
$q->execute();


File::Find::find({wanted => sub {push @definitions, $File::Find::name if(/\.def$/) } }, $DEFINITIONS);


foreach my $name (  @definitions )
{
	my $def = get_definition($name);
	open(IN,"<$name") or "die $name: $!";
	while(<IN>)
	{
		next if(/^\s*#/);
		if(/include\s+\"(.*)\"/)
		{
			my $dir = dirname($name);
			my $inc = get_definition("$dir/$1");
			push @{$def->{includes}},   $inc;		
			push @{$inc->{included_by}},$def;		
		}
	}
	close(IN);
}



# First report: inclusions

if(0)  { 

foreach my $p ( sort keys %definitions )
{
	my $def = $definitions{$p};

	print "$def->{name}\n";
	foreach my $x ( sort @{$def->{includes}} )
	{
		print "   includes   : $x->{name}\n";
	}

	foreach my $x ( sort @{$def->{included_by}} )
	{
		print "   included by: $x->{name}\n";
	}

}
}

print "\n\n";


# Get the definition object from a path
sub get_definition {
	my ($path) = @_;


	my $def = $definitions{$path} ;  
	unless(defined $def) {
		my $name = $path;
		$name =~ s/^$DEFINITIONS\/?//o;
		$def = bless( {name => $name, path => $path,
				includes => [], included_by => [] 
				}, "xref::definition") ;
		$definitions{$path} = $def;
	}
	return $def;
}

sub long     { @_; }
sub access   { @_; }
sub string   { @_; }
sub double   { @_; }
sub unop     { @_; }
sub binop    { @_; }
sub missing  { "missing"; }

@definitions = ();
foreach my $p ( sort keys %definitions )
{
	my $def = $definitions{$p};
	push @definitions, $def->{path} unless(@{$def->{included_by}});
}

local ($/);
open(IN,"./xref @definitions|") or die "xref: $!";
my $x = <IN>;
close(IN);

my  $xref;
eval "\$xref=[$x]";

my %xref;
foreach my $x ( @{$xref} )
{
	unless($x->isa("xref::object")) {
		my $p =  ref $x;
		eval "package $p; use base qw(xref::object);";
	}
	push @{$xref{$x->name}}, $x;
}

foreach my $name ( sort { lc $a cmp lc $b } keys %xref )
{
	my $visible = 0;
	my %edition;

	my %type;

	foreach my $x (  @{$xref{$name}} )
	{
		$visible ++ if($x->visible);
		$edition{$x->edition}++;
		$type{$x->type}++;
		
	}

	next unless($visible);

	my @edition = sort keys %edition;
	my @type    = sort keys %type;

	print "\n",$name," [@edition] [@type]\n\n";	
	foreach my $x ( sort { $a->{path} cmp $b->{path} } @{$xref{$name}} )
	{
		print "   ", $x->path, " " , $x->type , "[", $x->size, "] ";
		$x->show;
		print "\n";
		$x->insert;
	}
}


package xref::object;
sub name { my ($self) = @_; return $self->{name}; }
sub path { my ($self) = @_; return main::get_definition($self->{path})->{name} ; }
#sub visible { my ($self) = @_; return !exists $self->{flags}->{GRIB_ACCESSOR_FLAG_HIDDEN}; } 
sub visible { my ($self) = @_; return 1; } 

sub insert {
    my $insert = "insert into xref (keyname,type,size,position,msgtype,file,flag,ref) values ";
	my ($self) = @_;
	my @flags = map { s/GRIB_ACCESSOR_FLAG_//; $_; } sort keys %{$self->{flags}};
	if (!@flags) { @flags = ("none"); };
	my @params = @{$self->{params}} unless (!$self->{params}) ;
	if ($self->type eq "alias" ) {@params = ( $self->{target} )};
	if (!@params) { @params = ("none"); };
	my @values;
	foreach my $flag (@flags) {
	  foreach my $param (@params) {
	    my $value=" (\"".$self->{name}."\",\"".$self->type."\",".$self->size.",".$self->position.",\"".$self->edition."\",\"".$self->path."\",\"$flag\",\"$param\")\n";
		push (@values,$value);
	  }
	}
    $insert .= join(",",@values);
	#print "$insert\n";
	my $qh=$dbh->prepare($insert);
	$qh->{RaiseError}=0;
	$qh->{PrintError}=0;
	$qh->{HandleError}=sub {};
	$qh->execute();
}

sub show {
	my ($self) = @_;
	my @flags = map { s/GRIB_ACCESSOR_FLAG_//; $_; } sort keys %{$self->{flags}};
	my @params = @{$self->{params}};
	print join(",",@flags), " (", join(",",@params), ")";
}

sub size {
	my ($self) = @_;
	return $self->{size};
}

sub position {
	my ($self) = @_;
	if ($self->{position}) { 
	  return $self->{position};
	} else { return -1 ; }
}

sub edition {
	my ($self) = @_;
	my ($e)  = split("/",main::get_definition($self->{path})->{name});
	return $e;

}

sub type {
        my ($self) = @_;
		my $t = ref($self);
		$t =~ s/.*:://;
		return $t;
}


package xref::alias;
use base qw(xref::object);
sub show {
	my ($self) = @_;
	print "-> $self->{target}";
}

sub size {
	return -1;
}

package xref::unalias;
use base qw(xref::object);
sub show {
        my ($self) = @_;
}


package xref::label;
use base qw(xref::object);
sub visible { my ($self) = @_; return 0; }

package xref::position;
use base qw(xref::object);
#sub visible { my ($self) = @_; return 0; }

package xref::constant;
use base qw(xref::object);

package xref::pad;
use base qw(xref::object);
sub visible { my ($self) = @_; return 0; }
sub position { my ($self) = @_; return -1; }

package xref::padto;
use base qw(xref::object);
sub visible { my ($self) = @_; return 0; }
sub position { my ($self) = @_; return -1; }

package xref::padtoeven;
use base qw(xref::object);
sub visible { my ($self) = @_; return 0; }
sub position { my ($self) = @_; return -1; }

package xref::padtomultiple;
use base qw(xref::object);
sub visible { my ($self) = @_; return 0; }
sub position { my ($self) = @_; return -1; }

package xref::section_padding;
use base qw(xref::object);
sub visible { my ($self) = @_; return 0; }
sub position { my ($self) = @_; return -1; }

package xref::iterator;
use base qw(xref::object);
sub visible { my ($self) = @_; return 0; }
sub position { my ($self) = @_; return -1; }

package xref::bytes;
use base qw(xref::object);

package xref::section_length;
use base qw(xref::object);

package xref::scale;
use base qw(xref::object);
sub type { "real"; }
sub position { my ($self) = @_; return -1; }

package xref::ibmfloat;
use base qw(xref::object);
sub type { "real"; }

package xref::ieeefloat;
use base qw(xref::object);
sub type { "real"; }