annotate ipcluster/tools/zip_features_folderwise.py @ 0:e34cf1b6fe09 tip

commit
author Daniel Wolff
date Sat, 20 Feb 2016 18:14:24 +0100
parents
children
rev   line source
Daniel@0 1 #!/usr/local/python
Daniel@0 2 # -*- coding: utf-8 -*-
Daniel@0 3 __author__="wolffd"
Daniel@0 4 import os
Daniel@0 5 import sys
Daniel@0 6 import time
Daniel@0 7 import shutil
Daniel@0 8 import hashlib
Daniel@0 9 import re
Daniel@0 10 import errno
Daniel@0 11 import subprocess
Daniel@0 12
Daniel@0 13
Daniel@0 14 # this copies features which used to be stored with the audio
Daniel@0 15 # usage: copy_features source_path dest_path transform_substr ext move?
Daniel@0 16 # params:
Daniel@0 17 # transfrom_substr can be either a substring or hash of the transform file
Daniel@0 18 # or the filename.n3 of a transform file
Daniel@0 19
Daniel@0 20
Daniel@0 21 def main(audiopath, out_path, transform_substr = "", move = 0):
Daniel@0 22
Daniel@0 23
Daniel@0 24
Daniel@0 25 # replace transform_substr by pluginhash
Daniel@0 26 if transform_substr.endswith (".n3"):
Daniel@0 27 BLOCKSIZE = 65536
Daniel@0 28 hasher = hashlib.sha1()
Daniel@0 29 with open(transform_substr, 'rb') as afile:
Daniel@0 30 buf = afile.read(BLOCKSIZE)
Daniel@0 31 while len(buf) > 0:
Daniel@0 32 hasher.update(buf)
Daniel@0 33 buf = afile.read(BLOCKSIZE)
Daniel@0 34 transform_substr = str(hasher.hexdigest())
Daniel@0 35
Daniel@0 36 # ---
Daniel@0 37 # we traverse the file structure
Daniel@0 38 # and list files to copy
Daniel@0 39 # ---
Daniel@0 40 data = []
Daniel@0 41 count = 0
Daniel@0 42 count2 = 0
Daniel@0 43 for (dirpath, dirnames, filenames) in os.walk(audiopath):
Daniel@0 44 for dir in dirnames:
Daniel@0 45 print '\rChecked %d, gathered %d files' % (count, count2),
Daniel@0 46 count += 1
Daniel@0 47
Daniel@0 48 # we copy all requested files and the transform files as well!
Daniel@0 49 if (transform_substr and (transform_substr in dir)):
Daniel@0 50 source = os.path.join(dirpath, dir).replace('\\','/')
Daniel@0 51 data.append(source)
Daniel@0 52 count2 +=1
Daniel@0 53 # if count2 > 1:
Daniel@0 54 # break
Daniel@0 55
Daniel@0 56 # count jobs
Daniel@0 57 njobs = len(data)
Daniel@0 58 print '\r\nAbout to copy %d directories' % (njobs)
Daniel@0 59
Daniel@0 60
Daniel@0 61 count = 0
Daniel@0 62 # copy individual items
Daniel@0 63 for x in data:
Daniel@0 64 spath = os.path.split(x)
Daniel@0 65 folders = spath[0].split('/')
Daniel@0 66
Daniel@0 67 collectionname = folders[-1]
Daniel@0 68
Daniel@0 69 # create the target folder
Daniel@0 70 #try:
Daniel@0 71 # os.makedirs(out_path)
Daniel@0 72 #except OSError as exception:
Daniel@0 73 # if exception.errno!= errno.EEXIST:
Daniel@0 74 # raise
Daniel@0 75
Daniel@0 76 command = '7z -r -mmt=30 -mx5 -v50g -mm=bzip2 a ' + '"' + os.path.join(out_path,collectionname + '_'+ transform_substr + '.zip') + '"' + ' ' + x
Daniel@0 77 print command
Daniel@0 78 print " Currently zipping " + collectionname
Daniel@0 79
Daniel@0 80 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
Daniel@0 81 text = p.stdout.read()
Daniel@0 82 retcode = p.wait()
Daniel@0 83 if not (retcode==0):
Daniel@0 84 print "Error: " + text
Daniel@0 85
Daniel@0 86 #os.system(command)
Daniel@0 87 #print 'Removing ' + dest
Daniel@0 88 #shutil.rmtree(dest)
Daniel@0 89
Daniel@0 90 count = count + 1
Daniel@0 91
Daniel@0 92 # progress indicator
Daniel@0 93 print '\r%3.2f percent. %d done, %d pending' % (count/(njobs*1.0) * 100.0,count, njobs-count),
Daniel@0 94
Daniel@0 95 print '\rCopied %d of %d files.' % (count,njobs)
Daniel@0 96 print '\n'
Daniel@0 97
Daniel@0 98 def copytree(src, dst, symlinks=False, ignore=None):
Daniel@0 99 for item in os.listdir(src):
Daniel@0 100 s = os.path.join(src, item)
Daniel@0 101 d = os.path.join(dst, item)
Daniel@0 102 if os.path.isdir(s):
Daniel@0 103 shutil.copytree(s, d, symlinks, ignore)
Daniel@0 104 else:
Daniel@0 105 os.makedirs(d)
Daniel@0 106 shutil.copy2(s, d)
Daniel@0 107
Daniel@0 108 if __name__ == "__main__":
Daniel@0 109 if len(sys.argv) >= 5:
Daniel@0 110 main(sys.argv[1],sys.argv[2], sys.argv[3], int(sys.argv[4]))
Daniel@0 111 else:
Daniel@0 112 main(audiopath = 'D:/_Audio/',out_path = 'D:/_Audio_Analysis/', transform_substr = "", ext = "")
Daniel@0 113