Daniel@0: #!/usr/local/python Daniel@0: # -*- coding: utf-8 -*- Daniel@0: __author__="wolffd" Daniel@0: import os Daniel@0: import sys Daniel@0: import time Daniel@0: import shutil Daniel@0: import hashlib Daniel@0: import re Daniel@0: import errno Daniel@0: import subprocess Daniel@0: Daniel@0: Daniel@0: # this copies features which used to be stored with the audio Daniel@0: # usage: copy_features source_path dest_path transform_substr ext move? Daniel@0: # params: Daniel@0: # transfrom_substr can be either a substring or hash of the transform file Daniel@0: # or the filename.n3 of a transform file Daniel@0: Daniel@0: Daniel@0: def main(audiopath, out_path, transform_substr = "", move = 0): Daniel@0: Daniel@0: Daniel@0: Daniel@0: # replace transform_substr by pluginhash Daniel@0: if transform_substr.endswith (".n3"): Daniel@0: BLOCKSIZE = 65536 Daniel@0: hasher = hashlib.sha1() Daniel@0: with open(transform_substr, 'rb') as afile: Daniel@0: buf = afile.read(BLOCKSIZE) Daniel@0: while len(buf) > 0: Daniel@0: hasher.update(buf) Daniel@0: buf = afile.read(BLOCKSIZE) Daniel@0: transform_substr = str(hasher.hexdigest()) Daniel@0: Daniel@0: # --- Daniel@0: # we traverse the file structure Daniel@0: # and list files to copy Daniel@0: # --- Daniel@0: data = [] Daniel@0: count = 0 Daniel@0: count2 = 0 Daniel@0: for (dirpath, dirnames, filenames) in os.walk(audiopath): Daniel@0: for dir in dirnames: Daniel@0: print '\rChecked %d, gathered %d files' % (count, count2), Daniel@0: count += 1 Daniel@0: Daniel@0: # we copy all requested files and the transform files as well! Daniel@0: if (transform_substr and (transform_substr in dir)): Daniel@0: source = os.path.join(dirpath, dir).replace('\\','/') Daniel@0: data.append(source) Daniel@0: count2 +=1 Daniel@0: # if count2 > 1: Daniel@0: # break Daniel@0: Daniel@0: # count jobs Daniel@0: njobs = len(data) Daniel@0: print '\r\nAbout to copy %d directories' % (njobs) Daniel@0: Daniel@0: Daniel@0: count = 0 Daniel@0: # copy individual items Daniel@0: for x in data: Daniel@0: spath = os.path.split(x) Daniel@0: folders = spath[0].split('/') Daniel@0: Daniel@0: collectionname = folders[-1] Daniel@0: Daniel@0: # create the target folder Daniel@0: #try: Daniel@0: # os.makedirs(out_path) Daniel@0: #except OSError as exception: Daniel@0: # if exception.errno!= errno.EEXIST: Daniel@0: # raise Daniel@0: Daniel@0: command = '7z -r -mmt=30 -mx5 -v50g -mm=bzip2 a ' + '"' + os.path.join(out_path,collectionname + '_'+ transform_substr + '.zip') + '"' + ' ' + x Daniel@0: print command Daniel@0: print " Currently zipping " + collectionname Daniel@0: Daniel@0: p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) Daniel@0: text = p.stdout.read() Daniel@0: retcode = p.wait() Daniel@0: if not (retcode==0): Daniel@0: print "Error: " + text Daniel@0: Daniel@0: #os.system(command) Daniel@0: #print 'Removing ' + dest Daniel@0: #shutil.rmtree(dest) Daniel@0: Daniel@0: count = count + 1 Daniel@0: Daniel@0: # progress indicator Daniel@0: print '\r%3.2f percent. %d done, %d pending' % (count/(njobs*1.0) * 100.0,count, njobs-count), Daniel@0: Daniel@0: print '\rCopied %d of %d files.' % (count,njobs) Daniel@0: print '\n' Daniel@0: Daniel@0: def copytree(src, dst, symlinks=False, ignore=None): Daniel@0: for item in os.listdir(src): Daniel@0: s = os.path.join(src, item) Daniel@0: d = os.path.join(dst, item) Daniel@0: if os.path.isdir(s): Daniel@0: shutil.copytree(s, d, symlinks, ignore) Daniel@0: else: Daniel@0: os.makedirs(d) Daniel@0: shutil.copy2(s, d) Daniel@0: Daniel@0: if __name__ == "__main__": Daniel@0: if len(sys.argv) >= 5: Daniel@0: main(sys.argv[1],sys.argv[2], sys.argv[3], int(sys.argv[4])) Daniel@0: else: Daniel@0: main(audiopath = 'D:/_Audio/',out_path = 'D:/_Audio_Analysis/', transform_substr = "", ext = "") Daniel@0: