Mercurial > repos > iuc > imagej2_make_binary
annotate imagej2_base_utils.py @ 2:c026d0eddc3e draft
planemo upload commit 378c3ecfbaed0330faf197811d379431b0f9e92c-dirty
| author | iuc |
|---|---|
| date | Sun, 11 Oct 2015 13:30:58 -0400 |
| parents | d26f11339f08 |
| children | bb7bfa5bc1b0 |
| rev | line source |
|---|---|
|
0
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
1 import os |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
2 import shutil |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
3 import sys |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
4 import tempfile |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
5 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
6 FIJI_JAR_DIR = os.environ.get( 'FIJI_JAR_DIR', None ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
7 FIJI_OSX_JAVA3D_DIR = os.environ.get( 'FIJI_OSX_JAVA3D_DIR', None ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
8 FIJI_PLUGIN_DIR = os.environ.get( 'FIJI_PLUGIN_DIR', None ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
9 FIJI_ROOT_DIR = os.environ.get( 'FIJI_ROOT_DIR', None ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
10 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
11 BUFF_SIZE = 1048576 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
12 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
13 def cleanup_before_exit( tmp_dir ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
14 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
15 Remove temporary files and directories prior to tool exit. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
16 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
17 if tmp_dir and os.path.exists( tmp_dir ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
18 shutil.rmtree( tmp_dir ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
19 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
20 def get_base_cmd_bunwarpj( jvm_memory ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
21 if FIJI_JAR_DIR is not None and FIJI_PLUGIN_DIR is not None: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
22 if jvm_memory in [ None, 'None' ]: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
23 jvm_memory_str = '' |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
24 else: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
25 jvm_memory_str = '-Xmx%s' % jvm_memory |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
26 bunwarpj_base_cmd = "java %s -cp %s/ij-1.49k.jar:%s/bUnwarpJ_-2.6.1.jar bunwarpj.bUnwarpJ_" % \ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
27 ( jvm_memory_str, FIJI_JAR_DIR, FIJI_PLUGIN_DIR ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
28 return bunwarpj_base_cmd |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
29 return None |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
30 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
31 def get_base_command_imagej2( memory_size=None, macro=None, jython_script=None ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
32 imagej2_executable = get_imagej2_executable() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
33 if imagej2_executable is None: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
34 return None |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
35 cmd = '%s --ij2 --headless --debug' % imagej2_executable |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
36 if memory_size is not None: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
37 memory_size_cmd = ' -DXms=%s -DXmx=%s' % ( memory_size, memory_size ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
38 cmd += memory_size_cmd |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
39 if macro is not None: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
40 cmd += ' --macro %s' % os.path.abspath( macro ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
41 if jython_script is not None: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
42 cmd += ' --jython %s' % os.path.abspath( jython_script ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
43 return cmd |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
44 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
45 def get_file_extension( image_format ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
46 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
47 Return a valid bioformats file extension based on the received |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
48 value of image_format( e.g., "gif" is returned as ".gif". |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
49 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
50 return '.%s' % image_format |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
51 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
52 def get_file_name_without_extension( file_path ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
53 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
54 Eliminate the .ext from the received file name, assuming that |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
55 the file name consists of only a single '.'. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
56 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
57 if os.path.exists( file_path ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
58 path, name = os.path.split( file_path ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
59 name_items = name.split( '.' ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
60 return name_items[ 0 ] |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
61 return None |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
62 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
63 def get_imagej2_executable(): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
64 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
65 Fiji names the ImageJ executable different names for different |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
66 architectures, so figure out which name we need. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
67 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
68 platform_dict = get_platform_info_dict() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
69 if platform_dict.get( 'architecture', None ) in [ 'x86_64' ]: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
70 if platform_dict.get( 'os', None ) in [ 'darwin' ]: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
71 return 'ImageJ-macosx' |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
72 if platform_dict.get( 'os', None ) in [ 'linux' ]: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
73 return 'ImageJ-linux64' |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
74 return None |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
75 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
76 def get_input_image_path( tmp_dir, input_file, image_format ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
77 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
78 Bioformats uses file extensions (e.g., .job, .gif, etc) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
79 when reading and writing image files, so the Galaxy dataset |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
80 naming convention of setting all file extensions as .dat |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
81 must be handled. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
82 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
83 image_path = get_temporary_image_path( tmp_dir, image_format ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
84 # Remove the file so we can create a symlink. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
85 os.remove( image_path ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
86 os.symlink( input_file, image_path ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
87 return image_path |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
88 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
89 def get_platform_info_dict(): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
90 '''Return a dict with information about the current platform.''' |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
91 platform_dict = {} |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
92 sysname, nodename, release, version, machine = os.uname() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
93 platform_dict[ 'os' ] = sysname.lower() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
94 platform_dict[ 'architecture' ] = machine.lower() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
95 return platform_dict |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
96 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
97 def get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout, include_stdout=False ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
98 tmp_stderr.close() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
99 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
100 Return a stderr string of reasonable size. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
101 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
102 # Get stderr, allowing for case where it's very large. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
103 tmp_stderr = open( tmp_err, 'rb' ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
104 stderr_str = '' |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
105 buffsize = BUFF_SIZE |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
106 try: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
107 while True: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
108 stderr_str += tmp_stderr.read( buffsize ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
109 if not stderr_str or len( stderr_str ) % buffsize != 0: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
110 break |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
111 except OverflowError: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
112 pass |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
113 tmp_stderr.close() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
114 if include_stdout: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
115 tmp_stdout = open( tmp_out, 'rb' ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
116 stdout_str = '' |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
117 buffsize = BUFF_SIZE |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
118 try: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
119 while True: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
120 stdout_str += tmp_stdout.read( buffsize ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
121 if not stdout_str or len( stdout_str ) % buffsize != 0: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
122 break |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
123 except OverflowError: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
124 pass |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
125 tmp_stdout.close() |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
126 if include_stdout: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
127 return 'STDOUT\n%s\n\nSTDERR\n%s\n' % ( stdout_str, stderr_str ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
128 return stderr_str |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
129 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
130 def get_temp_dir( prefix='tmp-imagej-', dir=None ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
131 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
132 Return a temporary directory. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
133 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
134 return tempfile.mkdtemp( prefix=prefix, dir=dir ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
135 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
136 def get_tempfilename( dir=None, suffix=None ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
137 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
138 Return a temporary file name. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
139 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
140 fd, name = tempfile.mkstemp( suffix=suffix, dir=dir ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
141 os.close( fd ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
142 return name |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
143 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
144 def get_temporary_image_path( tmp_dir, image_format ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
145 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
146 Return the path to a temporary file with a valid image format |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
147 file extension that can be used with bioformats. |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
148 """ |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
149 file_extension = get_file_extension( image_format ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
150 return get_tempfilename( tmp_dir, file_extension ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
151 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
152 def handle_none_type( val, val_type='float' ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
153 if val is None: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
154 return ' None' |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
155 else: |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
156 if val_type == 'float': |
|
2
c026d0eddc3e
planemo upload commit 378c3ecfbaed0330faf197811d379431b0f9e92c-dirty
iuc
parents:
1
diff
changeset
|
157 return ' %.3f' % val |
|
0
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
158 elif val_type == 'int': |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
159 return ' %d' % val |
|
1
d26f11339f08
planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
0
diff
changeset
|
160 return ' %s' % val |
|
0
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
161 |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
162 def stop_err( msg ): |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
163 sys.stderr.write( msg ) |
|
66d9e595dff2
planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents:
diff
changeset
|
164 sys.exit( 1 ) |
