Move templatized file creation to tools
[cascardo/ipsilon.git] / ipsilon / tools / files.py
index 7f3bf7f..a18384c 100755 (executable)
@@ -19,6 +19,7 @@
 
 import os
 import pwd
+from string import Template
 
 
 def fix_user_dirs(path, user=None, mode=0700):
@@ -35,3 +36,11 @@ def fix_user_dirs(path, user=None, mode=0700):
         if pw:
             os.chown(root, pw.pw_uid, pw.pw_gid)
         os.chmod(root, mode)
+
+
+def write_from_template(destfile, template, opts):
+    with open(template) as f:
+        t = Template(f.read())
+    text = t.substitute(**opts)  # pylint: disable=star-args
+    with open(destfile, 'w+') as f:
+        f.write(text)