2323import asyncio
2424import os
2525import time
26+ from typing import Any
2627
2728import asyncssh
2829
2930
30- def _cleanup_result (result ) :
31+ def _cleanup_result (result : dict [ str , list [ str ]]) -> dict [ str , list [ str ]] :
3132 """Remove empty list from result.
3233
3334 >>> _cleanup_result({ '0': [], '1': []})
@@ -49,7 +50,9 @@ def _cleanup_result(result):
4950 return result
5051
5152
52- def _extend_result (result , new_result ):
53+ def _extend_result (
54+ result : dict [str , list [str ]], new_result : dict [str , list [str ]]
55+ ) -> dict [str , list [str ]]:
5356 """Extend result dictionnary values with new result
5457 dictionnary values
5558
@@ -100,7 +103,7 @@ def _extend_result(result, new_result):
100103 return result
101104
102105
103- def _check_all_nodes_processed (result ) :
106+ def _check_all_nodes_processed (result : dict [ str , list [ str ]]) -> bool :
104107 """Verify all nodes are successful or failed.
105108
106109 >>> _check_all_nodes_processed({ 'saclay': [], 'grenoble': []})
@@ -126,12 +129,14 @@ def _check_all_nodes_processed(result):
126129class OpenLinuxSsh :
127130 """Implement SSH API using asyncssh."""
128131
129- def __init__ (self , config_ssh , groups , verbose = False ):
132+ def __init__ (
133+ self , config_ssh : dict [str , Any ], groups : dict [str , list [str ]], verbose : bool = False
134+ ) -> None :
130135 self .config_ssh = config_ssh
131136 self .groups = groups
132137 self .verbose = verbose
133138
134- def run (self , command , with_proxy = True , ** kwargs ) :
139+ def run (self , command : str , with_proxy : bool = True , ** kwargs : Any ) -> dict [ str , list [ str ]] :
135140 """Run ssh command on nodes, optionally through a proxy."""
136141 result = {"0" : [], "1" : []}
137142 for site , hosts in self .groups .items ():
@@ -143,7 +148,7 @@ def run(self, command, with_proxy=True, **kwargs):
143148 result = _extend_result (result , result_cmd )
144149 return _cleanup_result (result )
145150
146- def scp (self , src , dst ) :
151+ def scp (self , src : str , dst : str ) -> dict [ str , list [ str ]] :
147152 """Copy file to SSH frontend via SFTP."""
148153 result = {"0" : [], "1" : []}
149154 for site in self .groups :
@@ -154,7 +159,7 @@ def scp(self, src, dst):
154159 result ["1" ].append (site )
155160 return _cleanup_result (result )
156161
157- def wait (self , max_wait ) :
162+ def wait (self , max_wait : int ) -> dict [ str , list [ str ]] :
158163 """Wait for requested Linux nodes until they boot."""
159164 result = {"0" : [], "1" : []}
160165 start_time = time .time ()
@@ -167,13 +172,20 @@ def wait(self, max_wait):
167172 result = _extend_result (result , result_cmd )
168173 return _cleanup_result (result )
169174
170- def _connect_kwargs (self , timeout = 10 ):
175+ def _connect_kwargs (self , timeout : int = 10 ) -> dict [ str , Any ] :
171176 kwargs = {"known_hosts" : None , "connect_timeout" : timeout }
172177 if SSH_KEY :
173178 kwargs ["client_keys" ] = [os .path .expanduser (SSH_KEY )]
174179 return kwargs
175180
176- async def _run_command (self , command , hosts , proxy_host = None , timeout = 10 , ** kwargs ):
181+ async def _run_command (
182+ self ,
183+ command : str ,
184+ hosts : list [str ],
185+ proxy_host : str | None = None ,
186+ timeout : int = 10 ,
187+ ** kwargs : Any ,
188+ ) -> dict [str , list [str ]]:
177189 tasks = [
178190 self ._run_on_host (host , command , proxy_host = proxy_host , timeout = timeout , ** kwargs )
179191 for host in hosts
@@ -190,7 +202,14 @@ async def _run_command(self, command, hosts, proxy_host=None, timeout=10, **kwar
190202 result ["0" ].append (host )
191203 return result
192204
193- async def _run_on_host (self , host , command , proxy_host = None , timeout = 10 , ** kwargs ):
205+ async def _run_on_host (
206+ self ,
207+ host : str ,
208+ command : str ,
209+ proxy_host : str | None = None ,
210+ timeout : int = 10 ,
211+ ** kwargs : Any ,
212+ ) -> tuple [int , str ]:
194213 ck = self ._connect_kwargs (timeout )
195214 if proxy_host :
196215 async with asyncssh .connect (
@@ -204,7 +223,7 @@ async def _run_on_host(self, host, command, proxy_host=None, timeout=10, **kwarg
204223 result = await conn .run (command , ** kwargs )
205224 return result .exit_status , result .stdout or ""
206225
207- async def _copy_file (self , site , src , dst ) :
226+ async def _copy_file (self , site : str , src : str , dst : str ) -> None :
208227 ck = self ._connect_kwargs ()
209228 async with asyncssh .connect (site , username = self .config_ssh ["user" ], ** ck ) as conn :
210229 async with conn .start_sftp_client () as sftp :
0 commit comments