Add transactions db default paths
[cascardo/ipsilon.git] / quickrun.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import argparse
21 import os
22 import subprocess
23 from string import Template
24
25
26 logger = None
27
28
29 def parse_args():
30     parser = argparse.ArgumentParser(description=\
31         'Run a test Ipsilon instance from the checkout directory')
32     parser.add_argument('--workdir', default=os.path.join(os.getcwd(), 'qrun'),
33                         help="Directory in which db/session files are stored")
34     return vars(parser.parse_args())
35
36
37 CONF_TEMPLATE='''
38 [global]
39 debug = True
40
41 log.screen = True
42 base.mount = "/idp"
43 base.dir = "${BASEDIR}"
44 admin.config.db = "${ADMINDB}"
45 user.prefs.db = "${USERSDB}"
46 transactions.db = "${TRANSDB}"
47
48 tools.sessions.on = True
49 tools.sessions.storage_type = "file"
50 tools.sessions.storage_path = "${WORKDIR}/sessions"
51 tools.sessions.timeout = 60
52 tools.sessions.secure = False
53 tools.sessions.httponly = False
54 '''
55
56 ADMIN_TEMPLATE='''
57 CREATE TABLE login_config (name TEXT,option TEXT,value TEXT);
58 INSERT INTO login_config VALUES('global', 'order', 'testauth');
59 '''
60
61 USERS_TEMPLATE='''
62 CREATE TABLE users(name TEXT, option TEXT, value TEXT);
63 INSERT INTO users VALUES('admin', 'is_admin', '1');
64 '''
65
66 def config(workdir):
67     os.makedirs(workdir)
68     os.makedirs(os.path.join(workdir, 'sessions'))
69
70     admin_db = os.path.join(workdir, 'adminconfig.sqlite')
71     sql = os.path.join(workdir, 'admin.sql')
72     with open(sql, 'w+') as f:
73         f.write(ADMIN_TEMPLATE)
74     subprocess.call(['sqlite3', '-init', sql, admin_db, '.quit'])
75
76     users_db = os.path.join(workdir, 'users.sqlite')
77     sql = os.path.join(workdir, 'users.sql')
78     with open(sql, 'w+') as f:
79         f.write(USERS_TEMPLATE)
80     subprocess.call(['sqlite3', '-init', sql, users_db, '.quit'])
81
82     trans_db = os.path.join(workdir, 'transactions.sqlite')
83
84     t = Template(CONF_TEMPLATE)
85     text = t.substitute({'BASEDIR': os.getcwd(),
86                          'WORKDIR': workdir,
87                          'ADMINDB': admin_db,
88                          'USERSDB': users_db,
89                          'TRANSDB': trans_db})
90     conf = os.path.join(workdir, 'ipsilon.conf')
91     with open(conf, 'w+') as f:
92         f.write(text)
93     return conf
94
95 if __name__ == '__main__':
96
97     args = parse_args()
98
99     penv = dict()
100     penv.update(os.environ)
101     penv['PYTHONPATH'] = './'
102
103     if not os.path.exists(args['workdir']):
104         conf = config(args['workdir'])
105     else:
106         conf = os.path.join(args['workdir'], 'ipsilon.conf')
107
108     p = subprocess.Popen(['./ipsilon/ipsilon', conf], env=penv)
109     p.wait()