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_folderwise.py "D:/_Audio/" "D:/Chord_Analysis/" "1a812" 0
|
Daniel@0
|
20
|
Daniel@0
|
21 def main(audiopath, out_path, transform_substr = "", 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 folders? (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 # ---
|
Daniel@0
|
43 # we traverse the file structure
|
Daniel@0
|
44 # and list files to copy
|
Daniel@0
|
45 # ---
|
Daniel@0
|
46 data = []
|
Daniel@0
|
47 count = 0
|
Daniel@0
|
48 count2 = 0
|
Daniel@0
|
49 for (dirpath, dirnames, filenames) in os.walk(audiopath):
|
Daniel@0
|
50 for dir in dirnames:
|
Daniel@0
|
51 print '\rChecked %d, gathered %d folders' % (count, count2),
|
Daniel@0
|
52 count += 1
|
Daniel@0
|
53
|
Daniel@0
|
54 # we copy all requested files and the transform files as well!
|
Daniel@0
|
55 if (transform_substr and (transform_substr in dir)):
|
Daniel@0
|
56 source = os.path.join(dirpath, dir).replace('\\','/')
|
Daniel@0
|
57 data.append(source)
|
Daniel@0
|
58 count2 +=1
|
Daniel@0
|
59
|
Daniel@0
|
60 # count jobs
|
Daniel@0
|
61 njobs = len(data)
|
Daniel@0
|
62 print '\nAbout to copy or move %d directories' % (njobs)
|
Daniel@0
|
63
|
Daniel@0
|
64
|
Daniel@0
|
65 count = 0
|
Daniel@0
|
66 # copy individual items
|
Daniel@0
|
67 for x in data:
|
Daniel@0
|
68
|
Daniel@0
|
69 spath = os.path.split(x)
|
Daniel@0
|
70 folders = spath[0].split('/')
|
Daniel@0
|
71
|
Daniel@0
|
72 # if exists, we remove the first folder
|
Daniel@0
|
73 # which contains "_Analysis" from the path
|
Daniel@0
|
74 # maxdepth contains the maximum depth of folders structure to keep,
|
Daniel@0
|
75 # counted from the most specific folder level
|
Daniel@0
|
76 max_depth = -2
|
Daniel@0
|
77 skip = -2
|
Daniel@0
|
78 for i in range(1, -max_depth+1):
|
Daniel@0
|
79 if "_Analysis" in folders[-i]:
|
Daniel@0
|
80 skip = -i
|
Daniel@0
|
81 break
|
Daniel@0
|
82
|
Daniel@0
|
83 folderbase = [folders[j] for j in range(max_depth,skip) + range(skip+1,0)]
|
Daniel@0
|
84 featpath = out_path + '/'.join(folderbase)
|
Daniel@0
|
85
|
Daniel@0
|
86 # create the target folder
|
Daniel@0
|
87 try:
|
Daniel@0
|
88 os.makedirs(featpath)
|
Daniel@0
|
89 except OSError as exception:
|
Daniel@0
|
90 if exception.errno!= errno.EEXIST:
|
Daniel@0
|
91 raise
|
Daniel@0
|
92
|
Daniel@0
|
93 # copy stuff
|
Daniel@0
|
94
|
Daniel@0
|
95 dest = featpath + '/' + spath[1]
|
Daniel@0
|
96 if move == 1:
|
Daniel@0
|
97 #print '\rMoving %s to %s' % (x,dest)
|
Daniel@0
|
98 shutil.move(x, dest )
|
Daniel@0
|
99
|
Daniel@0
|
100 if move == 0:
|
Daniel@0
|
101 #print '\rCopying %s to %s' % (x,dest)
|
Daniel@0
|
102 shutil.copytree(x, dest )
|
Daniel@0
|
103
|
Daniel@0
|
104 count = count + 1
|
Daniel@0
|
105
|
Daniel@0
|
106 # progress indicator
|
Daniel@0
|
107 print '\r%3.2f percent. %d done, %d pending' % (count/(njobs*1.0) * 100.0,count, njobs-count),
|
Daniel@0
|
108
|
Daniel@0
|
109 print '\rCopied %d of %d folders.' % (count,njobs)
|
Daniel@0
|
110 print '\n'
|
Daniel@0
|
111
|
Daniel@0
|
112
|
Daniel@0
|
113 if __name__ == "__main__":
|
Daniel@0
|
114 if len(sys.argv) >= 5:
|
Daniel@0
|
115 main(sys.argv[1],sys.argv[2], sys.argv[3], int(sys.argv[4]))
|
Daniel@0
|
116 else:
|
Daniel@0
|
117 main(audiopath = 'D:/_Audio/',out_path = 'D:/_Audio_Analysis/', transform_substr = "", ext = "")
|
Daniel@0
|
118
|