-
Notifications
You must be signed in to change notification settings - Fork 171
/
Mbooklist.py
executable file
·411 lines (311 loc) · 14.3 KB
/
Mbooklist.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python
'''List properties of Madagascar example book directories.
Scan a directory tree (or list of directory trees) to a given depth,
inventory the contents, and optionally execute a command in each leaf.
The inventory and optional command occurs only at the specified depth
(default 3), not the intervening depths. Directories named .svn are
skipped. Only directories containing an SConstruct file are listed or
executed.
Optional directory filters controlling inventory or command execution
may be specified based on existence of .rsfproj file, sf programs used,
type of external data required, and total rsf data-file size of the
completed example. A an optional input text file may also be specified
containing a list of examples to skip.
The optional command is executed in /bin/sh.
Examples (from within $RSFSRC):
sfbooklist book # inventory of book
sfbooklist levels=2 book/geostats # inventory of book/geostats
sfbooklist command=scons book # build examples with default filters
sfbooklist size=5 command=scons book # build examples smaller than 5MB
'''
# Copyright (C) 2010 James W. Jennings Jr.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os, copy, sys, signal
import rsf.prog as rsfprog
if not hasattr(os,'walk'):
sys.stderr.write('booklist needs Python 2.3 or greater\n')
sys.exit(unix_error)
import rsf.api as rsf
sfprefix = 'sf' # prefix for rsf commands
plprefix = 'vp' # prefix for vpl commands
sfsuffix = '.rsf' # suffix for rsf files
vpsuffix = '.vpl' # suffix for vplot files
RSFROOT = rsfprog.RSFROOT # get root directory of Madagascar installation
###############################################################################
def handler(signum, frame):
'signal handler for abortion [Ctrl-C]'
sys.stderr.write('\n[Ctrl-C] Aborting...\n')
if child:
os.kill (child,signal.SIGINT)
sys.exit(-1)
signal.signal(signal.SIGINT,handler) # handle interrupt
child = None
def command_wait(comm):
'Interruptable system command'
global child
child = os.fork()
if child:
t0 = os.times()
(pid,exit) = os.waitpid(child,0)
t1 = os.times()
child = 0
dt_user = t1[2]-t0[2]
dt_sys = t1[3]-t0[3]
dt_real = t1[4]-t0[4]
return (exit,dt_user,dt_sys,dt_real)
else:
status = os.system(comm)
if (status==0): os._exit(0)
else: os._exit(1)
def size_string(size):
'Make a human-readable size string'
units = [' B','KB','MB','GB','TB']
p = 1
while 1024**p <= size : p = p+1
return "%4.2f %s" % (float(size)/1024**(p-1),units[p-1])
def read_rsfproj(root,files):
'Read contents of an .rsfproj file'
error = 0 # defaults
rsfproj_exist = 'no'
uses_list = []
data_type = 'unknown'
data_size = 0
if '.rsfproj' in files: # get info from .rsfproj
rsfproj_exist = 'yes'
g = {} # read file
l = {}
execfile(os.path.join(root,'.rsfproj'),g,l)
# get uses
if 'uses' in l: uses_list = l['uses']
else: error = 1
# get data type
if 'data' in l:
if len(l['data']) == 0: data_type = 'none'
if len(l['data']) > 0: data_type = 'public'
if 'PRIVATE' in l['data']: data_type = 'private'
if 'LOCAL' in l['data']: data_type = 'local'
else:
error = 1
# get data size
if 'size' in l: data_size = l['size']
else: error = 1
return (error,rsfproj_exist,uses_list,data_type,data_size)
def calc_filter(options,props):
'Calculate command filter'
filter = True
(skiplist,rsfproj,uses,size,fetch_none,fetch_public,fetch_private,fetch_local) = options
(root,rsfproj_exist,uses_list,data_type,data_size) = props
# skiplist filter
if root in skiplist: filter = False
# rsfproj existence filter
if (rsfproj != 'both') and (rsfproj != rsfproj_exist): filter = False
# uses filter
if (uses != 'any') and (uses not in uses_list): filter = False
# total rsf data file size filter
if (data_size > size): filter = False
# external data type filter
if (fetch_none == False) and (data_type == 'none'): filter = False
if (fetch_public == False) and (data_type == 'public'): filter = False
if (fetch_private == False) and (data_type == 'private'): filter = False
if (fetch_local == False) and (data_type == 'local'): filter = False
return filter
def rsftimer_write(root,exit,dt_user,dt_sys,dt_real):
'write an .rsftimer file'
import time
rsftimer_file = open(os.path.join(root,'.rsftimer'),'a')
string = "%s %3d %8.2f %8.2f %8.2f %s\n"
rsftimer_file.write(string % (time.asctime(),exit,dt_user,dt_sys,dt_real,root))
rsftimer_file.close()
return
###############################################################################
def main(argv=sys.argv):
unix_success = 0
unix_error = 1
################ get user parameters
if len(argv) < 2 : # print selfdoc and exit if no parameters
rsfprog.selfdoc()
return unix_error
par = rsf.Par(argv) # get parameters
levels = par.int('levels',3)
# directory search depth
list = par.string('list')
# how much to list [all,filter,none], default = all
timer = par.string('timer')
# output execution time [log,file,none], default = none
rsfproj = par.string('rsfproj')
# rsfproj filter [yes,no,both], default = yes
size = par.int('size',1024**2)
# max data size filter (MB)
size = size*1024**2
uses = par.string('uses')
# uses filter, default = any
fetch_none = par.bool('nofetch',True)
# fetch-no-data filter
fetch_public = par.bool('public',False)
# fetch-public-data filter
fetch_private = par.bool('private',False)
# fetch-private-data filter
fetch_local = par.bool('local',False)
# fetch-local-data filter
command = par.string('command')
# command to execute in each directory, default = none
skipfile = par.string('skipfile')
# file with list of directories to skip
if list is None: list='all'
if list not in ['all','filter','none']:
sys.stderr.write('Unknown list option: %s\n' % list)
return unix_error
if timer is None: timer='none'
if timer not in ['log','file','none']:
sys.stderr.write('Unknown timer option: %s\n' % timer)
return unix_error
if rsfproj is None: rsfproj='yes'
if rsfproj not in ['yes','no','both']:
sys.stderr.write('Unknown rsfproj option: %s\n' % rsfproj)
return unix_error
if uses is None: uses='any'
if fetch_none is None: fetch_none = True
if fetch_public is None: fetch_public = False
if fetch_private is None: fetch_private = False
if fetch_local is None: fetch_local = False
################ build list of search directories
books = []
for item in argv[1:]:
if '=' in item: continue # skip parameter items
if not os.path.exists(item): # does item exist?
sys.stderr.write("Directory '%s' does not exist.\n" % item)
continue
if not os.path.isdir(item): # is item a directory?
sys.stderr.write("File '%s' is not a directory.\n" % item)
continue
books.append(os.path.normpath(item))
if len(books) == 0: # nothing to search
sys.stderr.write("No directories to search.\n")
return unix_error
sys.stdout.write("Searching in: %s\n\n" % books)
sys.stdout.flush()
################ read skipfile
g = {}
l = {}
if skipfile is not None: execfile(skipfile,g,l)
if 'skiplist' in l: skiplist = l['skiplist']
else: skiplist = []
################ get list of tools installed in $RSFROOT
tool_list = os.listdir(os.path.join(RSFROOT,"bin"))
################ search directory tree
if (list == 'all') or (list == 'filter'):
sys.stdout.write('command rsfproj data size directory\n')
sys.stdout.flush()
total_list = 0
total_size = 0
pass_list = 0
pass_size = 0
rsfproj_yes = 0
rsfproj_no = 0
rsfproj_error = 0
command_error = 0
data_unknown = 0
data_none = 0
data_public = 0
data_private = 0
data_local = 0
for bookdir in books:
for root,dirs,files in os.walk(bookdir):
if '.svn' in dirs: dirs.remove('.svn') # don't visit .svn dirs
reldir = root.replace(bookdir,"dummy",1) # get relative dir name
level = len(reldir.split(os.sep))-1 # get dir level
if level < levels: continue # scan to depth 'levels' ...
if level == levels: del dirs[:] # ... but not beyond 'levels'
# skip dirs without SConstruct
if 'SConstruct' not in files: continue
# read rsfproj file
tuple = read_rsfproj(root,files)
(error,rsfproj_exist,uses_list,data_type,data_size) = tuple
if error==1:
rsfproj_error = rsfproj_error+1
string = " ********* .rsfproj error ********* %s\n"
sys.stdout.write(string % root)
sys.stdout.flush()
tools = 'yes'
for item in uses_list:
if (item not in tool_list): tools = 'no '
# calculate directory filter
options = (skiplist,rsfproj,uses,size,fetch_none,fetch_public,fetch_private,fetch_local)
props = (root,rsfproj_exist,uses_list,data_type,data_size)
filter = calc_filter(options,props)
if filter==True:
pass_list = pass_list+1
pass_size = pass_size+data_size
# print data for each directory
if (list == 'all') or ((list == 'filter') and (filter == True)):
total_list = total_list+1
total_size = total_size+data_size
if filter: filter_command = 'yes'
else: filter_command = 'no '
if rsfproj_exist == 'yes' : rsfproj_yes = rsfproj_yes+1
if rsfproj_exist == 'no' : rsfproj_no = rsfproj_no+1
if data_type == 'unknown' : data_unknown = data_unknown+1
if data_type == 'none' : data_none = data_none+1
if data_type == 'public' : data_public = data_public+1
if data_type == 'private' : data_private = data_private+1
if data_type == 'local' : data_local = data_local+1
tuple = (filter_command,rsfproj_exist,
data_type,size_string(data_size),root)
sys.stdout.write('%s %-3s %10s %-7s %s\n' % tuple)
sys.stdout.flush()
# execute command in directory
if (filter is True) and (command is not None):
string = " +++++++++ running command +++++++++ %s\n"
sys.stdout.write(string % root)
sys.stdout.flush()
tuple = command_wait(' '.join(['cd',root,';',command]))
(exit,dt_user,dt_sys,dt_real) = tuple
if (timer == 'log'):
string = " user %6.2f sys %6.2f real %6.2f %s\n"
sys.stdout.write(string % (dt_user,dt_sys,dt_real,root))
if (timer == 'file'):
rsftimer_write(root,exit,dt_user,dt_sys,dt_real)
if (exit==0):
string = " --------- command success --------- %s\n"
else:
string = " ********* command error ********* %s\n"
command_error = command_error+1
sys.stdout.write(string % root)
sys.stdout.write("\n")
sys.stdout.flush()
sys.stdout.write("\n")
sys.stdout.write("Directories listed : %3d\n" % total_list)
sys.stdout.write("Total data size : %s\n" % size_string(total_size))
sys.stdout.write("\n")
sys.stdout.write("Directories with .rsfproj : %3d\n" % rsfproj_yes)
sys.stdout.write("Directories without .rsfproj : %3d\n" % rsfproj_no)
sys.stdout.write(".rsfproj errors : %3d\n" % rsfproj_error)
sys.stdout.write("\n")
sys.stdout.write("Directories for command : %3d\n" % pass_list)
sys.stdout.write("Total data size for command : %s\n" % size_string(pass_size))
sys.stdout.write("Command errors : %3d\n" % command_error)
sys.stdout.write("\n")
sys.stdout.write("Directories using unknown external data : %3d\n" % data_unknown)
sys.stdout.write("Directories using no external data : %3d\n" % data_none)
sys.stdout.write("Directories using public external data : %3d\n" % data_public)
sys.stdout.write("Directories using private external data : %3d\n" % data_private)
sys.stdout.write("Directories using local data : %3d\n" % data_local)
sys.stdout.write("\n")
return unix_success
###############################################################################
if __name__ == '__main__':
sys.exit(main())
# $Id$