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

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