Build dated RPMs by default
[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 shutil
23 import subprocess
24 from string import Template
25
26
27 logger = None
28
29
30 def parse_args():
31     parser = argparse.ArgumentParser(description=\
32         'Run a test Ipsilon instance from the checkout directory')
33     parser.add_argument('--workdir', default=os.path.join(os.getcwd(), 'qrun'),
34                         help="Directory in which db/session files are stored")
35     parser.add_argument('--cleanup', '-c', action='store_true', default=False,
36                         help="Wipe workdir before starting")
37     return vars(parser.parse_args())
38
39
40 CONF_TEMPLATE="templates/install/ipsilon.conf"
41
42 ADMIN_TEMPLATE='''
43 CREATE TABLE login_config (name TEXT,option TEXT,value TEXT);
44 INSERT INTO login_config VALUES('global', 'enabled', 'testauth');
45 '''
46
47 USERS_TEMPLATE='''
48 CREATE TABLE users(name TEXT, option TEXT, value TEXT);
49 INSERT INTO users VALUES('admin', 'is_admin', '1');
50 '''
51
52 def config(workdir):
53     os.makedirs(workdir)
54     os.makedirs(os.path.join(workdir, 'sessions'))
55
56     admin_db = os.path.join(workdir, 'adminconfig.sqlite')
57     sql = os.path.join(workdir, 'admin.sql')
58     with open(sql, 'w+') as f:
59         f.write(ADMIN_TEMPLATE)
60     subprocess.call(['sqlite3', '-init', sql, admin_db, '.quit'])
61
62     users_db = os.path.join(workdir, 'userprefs.sqlite')
63     sql = os.path.join(workdir, 'users.sql')
64     with open(sql, 'w+') as f:
65         f.write(USERS_TEMPLATE)
66     subprocess.call(['sqlite3', '-init', sql, users_db, '.quit'])
67
68     trans_db = os.path.join(workdir, 'transactions.sqlite')
69
70     with open(CONF_TEMPLATE) as f:
71         conf_template = f.read()
72     t = Template(conf_template)
73     text = t.substitute({'debugging': 'True',
74                          'instance': 'idp',
75                          'staticdir': os.getcwd(),
76                          'datadir': workdir,
77                          'admindb': admin_db,
78                          'usersdb': users_db,
79                          'transdb': trans_db,
80                          'sesstype': 'file',
81                          'sessopt': 'path',
82                          'sessval': os.path.join(workdir, 'sessions'),
83                          'secure': 'False'})
84     conf = os.path.join(workdir, 'ipsilon.conf')
85     with open(conf, 'w+') as f:
86         f.write(text)
87     return conf
88
89 if __name__ == '__main__':
90
91     args = parse_args()
92
93     penv = dict()
94     penv.update(os.environ)
95     penv['PYTHONPATH'] = os.getcwd()
96
97     exe = os.path.join(os.getcwd(), 'ipsilon/ipsilon')
98
99     if args['cleanup']:
100         shutil.rmtree(args['workdir'])
101
102     if not os.path.exists(args['workdir']):
103         conf = config(args['workdir'])
104     else:
105         conf = os.path.join(args['workdir'], 'ipsilon.conf')
106
107     if not os.path.exists(os.path.join(args['workdir'], 'ui')):
108         os.symlink(os.path.join(os.getcwd(), 'ui'),
109                    os.path.join(args['workdir'], 'ui'))
110
111
112     os.chdir(args['workdir'])
113
114     p = subprocess.Popen([exe, conf], env=penv)
115     p.wait()