Merge pull request #17 from robertbaldyga/fs_utils-timeout

Introduce option to define timeout for time-consuming file operations
This commit is contained in:
Robert Baldyga 2024-10-03 09:52:40 +02:00 committed by GitHub
commit 073793c83a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 16 deletions

View File

@ -164,9 +164,8 @@ def create_file(path):
return TestRun.executor.run_expect_success(cmd) return TestRun.executor.run_expect_success(cmd)
def compare(file, other_file): def compare(file, other_file, timeout: timedelta = timedelta(minutes=30)):
output = TestRun.executor.run( output = TestRun.executor.run(f"cmp --silent \"{file}\" \"{other_file}\"", timeout)
f"cmp --silent \"{file}\" \"{other_file}\"")
if output.exit_code == 0: if output.exit_code == 0:
return True return True
elif output.exit_code > 1: elif output.exit_code > 1:
@ -175,9 +174,8 @@ def compare(file, other_file):
return False return False
def diff(file, other_file): def diff(file, other_file, timeout: timedelta = timedelta(minutes=30)):
output = TestRun.executor.run( output = TestRun.executor.run(f"diff \"{file}\" \"{other_file}\"", timeout)
f"diff \"{file}\" \"{other_file}\"")
if output.exit_code == 0: if output.exit_code == 0:
return None return None
elif output.exit_code > 1: elif output.exit_code > 1:
@ -186,6 +184,13 @@ def diff(file, other_file):
return output.stderr return output.stderr
def md5sum(file, binary=True, timeout: timedelta = timedelta(minutes=30)):
output = TestRun.executor.run(f"md5sum {'-b' if binary else ''} {file}", timeout)
if output.exit_code != 0:
raise Exception(f"Md5sum command execution failed! {output.stdout}\n{output.stderr}")
return output.stdout.split()[0]
# For some reason separators other than '/' don't work when using sed on system paths # For some reason separators other than '/' don't work when using sed on system paths
# This requires escaping '/' in pattern and target string # This requires escaping '/' in pattern and target string
def escape_sed_string(string: str, sed_replace: bool = False): def escape_sed_string(string: str, sed_replace: bool = False):

View File

@ -14,18 +14,14 @@ class File(FsItem):
def __init__(self, full_path): def __init__(self, full_path):
FsItem.__init__(self, full_path) FsItem.__init__(self, full_path)
def compare(self, other_file): def compare(self, other_file, timeout: timedelta = timedelta(minutes=30)):
return fs_utils.compare(str(self), str(other_file)) return fs_utils.compare(str(self), str(other_file), timeout)
def diff(self, other_file): def diff(self, other_file, timeout: timedelta = timedelta(minutes=30)):
return fs_utils.diff(str(self), str(other_file)) return fs_utils.diff(str(self), str(other_file), timeout)
def md5sum(self, binary=True): def md5sum(self, binary=True, timeout: timedelta = timedelta(minutes=30)):
output = TestRun.executor.run( return fs_utils.md5sum(str(self), binary, timeout)
f"md5sum {'-b' if binary else ''} {self.full_path}")
if output.exit_code != 0:
raise Exception(f"Md5sum command execution failed! {output.stdout}\n{output.stderr}")
return output.stdout.split()[0]
def read(self): def read(self):
return fs_utils.read_file(str(self)) return fs_utils.read_file(str(self))