view ipcluster/tools/copy_features.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.py "D:/_Audio/" "D:/Chord_Analysis/" "1a812" ".csv" 0

def main(audiopath, out_path, transform_substr = "", ext = "", 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 files? (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())
        
    # define valid extensions
    if not ext:
        ext = tuple([".n3",".csv",".mid"])
    else:
        ext = tuple([ext])
    
    # ---
    # we traverse the file structure
    # and list files to copy
    # ---
    data = []
    count = 0
    count2 = 0
    for (dirpath, dirnames, filenames) in os.walk(audiopath):
        for file in filenames:
            print '\rChecked %d, gathered %d files' % (count, count2), 
            count += 1
            
            # we copy all requested files and the transform files as well!
            if (file.endswith(ext) or (transform_substr and (transform_substr in file))) and (transform_substr in dirpath):
                source = os.path.join(dirpath, file).replace('\\','/')
                data.append(source)
                count2 +=1
    
    # count jobs
    njobs = len(data)
    print '\nAbout to copy or move %d files' % (njobs) 
    
               
    count = 0
    # copy individual items
    for x in data:
        spath = os.path.split(x)
        folders = spath[0].split('/')
        
        # we remove the first folder that contains "_Analysis" from the path 
        max_depth = -3
        skip = -2
        for i in range(1, -max_depth+1):
            if "_Analysis" in folders[-i]:
                skip = -i
                break
        
        u = [folders[j] for j in range(max_depth,skip) +  range(skip+1,0)]
        featpath = out_path + '/'.join([folders[j] for j in range(max_depth,skip) +  range(skip+1,0)])
    
        # create the target folder
        try:
            os.makedirs(featpath)
        except OSError as exception:
            if exception.errno!= errno.EEXIST:
                raise   
            
        # copy stuff        
        try:
            dest = featpath + '/' + spath[1]
            if move == 1:
                #print '\rMoving %s to %s' % (x,dest)
                shutil.move(x, dest )
            else:
                #print '\rCopying %s to %s' % (x,dest)
                shutil.copy(x, dest )
            count = count + 1
        except:
            continue
        # 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 files.' % (count,njobs)
    print '\n'
    


if __name__ == "__main__":
    if len(sys.argv) >= 5:
        main(sys.argv[1],sys.argv[2], sys.argv[3],sys.argv[4], int(sys.argv[5]))
    else:
        main(audiopath = 'D:/_Audio/',out_path = 'D:/_Audio_Analysis/', transform_substr = "",  ext = "")