11"Various utilities used by sourmash tests."
2+
23import sys
34import os
45import tempfile
1314from io import StringIO
1415
1516
16- SIG_FILES = [os .path .join ('demo' , f ) for f in (
17- "SRR2060939_1.sig" , "SRR2060939_2.sig" , "SRR2241509_1.sig" ,
18- "SRR2255622_1.sig" , "SRR453566_1.sig" , "SRR453569_1.sig" , "SRR453570_1.sig" )
17+ SIG_FILES = [
18+ os .path .join ("demo" , f )
19+ for f in (
20+ "SRR2060939_1.sig" ,
21+ "SRR2060939_2.sig" ,
22+ "SRR2241509_1.sig" ,
23+ "SRR2255622_1.sig" ,
24+ "SRR453566_1.sig" ,
25+ "SRR453569_1.sig" ,
26+ "SRR453570_1.sig" ,
27+ )
1928]
2029
30+
2131def get_test_data (filename ):
2232 thisdir = os .path .dirname (__file__ )
2333 return os .path .join (thisdir , "test-data" , filename )
2434
35+
2536def get_example_data (filename ):
2637 thisdir = os .path .dirname (__file__ )
2738 return os .path .join (thisdir , "../examples" , filename )
2839
29- def scriptpath (scriptname = 'sourmash' ):
40+
41+ def scriptpath (scriptname = "sourmash" ):
3042 """Return the path to the scripts, in both dev and install situations."""
3143 # note - it doesn't matter what the scriptname is here, as long as
3244 # it's some script present in this version of sourmash.
@@ -39,15 +51,15 @@ def scriptpath(scriptname='sourmash'):
3951 if os .path .exists (os .path .join (path , scriptname )):
4052 return path
4153
42- for path in os .environ [' PATH' ].split (':' ):
54+ for path in os .environ [" PATH" ].split (":" ):
4355 if os .path .exists (os .path .join (path , scriptname )):
4456 return path
4557
4658
4759def _runscript (scriptname ):
4860 """Find & run a script with exec (i.e. not via os.system or subprocess)."""
4961 namespace = {"__name__" : "__main__" }
50- namespace [' sys' ] = globals ()[' sys' ]
62+ namespace [" sys" ] = globals ()[" sys" ]
5163
5264 entry_points = importlib .metadata .entry_points (
5365 group = "console_scripts" , name = "sourmash"
@@ -58,8 +70,8 @@ def _runscript(scriptname):
5870 return 0
5971
6072
61- ScriptResults = collections .namedtuple (' ScriptResults' ,
62- [ 'status' , 'out' , 'err' ])
73+ ScriptResults = collections .namedtuple (" ScriptResults" , [ "status" , "out" , "err" ])
74+
6375
6476def runscript (scriptname , args , ** kwargs ):
6577 """Run a Python script using exec().
@@ -75,17 +87,17 @@ def runscript(scriptname, args, **kwargs):
7587 sysargs .extend (args )
7688
7789 cwd = os .getcwd ()
78- in_directory = kwargs .get (' in_directory' , cwd )
79- fail_ok = kwargs .get (' fail_ok' , False )
90+ in_directory = kwargs .get (" in_directory" , cwd )
91+ fail_ok = kwargs .get (" fail_ok" , False )
8092
8193 try :
8294 status = - 1
8395 oldargs = sys .argv
8496 sys .argv = sysargs
8597
8698 oldin = None
87- if ' stdin_data' in kwargs :
88- oldin , sys .stdin = sys .stdin , StringIO (kwargs [' stdin_data' ])
99+ if " stdin_data" in kwargs :
100+ oldin , sys .stdin = sys .stdin , StringIO (kwargs [" stdin_data" ])
89101
90102 oldout , olderr = sys .stdout , sys .stderr
91103 sys .stdout = StringIO ()
@@ -95,8 +107,8 @@ def runscript(scriptname, args, **kwargs):
95107 os .chdir (in_directory )
96108
97109 try :
98- print (' running:' , scriptname , ' in:' , in_directory , file = oldout )
99- print (' arguments' , sysargs , file = oldout )
110+ print (" running:" , scriptname , " in:" , in_directory , file = oldout )
111+ print (" arguments" , sysargs , file = oldout )
100112
101113 status = _runscript (scriptname )
102114 except SystemExit as err :
@@ -123,9 +135,10 @@ def runscript(scriptname, args, **kwargs):
123135
124136 return ScriptResults (status , out , err )
125137
138+
126139class TempDirectory (object ):
127140 def __init__ (self ):
128- self .tempdir = tempfile .mkdtemp (prefix = ' sourmashtest_' )
141+ self .tempdir = tempfile .mkdtemp (prefix = " sourmashtest_" )
129142
130143 def __enter__ (self ):
131144 return self .tempdir
@@ -143,7 +156,7 @@ def __exit__(self, exc_type, exc_value, traceback):
143156class SourmashCommandFailed (Exception ):
144157 def __init__ (self , msg ):
145158 Exception .__init__ (self , msg )
146- self .message = msg
159+ self .message = msg
147160
148161
149162class RunnerContext (object ):
@@ -156,32 +169,34 @@ class RunnerContext(object):
156169
157170 You can use the 'output' method to build filenames in my temp directory.
158171 """
172+
159173 def __init__ (self , location ):
160174 self .location = location
161175 self .last_command = None
162176 self .last_result = None
163177
164178 def run_sourmash (self , * args , ** kwargs ):
165179 "Run the sourmash script with the given arguments."
166- kwargs [' fail_ok' ] = True
167- if ' in_directory' not in kwargs :
168- kwargs [' in_directory' ] = self .location
180+ kwargs [" fail_ok" ] = True
181+ if " in_directory" not in kwargs :
182+ kwargs [" in_directory" ] = self .location
169183
170- cmdlist = [' sourmash' ]
171- cmdlist .extend (( str (x ) for x in args ))
184+ cmdlist = [" sourmash" ]
185+ cmdlist .extend ((str (x ) for x in args ))
172186 self .last_command = " " .join (cmdlist )
173- self .last_result = runscript (' sourmash' , args , ** kwargs )
187+ self .last_result = runscript (" sourmash" , args , ** kwargs )
174188
175189 if self .last_result .status :
176190 raise SourmashCommandFailed (self .last_result .err )
177191
178192 return self .last_result
193+
179194 sourmash = run_sourmash
180195
181196 def run (self , scriptname , * args , ** kwargs ):
182197 "Run a script with the given arguments."
183- if ' in_directory' not in kwargs :
184- kwargs [' in_directory' ] = self .location
198+ if " in_directory" not in kwargs :
199+ kwargs [" in_directory" ] = self .location
185200 self .last_command = " " .join (args )
186201 self .last_result = runscript (scriptname , args , ** kwargs )
187202 return self .last_result
@@ -199,10 +214,10 @@ def __str__(self):
199214 if self .last_result .out :
200215 s += "- stdout:\n ---\n {}---\n " .format (self .last_result .out )
201216 else :
202- s += ' (no stdout)\n \n '
217+ s += " (no stdout)\n \n "
203218 if self .last_result .err :
204219 s += "- stderr:\n ---\n {}---\n " .format (self .last_result .err )
205220 else :
206- s += ' (no stderr)\n '
221+ s += " (no stderr)\n "
207222
208223 return s
0 commit comments