File: extractstrings.in

package info (click to toggle)
mpich 3.2-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 81,040 kB
  • ctags: 68,664
  • sloc: ansic: 358,905; f90: 54,597; perl: 18,527; cpp: 10,203; sh: 9,839; xml: 8,195; fortran: 7,799; makefile: 4,868; ruby: 53; sed: 9; php: 8
file content (295 lines) | stat: -rw-r--r-- 8,724 bytes parent folder | download | duplicates (4)
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
#! /usr/bin/env perl
# -*- Mode: perl; -*-
# 
# This script is a replacement for several scripts that process source
# files, extracting information from them.  This file provides a set 
# of routines for processing files and maintaining a cache of the results.
# It uses a more sophisticated
# approach to avoid rescanning all files by keeping track of local changes
# It does this on a directory-by-directory basis as a comprimise between
# doing the minimal work but limiting the number and size of the "extra"
# files.  In this case, for each directory, the following dot file is 
# produced (with a <name> provided depending on use:
#   .<name>-cache
# This file contains lines of the form
# <dir>
# <file name="filename" info="date or md5 hash"/>
# ...
# </dir>
# <data>
# <fileinfo name="filename">
# data for this file extracted from file
# </fileinfo>
# ...
# </data>
# The file has a separate directory to speed reading of the list of 
# know files so that the comparisons for updates can be 
# computed quickly.
#
# In addition, this allows us to create lists of information by
# combining the directories in which we're interested, rather than
# all of the directories.  This will also make it possible to 
# update these tables at configure time with modules provided by other
# developers.
#
# Algorithm:
# For each directory (use readdir)
#    Get a list of matching files (filename pattern supplied, default is
#    *.[chi])
#    if a cache file is found 
#        read cache file directory
#        compare with source files
#            for each out-of-date or new file, extract information (see below)
#            else mark file as unchanged
#            check for deleted files
#        if any changes
#            read old cache file for info from unchanged files
#            write new directory and data entries
#    else
#       for each file
#            extract information
#       write new directory and data entries
#    (optional) if specified, call routine to process cache file contents
#      (e.g., generate a derived file).
#
# Data structures
# Within a directory
#   fileInfo{filename} = extracted info
#   filesUpdated[]     = array of file names that match critera
#   fileInCache{filename} = comparison info
#   
# -------------------------------------------------------------------------
use warnings;

$gDebug     = 0;
# verbose of 1 gives the least amount of data, higher values give
# increasing amount of detail. (0, of course, gives no detail)
$gVerbose   = 0;
$gUpdateAll = 0;
# -------------------------------------------------------------------------
# Return an array of regular files in the named directory
sub GetFileNamesInDirectory {
    my ($dir,$pattern) = @_;
    my @filesFound = ();

    opendir DIR, $dir || die "Could not open $dir\n";
    while (my $file = readdir DIR) {
	if (! -f "$dir/$file") { next; }
	if ($file =~ /^\.$/ || $file =~ /^\.\.$/) { next; }
	if ($file =~ /$pattern/) {
	    $filesFound[$#filesFound+1] = $file;
	}
    }
    closedir DIR;
    return @filesFound;
}
# Return a hash of files and their comparison information
sub ReadCacheDirectory {
    my ($dir, $cachefile) = @_;
    my %fileInCache = ();
    my $found = 0;

    if (! -f "$dir/$cachefile") { return %fileInCache; }
    open CFD, "<$dir/$cachefile" || return %fileInCache;
    # Look for directory
    while (<CFD>) {
	if (/<dir>/) { $found = 1; last; }
    }
    if ($found) {
	while (<CFD>) {
	    if (/<\/dir>/) { last; }
	    if (/<file\s+name=\"([^\"]+)\"\s+info=\"([^\"]+)\"\s*\/>/) {
		$fileInCache{$1} = $2;
		print "Found file $1 in cache\n" if $gDebug;
	    }
	}
    }
    close CFD;

    return %fileInCache;
}
# Return a hash of information from each, indexed by filename
sub ReadCacheContents {
    my ($dir, $cachefile) = @_;
    my %fileInfo = ();
    my $found = 0;
    
    if (! -f "$dir/$cachefile") { return %fileInfo; }
    open CFD, "<$dir/$cachefile" || return %fileInfo;
    # Look for data
    while (<CFD>) {
	if (/<data>/) { $found = 1; last; }
    }
    if ($found) {
	while (<CFD>) {
	    if (/<\/data>/) { last; }
	    if (/<fileinfo\s+name=\"([^\"]+)\">/) {
		my $filename = $1;
		my $info = "";
		while (<CFD>) {
		    if (/<\/fileinfo>/) { last; }
		    $info .= $_;
		}
		$fileInfo{$filename} = $info;
	    }
	}
    }
    close CFD;

    return %fileInfo;
}
# Print the cache.  Pass fileInfo by reference (\%fileInfo)
sub PrintCacheFile {
    my ($dir,$cachefile,$fileInfo) = @_;

    open CFD, ">$dir/$cachefile" || die "Could not open $dir/$cachefile\n";

    print CFD "<dir>\n";
    foreach my $file (keys(%$fileInfo)) {
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime) = 
	    stat "$dir/$file";
	print CFD "<file name=\"$file\" info=\"$mtime\"/>\n";
    }
    print CFD "</dir>\n";
    print CFD "<data>\n";
    foreach my $file (keys(%$fileInfo)) {
	print CFD "<fileinfo name=\"$file\">\n";
	print CFD $$fileInfo{$file};
	print CFD "</fileinfo>\n";
    }
    print CFD "</data>\n";
    close CFD;
}

sub processFiles {
    my ($dir,$cachefile,$pattern) = @_;

    my @files = &GetFileNamesInDirectory( $dir, $pattern );
    my %filesInCache = &ReadCacheDirectory( $dir, $cachefile );
    
    my @filesToScan = ();
    my @filesDeleted = ();
    my @filesUnchanged = ();
    my %filesInDir = ();

    print "Number of files matching pattern in $dir is $#files\n" if $gDebug;
    for (my $i=0; $i <= $#files; $i++) {
	$filesInDir{$files[$i]} = 1;
    }
    foreach my $file (keys(%filesInCache)) {
	# Get info on file
	if (-f "$dir/$file") {
	    print "Found $file\n" if $gDebug;
	    delete $filesInDir{$file};
	    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime) = 
		stat "$dir/$file";
	    if ($mtime > $filesInCache{$file} || $gUpdateAll) {
		# File has been changed since last update
		$filesToScan[$#filesToScan+1] = $file;
	    }
	    else {
		$filesUnchanged[$#filesUnchanged+1] = $file;
	    }
	}
	else {
	    # This file has been deleted
	    print "File $dir/$file has been deleted since the cache was created\n" if $gDebug;
	    $filesDeleted[$#filesDeleted+1] = $file;
	}
    }
    my @filesCreated = keys(%filesInDir);
    
    # Check for unchanged
    if ($#filesCreated == -1 &&
	$#filesDeleted == -1 &&
	$#filesToScan  == -1) {
	# Nothing to do, we can leave the cache file as is
	print "Cache in $dir unchanged\n" if $gDebug || $gVerbose > 1;
    }
    else {
	if ($gDebug || $gVerbose > 0) {
	    # Give a detailed description
	    my $sep = "";
	    print "Changes in dir $dir: ";
	    if ($#fileCreated >= 0) {
		my $num = $#filesCreated + 1;
		print "created = $num";
		$sep = ", ";
		}
	    if ($#filesDeleted >= 0) {
		my $num = $#filesDeleted + 1;
		print "${sep}deleted = $num";
		$sep = ", ";
	    }
	    if ($#filesToScan >= 0) {
		my $num = $#filesToScan + 1;
		print "${sep}changed = $num";
	    }
	    print "\n";
	}
	# We need to scan some files, adding their info to fileInfo
	my %fileInfo = &ReadCacheContents( $dir, $cachefile );
	for (my $i=0; $i<=$#filesDeleted; $i++) {
	    delete $fileInfo{$filesDeleted[$i]};
	}
	foreach $file (@filesCreated,@filesToScan) {
	    print "Scanning file $dir/$file\n" if $gDebug;
	    $fileInfo{$file} = &$scanFile( "$dir/$file" );
	}
	&PrintCacheFile( $dir, $cachefile, \%fileInfo );
    }
}
sub processDirs {
    my ($dir, $cachefile, $pattern) = @_;

    print "Processing $dir...\n" if $gDebug;
    my @dirs = ();
    # Find the directories
    opendir DIR, "$dir" || die "Cannot open $dir\n";
    while (my $file = readdir DIR) {
	if (! -d "$dir/$file") { next; }
	if ($file =~ /^\./) { next; }
	if ($file =~ /^.svn/) { next; }
	if ($file =~ /autom4te.cache/) { next; }
	$dirs[$#dirs+1] = "$file";
    }
    closedir DIR;
    
    # For each of these, process it
    for (my $i=0; $i<=$#dirs; $i++) {
	my $ndir = "$dir/$dirs[$i]";
	&processDirs( $ndir, $cachefile, $pattern );
    }

    # process the files in this directory
    &processFiles( $dir, $cachefile, $pattern );
}

# This is a general routine to process directories
sub processDirsAndAction {
    my ($dir, $action, $actionData) = @_;

    print "Processing $dir...\n" if $gDebug;
    my @dirs = ();
    # Find the directories
    opendir DIR, "$dir" || die "Cannot open $dir\n";
    while (my $file = readdir DIR) {
	if (! -d "$dir/$file") { next; }
	if ($file =~ /^\./) { next; }
	if ($file =~ /^.svn/) { next; }
	if ($file =~ /autom4te.cache/) { next; }
	$dirs[$#dirs+1] = "$file";
    }
    closedir DIR;
    
    # For each of these, process it
    for (my $i=0; $i<=$#dirs; $i++) {
	my $ndir = "$dir/$dirs[$i]";
	&processDirsAndAction( $ndir, $action, $actionData );
    }

    # process the files in this directory
    &$action( $dir, $actionData );
}

1;