annotate imagej2_base_utils.py @ 3:bb65623b90a8 draft

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