1 | from config import * |
---|
2 | import commands,sys |
---|
3 | import re |
---|
4 | |
---|
5 | def runcommand(cmd,debug=0): |
---|
6 | if VERBOSE >= VERBOSE_CMD or debug: print cmd |
---|
7 | if debug: return |
---|
8 | status, output = commands.getstatusoutput(cmd) |
---|
9 | if status == 0 or VERBOSE >= VERBOSE_OUT: |
---|
10 | output = output.split('\n') |
---|
11 | if VERBOSE >= VERBOSE_OUT: |
---|
12 | for i in output: |
---|
13 | if i: print i |
---|
14 | if not status == 0: |
---|
15 | print 'error:',status,output |
---|
16 | print 'command returning error was',cmd |
---|
17 | sys.exit(1) |
---|
18 | return output |
---|
19 | |
---|
20 | def list_files(datapath,filter='f'): |
---|
21 | cmd = '%s%s%s%s' % ('find ',datapath,' -type ',filter) |
---|
22 | return runcommand(cmd) |
---|
23 | |
---|
24 | def mkdir(path): |
---|
25 | cmd = '%s%s' % ('mkdir -p ',path) |
---|
26 | return runcommand(cmd) |
---|
27 | |
---|
28 | def act_on_data (action,datapath,respath,suffix='.txt',filter='f'): |
---|
29 | """ execute action(datafile,resfile) on all files in datapath """ |
---|
30 | dirlist = list_files(datapath,filter=filter) |
---|
31 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
32 | if(respath_in_datapath and suffix == ''): |
---|
33 | print 'error: respath in datapath and no suffix used' |
---|
34 | sys.exit(1) |
---|
35 | for i in dirlist: |
---|
36 | s = re.split(datapath, i,maxsplit=1)[1] |
---|
37 | j = "%s%s%s%s"%(respath,'/',s,suffix) |
---|
38 | action(i,j) |
---|
39 | |
---|
40 | def act_on_results (action,datapath,respath,filter='d'): |
---|
41 | """ execute action(respath) an all subdirectories in respath """ |
---|
42 | dirlist = list_files(datapath,filter='d') |
---|
43 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
44 | if(respath_in_datapath and not filter == 'd' and suffix == ''): |
---|
45 | print 'warning: respath is in datapath' |
---|
46 | for i in dirlist: |
---|
47 | s = re.split(datapath, i ,maxsplit=1)[1] |
---|
48 | action("%s%s%s"%(respath,'/',s)) |
---|