Move templatized file creation to tools
authorSimo Sorce <simo@redhat.com>
Fri, 18 Apr 2014 04:16:12 +0000 (00:16 -0400)
committerSimo Sorce <simo@redhat.com>
Mon, 21 Apr 2014 04:05:05 +0000 (00:05 -0400)
Signed-off-by: Simo Sorce <simo@redhat.com>
ipsilon/install/ipsilon-client-install
ipsilon/tools/files.py

index f49e351..b9cc4b6 100755 (executable)
@@ -21,7 +21,7 @@ from ipsilon.tools.saml2metadata import Metadata
 from ipsilon.tools.saml2metadata import SAML2_NAMEID_MAP
 from ipsilon.tools.saml2metadata import SAML2_SERVICE_MAP
 from ipsilon.tools.certs import Certificate
-from string import Template
+from ipsilon.tools import files
 import argparse
 import logging
 import os
@@ -118,20 +118,15 @@ def saml2():
             # default location, enable the default page
             psp = ''
 
-        with open(SAML2_TEMPLATE) as f:
-            template = f.read()
-        t = Template(template)
-        hunk = t.substitute(saml_base=args['saml_base'],
-                            saml_protect=saml_protect,
-                            saml_sp_key=c.key,
-                            saml_sp_cert=c.cert,
-                            saml_sp_meta=sp_metafile,
-                            saml_idp_meta=idp_metafile,
-                            saml_sp=args['saml_sp'],
-                            saml_auth=saml_auth, sp=psp)
-
-        with open(SAML2_CONFFILE, 'w+') as f:
-            f.write(hunk)
+        samlopts = {'saml_base': args['saml_base'],
+                    'saml_protect': saml_protect,
+                    'saml_sp_key': c.key,
+                    'saml_sp_cert': c.cert,
+                    'saml_sp_meta': sp_metafile,
+                    'saml_idp_meta': idp_metafile,
+                    'saml_sp': args['saml_sp'],
+                    'saml_auth': saml_auth, sp=psp}
+        files.write_from_template(SAML2_CONFFILE, SAML2_TEMPLATE, samlopts)
 
         files.fix_user_dirs(SAML2_HTTPDIR, args['httpd_user'])
 
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)