Restore ability to run from checkout
[cascardo/ipsilon.git] / quickrun.py
diff --git a/quickrun.py b/quickrun.py
new file mode 100755 (executable)
index 0000000..caa9627
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/python
+#
+# Copyright (C) 2014  Simo Sorce <simo@redhat.com>
+#
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import argparse
+import os
+import subprocess
+from string import Template
+
+
+logger = None
+
+
+def parse_args():
+    parser = argparse.ArgumentParser(description=\
+        'Run a test Ipsilon instance from the checkout directory')
+    parser.add_argument('--workdir', default=os.path.join(os.getcwd(), 'tmp'),
+                        help="Directory in which db/session files are stored")
+    return vars(parser.parse_args())
+
+
+CONF_TEMPLATE='''
+[global]
+debug = True
+
+log.screen = True
+base.mount = "/idp"
+base.dir = "${BASEDIR}"
+admin.config.db = "${ADMINDB}"
+user.prefs.db = "${USERSDB}"
+
+tools.sessions.on = True
+tools.sessions.storage_type = "file"
+tools.sessions.storage_path = "${WORKDIR}/sessions"
+tools.sessions.timeout = 60
+'''
+
+ADMIN_TEMPLATE='''
+CREATE TABLE login_config (name TEXT,option TEXT,value TEXT);
+INSERT INTO login_config VALUES('global', 'order', 'testauth');
+'''
+
+USERS_TEMPLATE='''
+CREATE TABLE users(name TEXT, option TEXT, value TEXT);
+INSERT INTO users VALUES('admin', 'is_admin', '1');
+'''
+
+if __name__ == '__main__':
+
+    args = parse_args()
+
+    penv = dict()
+    penv.update(os.environ)
+    penv['PYTHONPATH'] = './'
+
+    if not os.path.exists(args['workdir']):
+        os.makedirs(args['workdir'])
+        os.makedirs(os.path.join(args['workdir'], 'sessions'))
+
+    admin_db = os.path.join(args['workdir'], 'adminconfig.sqlite')
+    sql = os.path.join(args['workdir'], 'admin.sql')
+    with open(sql, 'w+') as f:
+        f.write(ADMIN_TEMPLATE)
+    subprocess.call(['sqlite3', '-init', sql, admin_db, '.quit'])
+
+    users_db = os.path.join(args['workdir'], 'users.sqlite')
+    sql = os.path.join(args['workdir'], 'users.sql')
+    with open(sql, 'w+') as f:
+        f.write(USERS_TEMPLATE)
+    subprocess.call(['sqlite3', '-init', sql, users_db, '.quit'])
+
+    t = Template(CONF_TEMPLATE)
+    text = t.substitute({'BASEDIR': os.getcwd(),
+                         'WORKDIR': args['workdir'],
+                         'ADMINDB': admin_db,
+                         'USERSDB': users_db})
+    conf = os.path.join(args['workdir'], 'ipsilon.conf')
+    with open(conf, 'w+') as f:
+        f.write(text)
+
+
+    p = subprocess.Popen(['./ipsilon/ipsilon', conf], env=penv)
+    p.wait()