def script_write(filename, script):
"""Write a script to a file.
Proper execute permissions will be set.
:param ``str`` filename:
File to write to.
:param ``script:
String or iterable returning strings. Can be unicode.
"""
if isinstance(script, six.string_types):
# If the script is fully provided in a string, wrap it in a StringIO
if hasattr(script, 'decode'):
script = io.StringIO(script.decode())
else:
script = io.StringIO(script)
with io.open(filename, 'wb') as f:
for chunk in script:
# The value must be properly encoded
data = chunk.encode(encoding='utf8', errors='replace')
f.write(data)
if os.name == 'posix':
os.fchmod(f.fileno(), 0o755)
评论列表
文章目录