[toolsutils] Small fixes to fill_templated_file()
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 10 Dec 2015 17:57:55 +0100
changeset 10966 8909593f46d4
parent 10965 b1ba18016151
child 10967 3f620fd1ed18
[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
toolsutils.py
--- 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"""