Skip to content

Commit 1dbf547

Browse files
author
lukpueh
authored
Merge pull request #202 from lukpueh/add-default-kwarg-helper
Add default timeout setting helper to process module
2 parents bbcbeca + 193da9c commit 1dbf547

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

securesystemslib/process.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@
3737
import subprocess
3838

3939
import securesystemslib.formats
40-
40+
import securesystemslib.settings
4141

4242
DEVNULL = subprocess.DEVNULL
4343
PIPE = subprocess.PIPE
44-
# NOTE: If changed programatically, please do via this process module, e.g.
45-
# securesystemslib.process.SUBPROCESS_TIMEOUT = <seconds>
46-
from securesystemslib.settings import SUBPROCESS_TIMEOUT
47-
4844

4945
log = logging.getLogger(__name__)
5046

47+
def _default_timeout():
48+
"""Helper to use securesystemslib.settings.SUBPROCESS_TIMEOUT as default
49+
argument, and still be able to modify it after the function definitions are
50+
evaluated. """
51+
return securesystemslib.settings.SUBPROCESS_TIMEOUT
52+
53+
5154

52-
def run(cmd, check=True, timeout=SUBPROCESS_TIMEOUT, **kwargs):
55+
def run(cmd, check=True, timeout=_default_timeout(), **kwargs):
5356
"""
5457
<Purpose>
5558
Provide wrapper for `subprocess.run` (see
@@ -126,7 +129,7 @@ def run(cmd, check=True, timeout=SUBPROCESS_TIMEOUT, **kwargs):
126129

127130

128131

129-
def run_duplicate_streams(cmd, timeout=SUBPROCESS_TIMEOUT):
132+
def run_duplicate_streams(cmd, timeout=_default_timeout()):
130133
"""
131134
<Purpose>
132135
Provide a function that executes a command in a subprocess and, upon

tests/test_process.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io
2424
import sys
2525
import securesystemslib.process
26+
import securesystemslib.settings
2627

2728

2829
class Test_Process(unittest.TestCase):
@@ -121,6 +122,20 @@ def test_run_duplicate_streams_timeout(self):
121122
timeout=-1)
122123

123124

125+
def test__default_timeout(self):
126+
"""Test default timeout modification. """
127+
# Backup timeout and check that it is what's returned by _default_timeout()
128+
timeout_old = securesystemslib.settings.SUBPROCESS_TIMEOUT
129+
self.assertEqual(securesystemslib.process._default_timeout(), timeout_old)
130+
131+
# Modify timeout and check that _default_timeout() returns the same value
132+
timeout_new = timeout_old + 1
133+
securesystemslib.settings.SUBPROCESS_TIMEOUT = timeout_new
134+
self.assertEqual(securesystemslib.process._default_timeout(), timeout_new)
135+
136+
# Restore original timeout
137+
securesystemslib.settings.SUBPROCESS_TIMEOUT = timeout_old
138+
124139

125140
if __name__ == "__main__":
126141
unittest.main()

0 commit comments

Comments
 (0)