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