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