annotate qiime/qiime_wrapper.py @ 1:2c1d19ebac20 draft default tip

Deleted selected files
author azuzolo
date Wed, 06 Jun 2012 16:41:00 -0400
parents 003162f90751
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
003162f90751 Uploaded
azuzolo
parents:
diff changeset
1 #!/usr/bin/env python
003162f90751 Uploaded
azuzolo
parents:
diff changeset
2 import logging, os, string, sys, tempfile, glob, shutil, types, urllib, optparse, re
003162f90751 Uploaded
azuzolo
parents:
diff changeset
3 import shlex, subprocess
003162f90751 Uploaded
azuzolo
parents:
diff changeset
4
003162f90751 Uploaded
azuzolo
parents:
diff changeset
5 """
003162f90751 Uploaded
azuzolo
parents:
diff changeset
6 sys.argv
003162f90751 Uploaded
azuzolo
parents:
diff changeset
7 this --galaxy_datasets= --quime_script
003162f90751 Uploaded
azuzolo
parents:
diff changeset
8
003162f90751 Uploaded
azuzolo
parents:
diff changeset
9 alpha_rarefaction
003162f90751 Uploaded
azuzolo
parents:
diff changeset
10 output html
003162f90751 Uploaded
azuzolo
parents:
diff changeset
11 wf_arare/alpha_rarefaction_plots/rarefaction_plots.html
003162f90751 Uploaded
azuzolo
parents:
diff changeset
12 wf_arare/alpha_rarefaction_plots/html_plots/
003162f90751 Uploaded
azuzolo
parents:
diff changeset
13 wf_arare/alpha_div
003162f90751 Uploaded
azuzolo
parents:
diff changeset
14 wf_arare/alpha_div/alpha_rarefaction_101_0.txt
003162f90751 Uploaded
azuzolo
parents:
diff changeset
15
003162f90751 Uploaded
azuzolo
parents:
diff changeset
16 --galaxy_summary_html=$output_html
003162f90751 Uploaded
azuzolo
parents:
diff changeset
17 --galaxy_summary_template=$output_template
003162f90751 Uploaded
azuzolo
parents:
diff changeset
18 --galaxy_summary_links='label:link,label:link'
003162f90751 Uploaded
azuzolo
parents:
diff changeset
19 --galaxy_outputdir=$output_html.extra_files_path
003162f90751 Uploaded
azuzolo
parents:
diff changeset
20
003162f90751 Uploaded
azuzolo
parents:
diff changeset
21
003162f90751 Uploaded
azuzolo
parents:
diff changeset
22 """
003162f90751 Uploaded
azuzolo
parents:
diff changeset
23
003162f90751 Uploaded
azuzolo
parents:
diff changeset
24 def stop_err( msg ):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
25 sys.stderr.write( "%s\n" % msg )
003162f90751 Uploaded
azuzolo
parents:
diff changeset
26 sys.exit()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
27
003162f90751 Uploaded
azuzolo
parents:
diff changeset
28 def __main__():
003162f90751 Uploaded
azuzolo
parents:
diff changeset
29 debug = False
003162f90751 Uploaded
azuzolo
parents:
diff changeset
30 tmp_dir = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
31 inputdir = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
32 outputdir = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
33 dataset_patterns = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
34 datasetid = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
35 new_dataset_patterns = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
36 new_files_path = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
37 summary_html=None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
38 summary_template=None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
39 summary_links=None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
40 ## adds "log file" printing capabilities for primary output in dynamic file output
003162f90751 Uploaded
azuzolo
parents:
diff changeset
41 logfile = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
42 ## added support for correcting file extensions
003162f90751 Uploaded
azuzolo
parents:
diff changeset
43 newext = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
44 extchange = None
003162f90751 Uploaded
azuzolo
parents:
diff changeset
45 ## check if there are files to generate
003162f90751 Uploaded
azuzolo
parents:
diff changeset
46 cmd_args = []
003162f90751 Uploaded
azuzolo
parents:
diff changeset
47 for arg in sys.argv[1:]:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
48 if arg.startswith('--galaxy_'):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
49 (opt,val) = arg.split('=') if arg.find('=') > 0 else (arg,None)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
50 '''
003162f90751 Uploaded
azuzolo
parents:
diff changeset
51 if opt == '--galaxy_tmpdir':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
52 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
53 if not os.path.exists(val):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
54 os.makedirs(val)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
55 tmp_dir = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
56 except Exception, ex:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
57 stop_err(ex)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
58 '''
003162f90751 Uploaded
azuzolo
parents:
diff changeset
59 if opt == '--galaxy_outputdir':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
60 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
61 if not os.path.exists(val):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
62 os.makedirs(val)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
63 outputdir = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
64 except Exception, ex:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
65 stop_err(ex)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
66 if opt == '--galaxy_datasets':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
67 dataset_patterns = val.split(',')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
68 if opt == '--galaxy_datasetid':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
69 datasetid = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
70 if opt == '--galaxy_new_datasets':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
71 new_dataset_patterns = val.split(',')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
72 if opt == '--galaxy_new_files_path':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
73 if not os.path.exists(val):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
74 os.makedirs(val)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
75 new_files_path = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
76 if opt == '--galaxy_summary_html':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
77 summary_html=val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
78 if opt == '--galaxy_summary_template':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
79 summary_template=val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
80 if opt == '--galaxy_summary_links':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
81 summary_links=val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
82 if opt == '--galaxy_debug':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
83 debug = True
003162f90751 Uploaded
azuzolo
parents:
diff changeset
84 if opt == '--galaxy_logfile':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
85 logfile = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
86 if opt == '--galaxy_ext_change':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
87 extchange = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
88 if opt == '--galaxy_new_ext':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
89 newext = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
90 if opt == '--galaxy_inputdir':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
91 inputdir = val
003162f90751 Uploaded
azuzolo
parents:
diff changeset
92 else:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
93 cmd_args.append(arg)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
94 if debug: print >> sys.stdout, '\n : '.join(cmd_args)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
95 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
96 stderr = ''
003162f90751 Uploaded
azuzolo
parents:
diff changeset
97 # allow for changing of file extension for files which require it
003162f90751 Uploaded
azuzolo
parents:
diff changeset
98 if extchange != None and inputdir != None and newext != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
99 #newfile = os.path.join(inputdir,"temporary."+newext)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
100 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
101 os.link(extchange,inputdir+"/temporary."+newext)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
102 except:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
103 shutil.copy2(extchange,inputdir+"/temporary."+newext)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
104 cmdline = ' '.join(cmd_args)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
105 if debug: print >> sys.stdout, cmdline
003162f90751 Uploaded
azuzolo
parents:
diff changeset
106 '''
003162f90751 Uploaded
azuzolo
parents:
diff changeset
107 if tmp_dir == None or not os.path.isdir(tmp_dir):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
108 tmp_dir = tempfile.mkdtemp()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
109 if outputdir == None or not os.path.isdir(outputdir):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
110 outputdir = tmp_dir
003162f90751 Uploaded
azuzolo
parents:
diff changeset
111 '''
003162f90751 Uploaded
azuzolo
parents:
diff changeset
112 tmp_stderr_name = tempfile.NamedTemporaryFile( dir=tmp_dir,suffix='.err' ).name
003162f90751 Uploaded
azuzolo
parents:
diff changeset
113 tmp_stderr = open( tmp_stderr_name, 'wb' )
003162f90751 Uploaded
azuzolo
parents:
diff changeset
114 tmp_stdout_name = tempfile.NamedTemporaryFile( dir=tmp_dir,suffix='.out' ).name
003162f90751 Uploaded
azuzolo
parents:
diff changeset
115 tmp_stdout = open( tmp_stdout_name, 'wb' )
003162f90751 Uploaded
azuzolo
parents:
diff changeset
116 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno() )
003162f90751 Uploaded
azuzolo
parents:
diff changeset
117 returncode = proc.wait()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
118 tmp_stderr.close()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
119 # get stderr, allowing for case where it's very large
003162f90751 Uploaded
azuzolo
parents:
diff changeset
120 tmp_stderr = open( tmp_stderr_name, 'rb' )
003162f90751 Uploaded
azuzolo
parents:
diff changeset
121 buffsize = 1048576
003162f90751 Uploaded
azuzolo
parents:
diff changeset
122 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
123 while True:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
124 stderr += tmp_stderr.read( buffsize )
003162f90751 Uploaded
azuzolo
parents:
diff changeset
125 if not stderr or len( stderr ) % buffsize != 0:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
126 break
003162f90751 Uploaded
azuzolo
parents:
diff changeset
127 if debug: print >> sys.stderr, stderr
003162f90751 Uploaded
azuzolo
parents:
diff changeset
128 except OverflowError:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
129 pass
003162f90751 Uploaded
azuzolo
parents:
diff changeset
130 tmp_stderr.close()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
131 if returncode != 0:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
132 if debug: print >> sys.stderr, "returncode = %d" % returncode
003162f90751 Uploaded
azuzolo
parents:
diff changeset
133 raise Exception, stderr
003162f90751 Uploaded
azuzolo
parents:
diff changeset
134 #raise Exception, sys.stderr
003162f90751 Uploaded
azuzolo
parents:
diff changeset
135 # collect results
003162f90751 Uploaded
azuzolo
parents:
diff changeset
136 if dataset_patterns != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
137 for root, dirs, files in os.walk(outputdir):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
138 for fname in files:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
139 fpath = os.path.join(root,fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
140 if dataset_patterns != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
141 for output in dataset_patterns:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
142 (pattern,path) = output.split(':')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
143 if debug: print >> sys.stdout, '%s -> %s' % (pattern,path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
144 if path == None or path == 'None':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
145 continue
003162f90751 Uploaded
azuzolo
parents:
diff changeset
146 if debug: print >> sys.stdout, 'outdir %s match: %s' % (fname,re.match(pattern,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
147 if re.match(pattern,fname):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
148 found = True
003162f90751 Uploaded
azuzolo
parents:
diff changeset
149 # flist.remove(fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
150 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
151 shutil.copy2(fpath, path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
152 if new_files_path != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
153 os.link(fpath, os.path.join(new_files_path,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
154 except Exception, ex:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
155 stop_err('%s' % ex)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
156 # move result to outdir
003162f90751 Uploaded
azuzolo
parents:
diff changeset
157 # Need to flatten the dir hierachy in order for galaxy to serve the href links
003162f90751 Uploaded
azuzolo
parents:
diff changeset
158 if summary_html != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
159 """
003162f90751 Uploaded
azuzolo
parents:
diff changeset
160 for root, dirs, files in os.walk(outputdir):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
161 if root != outputdir:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
162 for fname in files:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
163 fpath = os.path.join(root,fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
164 """
003162f90751 Uploaded
azuzolo
parents:
diff changeset
165 ## move everything up one level
003162f90751 Uploaded
azuzolo
parents:
diff changeset
166 dlist = os.listdir(outputdir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
167 for dname in dlist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
168 dpath = os.path.join(outputdir,dname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
169 if os.path.isdir(dpath):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
170 flist = os.listdir(dpath)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
171 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
172 fpath = os.path.join(dpath,fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
173 shutil.move(fpath,outputdir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
174 if summary_template != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
175 shutil.copy(summary_template,summary_html)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
176 """
003162f90751 Uploaded
azuzolo
parents:
diff changeset
177 flist = os.listdir(outputdir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
178 if debug: print >> sys.stdout, 'outputdir: %s' % outputdir
003162f90751 Uploaded
azuzolo
parents:
diff changeset
179 if debug: print >> sys.stdout, 'files: %s' % ','.join(flist)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
180 if dataset_patterns != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
181 for output in dataset_patterns:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
182 (pattern,path) = output.split(':')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
183 if debug: print >> sys.stdout, '%s -> %s' % (pattern,path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
184 if path == None or path == 'None':
003162f90751 Uploaded
azuzolo
parents:
diff changeset
185 continue
003162f90751 Uploaded
azuzolo
parents:
diff changeset
186 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
187 if debug: print >> sys.stdout, 'outdir %s match: %s' % (fname,re.match(pattern,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
188 if re.match(pattern,fname):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
189 found = True
003162f90751 Uploaded
azuzolo
parents:
diff changeset
190 flist.remove(fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
191 fpath = os.path.join(outputdir,fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
192 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
193 shutil.copy2(fpath, path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
194 except Exception, ex:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
195 stop_err('%s' % ex)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
196 """
003162f90751 Uploaded
azuzolo
parents:
diff changeset
197 # Handle the dynamically generated galaxy datasets
003162f90751 Uploaded
azuzolo
parents:
diff changeset
198 # http://bitbucket.org/galaxy/galaxy-central/wiki/ToolsMultipleOutput
003162f90751 Uploaded
azuzolo
parents:
diff changeset
199 # --new_datasets = specifies files to be found in the new_file_path
003162f90751 Uploaded
azuzolo
parents:
diff changeset
200 # The list items are separated by commas
003162f90751 Uploaded
azuzolo
parents:
diff changeset
201 # Each item conatins: a regex pattern for matching filenames and a galaxy datatype (separated by :)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
202 # The regex match.groups()[0] is used as the id name of the dataset, and must result in unique name for each output
003162f90751 Uploaded
azuzolo
parents:
diff changeset
203 # The --galaxy_output flag is used for instances where data needs to be copied to the extra_files_path for later
003162f90751 Uploaded
azuzolo
parents:
diff changeset
204 # directory use
003162f90751 Uploaded
azuzolo
parents:
diff changeset
205 if new_dataset_patterns != None and new_files_path != None and datasetid != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
206 for output in new_dataset_patterns:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
207 if ':' in output: pattern,ext = output.split(':',1)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
208 flist = os.listdir(new_files_path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
209 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
210 m = re.match(pattern,fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
211 if m:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
212 fpath = os.path.join(new_files_path,fname)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
213 if len(m.groups()) > 0:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
214 root = m.groups()[0]
003162f90751 Uploaded
azuzolo
parents:
diff changeset
215 else:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
216 # remove the ext from the name if it exists, galaxy will add back later
003162f90751 Uploaded
azuzolo
parents:
diff changeset
217 # remove underscores since galaxy uses that as a field separator for dynamic datasets
003162f90751 Uploaded
azuzolo
parents:
diff changeset
218 root = re.sub('\.?'+ext+'$','',fname).replace('_','').replace('.','')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
219 # filename pattern required by galaxy
003162f90751 Uploaded
azuzolo
parents:
diff changeset
220 fn = "%s_%s_%s_%s_%s" % ( 'primary', datasetid, root, 'visible', ext )
003162f90751 Uploaded
azuzolo
parents:
diff changeset
221 if debug: print >> sys.stdout, '> %s' % fpath
003162f90751 Uploaded
azuzolo
parents:
diff changeset
222 if debug: print >> sys.stdout, '< %s' % os.path.join(new_files_path,fn)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
223 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
224 os.link(fpath, os.path.join(new_files_path,fn))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
225 # needed for files with variable output and a directory structure
003162f90751 Uploaded
azuzolo
parents:
diff changeset
226 if outputdir != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
227 os.link(fpath, os.path.join(outputdir,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
228 # clean out files from tmp directory, may be unnecessary
003162f90751 Uploaded
azuzolo
parents:
diff changeset
229 #os.remove(fpath)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
230 except:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
231 shutil.copy2(fpath, os.path.join(new_files_path,fn))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
232 # needed for files with variable output and a directory structure
003162f90751 Uploaded
azuzolo
parents:
diff changeset
233 if outputdir != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
234 os.link(fpath, os.path.join(outputdir,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
235
003162f90751 Uploaded
azuzolo
parents:
diff changeset
236 print "bob" + logfile
003162f90751 Uploaded
azuzolo
parents:
diff changeset
237 '''
003162f90751 Uploaded
azuzolo
parents:
diff changeset
238 if logfile != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
239 print "bleep"
003162f90751 Uploaded
azuzolo
parents:
diff changeset
240 if outputdir != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
241 print "beep"
003162f90751 Uploaded
azuzolo
parents:
diff changeset
242 logwrite = open(logfile, 'w+')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
243 logwrite.write('Tool started. Files created by tool: \n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
244 flist = os.listdir(outputdir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
245 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
246 if 'DS_Store' not in fname:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
247 logwrite.write(fname+'\n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
248 logwrite.write('Tool Finished.')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
249 logwrite.close()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
250 if new_files_path != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
251 print "boop"
003162f90751 Uploaded
azuzolo
parents:
diff changeset
252 logwrite = open(logfile, 'w+')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
253 if len(logfile.readline() > 0):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
254 logwrite.write('Tool started. Files created by tool: \n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
255 flist = os.listdir(new_files_path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
256 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
257 if 'DS_Store' not in fname:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
258 logwrite.write(fname+'\n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
259 logwrite.write('Tool Finished.')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
260 logwrite.close()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
261 '''
003162f90751 Uploaded
azuzolo
parents:
diff changeset
262 except Exception, e:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
263 msg = str(e) + stderr
003162f90751 Uploaded
azuzolo
parents:
diff changeset
264 #msg = str(e) + str(sys.stderr)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
265 #stop_err( 'Error running ' + msg)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
266 finally:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
267 # Only remove temporary directories and files from temporary directory
003162f90751 Uploaded
azuzolo
parents:
diff changeset
268 # Enclose in try block, so we don't report error on stale nfs handles
003162f90751 Uploaded
azuzolo
parents:
diff changeset
269 try:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
270 if logfile != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
271 if outputdir != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
272 logwrite = open(logfile, 'r+')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
273 logwrite.write('Tool started. Files created by tool: \n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
274 flist = os.listdir(outputdir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
275 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
276 if 'DS_Store' not in fname and 'primary' not in fname:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
277 logwrite.write(fname+'\n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
278 logwrite.write('Tool Finished.')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
279 logwrite.close()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
280 if new_files_path != None:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
281 logwrite = open(logfile, 'r+')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
282 logwrite.write('Tool started. Files created by tool: \n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
283 flist = os.listdir(new_files_path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
284 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
285 if 'DS_Store' not in fname and 'primary' not in fname:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
286 logwrite.write(fname+'\n')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
287 logwrite.write('Tool Finished.')
003162f90751 Uploaded
azuzolo
parents:
diff changeset
288 logwrite.close()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
289 if tmp_dir != None and os.path.exists(tmp_dir) and os.path.isfile(tmp_dir):
003162f90751 Uploaded
azuzolo
parents:
diff changeset
290 #shutil.rmtree(tmp_dir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
291 pass
003162f90751 Uploaded
azuzolo
parents:
diff changeset
292 if outputdir != None and 'files' not in outputdir:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
293 flist = os.listdir(outputdir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
294 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
295 if 'DS_Store' not in fname and 'primary' not in fname:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
296 os.remove(os.path.join(outputdir,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
297 if inputdir != None and 'files' not in inputdir:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
298 flist = os.listdir(inputdir)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
299 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
300 if 'DS_Store' not in fname and 'primary' not in fname:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
301 os.remove(os.path.join(inputdir,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
302 if new_files_path != None and 'files' not in new_files_path:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
303 flist = os.listdir(new_files_path)
003162f90751 Uploaded
azuzolo
parents:
diff changeset
304 for fname in flist:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
305 if 'DS_Store' not in fname and 'primary' not in fname:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
306 os.remove(os.path.join(new_files_path,fname))
003162f90751 Uploaded
azuzolo
parents:
diff changeset
307
003162f90751 Uploaded
azuzolo
parents:
diff changeset
308 except:
003162f90751 Uploaded
azuzolo
parents:
diff changeset
309 pass
003162f90751 Uploaded
azuzolo
parents:
diff changeset
310
003162f90751 Uploaded
azuzolo
parents:
diff changeset
311 if __name__ == "__main__": __main__()
003162f90751 Uploaded
azuzolo
parents:
diff changeset
312