[toolsutils] Small fixes to fill_templated_file()
* Use context managers to close _both_ files
* Use io.open to enforce text mode on both python 2 and 3
--- a/toolsutils.py Thu Dec 10 18:00:41 2015 +0100
+++ b/toolsutils.py Thu Dec 10 17:57:55 2015 +0100
@@ -22,6 +22,7 @@
# XXX move most of this in logilab.common (shellutils ?)
+import io
import os, sys
import subprocess
from os import listdir, makedirs, environ, chmod, walk, remove
@@ -153,10 +154,10 @@
shutil.copyfile(fpath, tfpath)
def fill_templated_file(fpath, tfpath, context):
- fobj = open(tfpath, 'w')
- templated = open(fpath).read()
- fobj.write(templated % context)
- fobj.close()
+ with io.open(fpath, encoding='ascii') as fobj:
+ template = fobj.read()
+ with io.open(tfpath, 'w', encoding='ascii') as fobj:
+ fobj.write(template % context)
def restrict_perms_to_user(filepath, log=None):
"""set -rw------- permission on the given file"""