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
|
Daniel@0
|
12
|
Daniel@0
|
13 # this copies features which used to be stored with the audio
|
Daniel@0
|
14 # usage: copy_features source_path dest_path transform_substr ext move?
|
Daniel@0
|
15 # params:
|
Daniel@0
|
16 # transfrom_substr can be either a substring or hash of the transform file
|
Daniel@0
|
17 # or the filename.n3 of a transform file
|
Daniel@0
|
18 #
|
Daniel@0
|
19 # e.g. : D:\tools>python copy_features.py "D:/_Audio/" "D:/Chord_Analysis/" "1a812" ".csv" 0
|
Daniel@0
|
20
|
Daniel@0
|
21 def main(audiopath, out_path, transform_substr = "", ext = "", move = 0):
|
Daniel@0
|
22 # check move input
|
Daniel@0
|
23 if move == 1:
|
Daniel@0
|
24 print "Move script for VAMP features on the BL server"
|
Daniel@0
|
25 v = raw_input("Do you really want to move files? (y/n)")
|
Daniel@0
|
26 if not (v == 'y'):
|
Daniel@0
|
27 return
|
Daniel@0
|
28 else:
|
Daniel@0
|
29 print "Copy script for VAMP features on the BL server"
|
Daniel@0
|
30
|
Daniel@0
|
31 # replace transform_substr by pluginhash
|
Daniel@0
|
32 if transform_substr.endswith (".n3"):
|
Daniel@0
|
33 BLOCKSIZE = 65536
|
Daniel@0
|
34 hasher = hashlib.sha1()
|
Daniel@0
|
35 with open(transform_substr, 'rb') as afile:
|
Daniel@0
|
36 buf = afile.read(BLOCKSIZE)
|
Daniel@0
|
37 while len(buf) > 0:
|
Daniel@0
|
38 hasher.update(buf)
|
Daniel@0
|
39 buf = afile.read(BLOCKSIZE)
|
Daniel@0
|
40 transform_substr = str(hasher.hexdigest())
|
Daniel@0
|
41
|
Daniel@0
|
42 # define valid extensions
|
Daniel@0
|
43 if not ext:
|
Daniel@0
|
44 ext = tuple([".n3",".csv",".mid"])
|
Daniel@0
|
45 else:
|
Daniel@0
|
46 ext = tuple([ext])
|
Daniel@0
|
47
|
Daniel@0
|
48 # ---
|
Daniel@0
|
49 # we traverse the file structure
|
Daniel@0
|
50 # and list files to copy
|
Daniel@0
|
51 # ---
|
Daniel@0
|
52 data = []
|
Daniel@0
|
53 count = 0
|
Daniel@0
|
54 count2 = 0
|
Daniel@0
|
55 for (dirpath, dirnames, filenames) in os.walk(audiopath):
|
Daniel@0
|
56 for file in filenames:
|
Daniel@0
|
57 print '\rChecked %d, gathered %d files' % (count, count2),
|
Daniel@0
|
58 count += 1
|
Daniel@0
|
59
|
Daniel@0
|
60 # we copy all requested files and the transform files as well!
|
Daniel@0
|
61 if (file.endswith(ext) or (transform_substr and (transform_substr in file))) and (transform_substr in dirpath):
|
Daniel@0
|
62 source = os.path.join(dirpath, file).replace('\\','/')
|
Daniel@0
|
63 data.append(source)
|
Daniel@0
|
64 count2 +=1
|
Daniel@0
|
65
|
Daniel@0
|
66 # count jobs
|
Daniel@0
|
67 njobs = len(data)
|
Daniel@0
|
68 print '\nAbout to copy or move %d files' % (njobs)
|
Daniel@0
|
69
|
Daniel@0
|
70
|
Daniel@0
|
71 count = 0
|
Daniel@0
|
72 # copy individual items
|
Daniel@0
|
73 for x in data:
|
Daniel@0
|
74 spath = os.path.split(x)
|
Daniel@0
|
75 folders = spath[0].split('/')
|
Daniel@0
|
76
|
Daniel@0
|
77 # we remove the first folder that contains "_Analysis" from the path
|
Daniel@0
|
78 max_depth = -3
|
Daniel@0
|
79 skip = -2
|
Daniel@0
|
80 for i in range(1, -max_depth+1):
|
Daniel@0
|
81 if "_Analysis" in folders[-i]:
|
Daniel@0
|
82 skip = -i
|
Daniel@0
|
83 break
|
Daniel@0
|
84
|
Daniel@0
|
85 u = [folders[j] for j in range(max_depth,skip) + range(skip+1,0)]
|
Daniel@0
|
86 featpath = out_path + '/'.join([folders[j] for j in range(max_depth,skip) + range(skip+1,0)])
|
Daniel@0
|
87
|
Daniel@0
|
88 # create the target folder
|
Daniel@0
|
89 try:
|
Daniel@0
|
90 os.makedirs(featpath)
|
Daniel@0
|
91 except OSError as exception:
|
Daniel@0
|
92 if exception.errno!= errno.EEXIST:
|
Daniel@0
|
93 raise
|
Daniel@0
|
94
|
Daniel@0
|
95 # copy stuff
|
Daniel@0
|
96 try:
|
Daniel@0
|
97 dest = featpath + '/' + spath[1]
|
Daniel@0
|
98 if move == 1:
|
Daniel@0
|
99 #print '\rMoving %s to %s' % (x,dest)
|
Daniel@0
|
100 shutil.move(x, dest )
|
Daniel@0
|
101 else:
|
Daniel@0
|
102 #print '\rCopying %s to %s' % (x,dest)
|
Daniel@0
|
103 shutil.copy(x, dest )
|
Daniel@0
|
104 count = count + 1
|
Daniel@0
|
105 except:
|
Daniel@0
|
106 continue
|
Daniel@0
|
107 # progress indicator
|
Daniel@0
|
108 print '\r%3.2f percent. %d done, %d pending' % (count/(njobs*1.0) * 100.0,count, njobs-count),
|
Daniel@0
|
109
|
Daniel@0
|
110 print '\rCopied %d of %d files.' % (count,njobs)
|
Daniel@0
|
111 print '\n'
|
Daniel@0
|
112
|
Daniel@0
|
113
|
Daniel@0
|
114
|
Daniel@0
|
115 if __name__ == "__main__":
|
Daniel@0
|
116 if len(sys.argv) >= 5:
|
Daniel@0
|
117 main(sys.argv[1],sys.argv[2], sys.argv[3],sys.argv[4], int(sys.argv[5]))
|
Daniel@0
|
118 else:
|
Daniel@0
|
119 main(audiopath = 'D:/_Audio/',out_path = 'D:/_Audio_Analysis/', transform_substr = "", ext = "")
|
Daniel@0
|
120 |