[4cc9fe5] | 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 |
---|
[d45520a] | 17 | #sys.exit(1) |
---|
[4cc9fe5] | 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 | |
---|
[d45520a] | 24 | def list_wav_files(datapath,maxdepth=1): |
---|
| 25 | return list_files(datapath, filter="f -name '*.wav' -maxdepth %d"%maxdepth) |
---|
| 26 | |
---|
[2bd1a2a] | 27 | def list_snd_files(datapath,maxdepth=1): |
---|
| 28 | return list_files(datapath, filter="f -name '*.wav' -o -name '*.aif' -maxdepth %d"%maxdepth) |
---|
| 29 | |
---|
[d45520a] | 30 | def list_dirs(datapath): |
---|
| 31 | return list_files(datapath, filter="d") |
---|
| 32 | |
---|
[4cc9fe5] | 33 | def mkdir(path): |
---|
| 34 | cmd = '%s%s' % ('mkdir -p ',path) |
---|
| 35 | return runcommand(cmd) |
---|
| 36 | |
---|
[d45520a] | 37 | def act_on_data (action,datapath,respath,suffix='.txt',filter='f',sub='\.wav$',**keywords): |
---|
[4cc9fe5] | 38 | """ execute action(datafile,resfile) on all files in datapath """ |
---|
| 39 | dirlist = list_files(datapath,filter=filter) |
---|
[d45520a] | 40 | if dirlist == ['']: dirlist = [] |
---|
[4cc9fe5] | 41 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
| 42 | if(respath_in_datapath and suffix == ''): |
---|
| 43 | print 'error: respath in datapath and no suffix used' |
---|
[d45520a] | 44 | #sys.exit(1) |
---|
[4cc9fe5] | 45 | for i in dirlist: |
---|
[d45520a] | 46 | j = re.split(datapath, i,maxsplit=1)[1] |
---|
| 47 | j = re.sub(sub,'',j) |
---|
| 48 | #j = "%s%s%s"%(respath,j,suffix) |
---|
| 49 | j = "%s%s"%(respath,j) |
---|
| 50 | if sub != '': |
---|
| 51 | j = re.sub(sub,suffix,j) |
---|
| 52 | else: |
---|
| 53 | j = "%s%s" % (j,suffix) |
---|
| 54 | action(i,j,**keywords) |
---|
[4cc9fe5] | 55 | |
---|
| 56 | def act_on_results (action,datapath,respath,filter='d'): |
---|
| 57 | """ execute action(respath) an all subdirectories in respath """ |
---|
| 58 | dirlist = list_files(datapath,filter='d') |
---|
| 59 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
| 60 | if(respath_in_datapath and not filter == 'd' and suffix == ''): |
---|
| 61 | print 'warning: respath is in datapath' |
---|
| 62 | for i in dirlist: |
---|
| 63 | s = re.split(datapath, i ,maxsplit=1)[1] |
---|
| 64 | action("%s%s%s"%(respath,'/',s)) |
---|