Skip to content

Commit 796b72e

Browse files
committed
Jail: type-hint execs
in the process i also found a few instances where we were mutating input parameters — unnecessarily so. n.b.: libiocage.lib.Foo is the module, libiocage.lib.Foo.Foo is the actual class that we want
1 parent 4344d2b commit 796b72e

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

libiocage/lib/Jail.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class Jail:
6363
"""
6464

6565
def __init__(self,
66-
data: Union[str, dict]={},
67-
zfs: Optional[libzfs.ZFS]=None,
68-
host: Optional[libiocage.lib.Host]=None,
69-
logger: Optional[libiocage.lib.Logger]=None,
70-
new=False) -> None:
66+
data: Union[str, dict]={},
67+
zfs: Optional[libzfs.ZFS]=None,
68+
host: Optional[libiocage.lib.Host.Host]=None,
69+
logger: Optional[libiocage.lib.Logger.Logger]=None,
70+
new: bool=False) -> None:
7171
"""
7272
Initializes a Jail
7373
@@ -82,7 +82,7 @@ def __init__(self,
8282
host:
8383
Inherit an existing Host instance from ancestor classes
8484
85-
logger (libiocage.lib.Logger): (optional)
85+
logger:
8686
Inherit an existing Logger instance from ancestor classes
8787
8888
"""
@@ -308,35 +308,32 @@ def create(self, release_name: str) -> None:
308308
self.config.data["release"] = release.name
309309
self.config.save()
310310

311-
def exec(self, command, **kwargs):
311+
def exec(self, command: List[str], **kwargs):
312312
"""
313313
Execute a command in a started jail
314314
315-
command (list):
315+
command:
316316
A list of command and it's arguments
317317
318318
Example: ["/usr/bin/whoami"]
319319
"""
320320

321-
command = ["/usr/sbin/jexec", self.identifier] + command
321+
jexec_command = ["/usr/sbin/jexec", self.identifier] + command
322322

323323
return libiocage.lib.helpers.exec(
324-
command, logger=self.logger, **kwargs
324+
jexec_command, logger=self.logger, **kwargs
325325
)
326326

327-
def passthru(self, command):
327+
def passthru(self, command: List[str]):
328328
"""
329329
Execute a command in a started jail ans passthrough STDIN and STDOUT
330330
331-
command (list):
331+
command:
332332
A list of command and it's arguments
333333
334334
Example: ["/bin/sh"]
335335
"""
336336

337-
if isinstance(command, str):
338-
command = [command]
339-
340337
return libiocage.lib.helpers.exec_passthru(
341338
[
342339
"/usr/sbin/jexec",

libiocage/lib/helpers.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import libiocage.lib.Host
88
import libiocage.lib.Logger
99

10+
from typing import List
11+
1012

1113
def init_zfs(self, zfs):
1214
if isinstance(zfs, libzfs.ZFS):
@@ -46,11 +48,10 @@ def init_logger(self, logger=None):
4648
object.__setattr__(self, 'logger', new_logger)
4749

4850

49-
def exec(command, logger=None, ignore_error=False):
50-
if isinstance(command, str):
51-
command = [command]
51+
def exec(command, logger=None, ignore_error=False) -> (
52+
subprocess.Popen, str, str,):
5253

53-
command_str = " ".join(command)
54+
command_str = " ".join(list([command]))
5455

5556
if logger:
5657
logger.log(f"Executing: {command_str}", level="spam")
@@ -216,21 +217,16 @@ def to_string(data, true="yes", false="no", none="-"):
216217
return str(data)
217218

218219

219-
def exec_passthru(command, logger=None):
220-
if isinstance(command, str):
221-
command = [command]
222-
220+
def exec_passthru(command: List[str], logger=None):
223221
command_str = " ".join(command)
222+
224223
if logger:
225224
logger.spam(f"Executing (interactive): {command_str}")
226225

227-
return subprocess.Popen(command).communicate()
228-
226+
return subprocess.Popen(command_str).communicate()
229227

230-
def exec_raw(command, logger=None, **kwargs):
231-
if isinstance(command, str):
232-
command = [command]
233228

229+
def exec_raw(command: List[str], logger=None, **kwargs):
234230
command_str = " ".join(command)
235231
if logger:
236232
logger.spam(f"Executing (raw): {command_str}")
@@ -241,7 +237,7 @@ def exec_raw(command, logger=None, **kwargs):
241237
)
242238

243239

244-
def exec_iter(command, logger=None):
240+
def exec_iter(command: List[str], logger=None):
245241
process = exec_raw(
246242
command,
247243
logger=logger,

0 commit comments

Comments
 (0)