From 69cd554dfb4093e0d6d4cadd30e04f92811a93ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Wed, 4 Jul 2018 08:27:56 +0200 Subject: [PATCH] utils.script: Add support for "open_mode" to support binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the "Script" only uses text mode to open files, let's allow to set any mode by using "open_mode" to allow passing binary data, when needed. Signed-off-by: Lukáš Doktor --- avocado/utils/script.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/avocado/utils/script.py b/avocado/utils/script.py index 70c0b469..774cd17e 100644 --- a/avocado/utils/script.py +++ b/avocado/utils/script.py @@ -39,7 +39,7 @@ class Script(object): Class that represents a script. """ - def __init__(self, path, content, mode=DEFAULT_MODE): + def __init__(self, path, content, mode=DEFAULT_MODE, open_mode='w'): """ Creates an instance of :class:`Script`. @@ -54,6 +54,7 @@ class Script(object): self.content = content self.mode = mode self.stored = False + self.open_mode = open_mode def __repr__(self): return '%s(path="%s", stored=%s)' % (self.__class__.__name__, @@ -78,7 +79,7 @@ class Script(object): """ dirname = os.path.dirname(self.path) utils_path.init_dir(dirname) - with open(self.path, 'w') as fd: + with open(self.path, self.open_mode) as fd: fd.write(self.content) os.chmod(self.path, self.mode) self.stored = True @@ -104,7 +105,7 @@ class TemporaryScript(Script): Class that represents a temporary script. """ - def __init__(self, name, content, prefix='avocado_script', mode=DEFAULT_MODE): + def __init__(self, name, content, prefix='avocado_script', mode=DEFAULT_MODE, open_mode='w'): """ Creates an instance of :class:`TemporaryScript`. @@ -124,6 +125,7 @@ class TemporaryScript(Script): self.content = content self.mode = mode self.stored = False + self.open_mode = open_mode def __del__(self): self.remove() -- GitLab