Mercurial > repos > brenninc > xdirectory_reader_limited_by_data_table
comparison directory_copier.py @ 0:ccabef3f7d5f draft
Uploaded first version
author | brenninc |
---|---|
date | Sun, 08 May 2016 11:01:03 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ccabef3f7d5f |
---|---|
1 import gzip | |
2 import optparse # using optparse as hydra still python 2.6 | |
3 import os.path | |
4 import shutil | |
5 import sys | |
6 | |
7 def report_error(*args): | |
8 sys.stderr.write(' '.join(map(str,args)) + '\n') | |
9 sys.stderr.flush() | |
10 sys.exit(1) | |
11 | |
12 | |
13 def get_tool_data(name): | |
14 root_dir = os.path.dirname((os.path.realpath(__file__))) | |
15 path = os.path.join(root_dir,"tool-data",name) | |
16 if not(os.path.isfile(path)): | |
17 report_error(name,"file not found in tool's tool-data folder. Please ask you galaxy admin to add it back") | |
18 return path | |
19 | |
20 | |
21 def check_white_list(path_to_check): | |
22 white_list = get_tool_data("white-list.ini") | |
23 with open(white_list, 'r') as white_list_file: | |
24 for line in white_list_file: | |
25 line = line.strip() | |
26 if len(line) >= 1 and path_to_check.startswith(line): | |
27 return True | |
28 report_error(path_to_check,"has not been included in the white list. Please contact the local galaxy admin to add it.") | |
29 | |
30 | |
31 def check_black_list(path_to_check): | |
32 black_list = get_tool_data("black-list.ini") | |
33 with open(black_list, 'r') as black_list_file: | |
34 for line in black_list_file: | |
35 line = line.strip() | |
36 if len(line) >= 1 and line in path_to_check: | |
37 report_error(line,"has been black list so",path_to_check,"is not allowed. Please contact the local galaxy admin to change that, or add a symlink.") | |
38 return True | |
39 | |
40 | |
41 def check_pattern_get_new_name(a_file, ending, options): | |
42 if options.start: | |
43 if not(a_file.startswith(options.start)): | |
44 return None | |
45 if options.last: | |
46 if ending[0] == ".": | |
47 last = options.last + ending | |
48 else: | |
49 if options.last[-1] == ".": | |
50 last = options.last + ending | |
51 else: | |
52 last = options.last + "." + ending | |
53 if not(a_file.endswith(last)): | |
54 return None | |
55 if options.new_ending: | |
56 name = a_file[:-len(ending)] | |
57 if options.new_ending[0] ==".": | |
58 if name[-1] == ".": | |
59 name = name[:-1] | |
60 return name + options.new_ending | |
61 if options.decompress: | |
62 if a_file.endswith(".gz"): | |
63 return a_file[:-3] | |
64 return a_file | |
65 | |
66 | |
67 def check_and_get_new_name(a_file, options): | |
68 for ending in options.endings: | |
69 if a_file.endswith(ending): | |
70 return check_pattern_get_new_name (a_file, ending, options) | |
71 return None | |
72 | |
73 | |
74 def link(a_file, new_name, path): | |
75 file_path = os.path.join(os.path.realpath(path), a_file) | |
76 sym_path = os.path.join(os.path.realpath("output"), new_name) | |
77 #if not(os.path.exists(sym_path)): | |
78 os.link(file_path, sym_path) | |
79 | |
80 | |
81 def decompress(a_file, new_name, path): | |
82 file_path = os.path.join(os.path.realpath(path), a_file) | |
83 target_path = os.path.join(os.path.realpath("output"), new_name) | |
84 with gzip.open(file_path, 'rb') as f_in, open(target_path, 'wb') as f_out: | |
85 shutil.copyfileobj(f_in, f_out) | |
86 | |
87 | |
88 def copy_and_link(path, options): | |
89 os.mkdir("output") | |
90 with open(options.list, 'w') as list_file: | |
91 files = os.listdir(path) | |
92 files.sort() | |
93 for a_file in files: | |
94 new_name = check_and_get_new_name(a_file, options) | |
95 if new_name: | |
96 list_file.write(new_name) | |
97 list_file.write("\n") | |
98 if options.decompress: | |
99 if a_file.endswith(".gz"): | |
100 decompress(a_file, new_name,path) | |
101 else: | |
102 link(a_file, new_name, path) | |
103 elif options.link: | |
104 link(a_file, new_name, path) | |
105 | |
106 | |
107 if __name__ == '__main__': | |
108 parser = optparse.OptionParser() | |
109 parser.add_option("--path", action="store", type="string", | |
110 help="Path of directory to check. ") | |
111 parser.add_option("--ending", action="append", type="string", dest="endings", | |
112 help="Ending that can be listed and if requested linked or decompressed. ") | |
113 parser.add_option("--start", action="store", type="string", | |
114 help="String that must be at the start of the file name ") | |
115 parser.add_option("--last", action="store", type="string", | |
116 help="String that must be the last bit of the file name before the endings") | |
117 parser.add_option("--new_ending", action="store", type="string", | |
118 help="New ending to replace any previous ending in list and if required links or decompressions. Note: If not set decompression will auto remove the compressioned part of the ending") | |
119 #parser.add_option("--regex", action="store", type="string", | |
120 # help="Regex pattern the file name (less . ending) must match before the endings") | |
121 parser.add_option("--list", action="store", type="string", | |
122 help="Path to where all files should be listed. ") | |
123 parser.add_option("--link", action="store_true", default=False, | |
124 help="If set will cause links to be added in output directory. ") | |
125 parser.add_option("--decompress", action="store_true", default=False, | |
126 help="If set will cause gz files to be decompressed or if not a supported decompression ending linked.") | |
127 (options, args) = parser.parse_args() | |
128 | |
129 | |
130 path = options.path.strip() | |
131 if path[-1] != '/': | |
132 path = path + "/" | |
133 check_white_list(path) | |
134 print path, "white listed" | |
135 check_black_list(path) | |
136 print path, "not black listed" | |
137 copy_and_link(path, options) | |
138 |