提交 69cd554d 编写于 作者: L Lukáš Doktor

utils.script: Add support for "open_mode" to support binary

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: NLukáš Doktor <ldoktor@redhat.com>
上级 5b7f26f0
......@@ -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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册