Mercurial > hg > dml-open-backendtools
view ipcluster/tools/copy_features_folderwise.py @ 0:e34cf1b6fe09 tip
commit
author | Daniel Wolff |
---|---|
date | Sat, 20 Feb 2016 18:14:24 +0100 |
parents | |
children |
line wrap: on
line source
#!/usr/local/python # -*- coding: utf-8 -*- __author__="wolffd" import os import sys import time import shutil import hashlib import re import errno # this copies features which used to be stored with the audio # usage: copy_features source_path dest_path transform_substr ext move? # params: # transfrom_substr can be either a substring or hash of the transform file # or the filename.n3 of a transform file # # e.g. : D:\tools>python copy_features_folderwise.py "D:/_Audio/" "D:/Chord_Analysis/" "1a812" 0 def main(audiopath, out_path, transform_substr = "", move = 0): # check move input if move == 1: print "Move script for VAMP features on the BL server" v = raw_input("Do you really want to move folders? (y/n)") if not (v == 'y'): return else: print "Copy script for VAMP features on the BL server" # replace transform_substr by pluginhash if transform_substr.endswith (".n3"): BLOCKSIZE = 65536 hasher = hashlib.sha1() with open(transform_substr, 'rb') as afile: buf = afile.read(BLOCKSIZE) while len(buf) > 0: hasher.update(buf) buf = afile.read(BLOCKSIZE) transform_substr = str(hasher.hexdigest()) # --- # we traverse the file structure # and list files to copy # --- data = [] count = 0 count2 = 0 for (dirpath, dirnames, filenames) in os.walk(audiopath): for dir in dirnames: print '\rChecked %d, gathered %d folders' % (count, count2), count += 1 # we copy all requested files and the transform files as well! if (transform_substr and (transform_substr in dir)): source = os.path.join(dirpath, dir).replace('\\','/') data.append(source) count2 +=1 # count jobs njobs = len(data) print '\nAbout to copy or move %d directories' % (njobs) count = 0 # copy individual items for x in data: spath = os.path.split(x) folders = spath[0].split('/') # if exists, we remove the first folder # which contains "_Analysis" from the path # maxdepth contains the maximum depth of folders structure to keep, # counted from the most specific folder level max_depth = -2 skip = -2 for i in range(1, -max_depth+1): if "_Analysis" in folders[-i]: skip = -i break folderbase = [folders[j] for j in range(max_depth,skip) + range(skip+1,0)] featpath = out_path + '/'.join(folderbase) # create the target folder try: os.makedirs(featpath) except OSError as exception: if exception.errno!= errno.EEXIST: raise # copy stuff dest = featpath + '/' + spath[1] if move == 1: #print '\rMoving %s to %s' % (x,dest) shutil.move(x, dest ) if move == 0: #print '\rCopying %s to %s' % (x,dest) shutil.copytree(x, dest ) count = count + 1 # progress indicator print '\r%3.2f percent. %d done, %d pending' % (count/(njobs*1.0) * 100.0,count, njobs-count), print '\rCopied %d of %d folders.' % (count,njobs) print '\n' if __name__ == "__main__": if len(sys.argv) >= 5: main(sys.argv[1],sys.argv[2], sys.argv[3], int(sys.argv[4])) else: main(audiopath = 'D:/_Audio/',out_path = 'D:/_Audio_Analysis/', transform_substr = "", ext = "")