forked from anusharanganathan/RDFDatabank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_db.py
61 lines (51 loc) · 1.82 KB
/
setup_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sqlalchemy as sa
from rdfdatabank.model import init_model
from rdfdatabank.lib.auth_entry import add_user, add_user_groups, add_silo
import ConfigParser
import sys, os
class setupDB():
def __init__(self, config_file='/var/lib/databank/production.ini'):
if not os.path.exists(config_file):
print "Config file not found"
sys.exit()
c = ConfigParser.ConfigParser()
c.read(config_file)
if not 'app:main' in c.sections():
print "Section app:main not found in config file"
sys.exit()
engine = sa.create_engine(c.get('app:main', 'sqlalchemy.url'))
init_model(engine)
return
def addUser(self, user_details):
if not ('username' in user_details and user_details['username'] and \
'password' in user_details and user_details['password'] and \
('name' in user_details and user_details['name'] or \
('firstname' in user_details and user_details['firstname'] and \
'lastname' in user_details and user_details['lastname']))):
return False
add_user(user_details)
return True
def addSilo(self, silo):
add_silo(silo)
return
def addUserGroup(self, username, silo, permission):
groups = []
groups.append((silo, permission))
add_user_groups(username, groups)
return
if __name__ == "__main__":
#Initialize sqlalchemy
s = setupDB()
#add user
username = sys.argv[1]
password = sys.argv[2]
email = sys.argv[3]
user_details = {
'username':u'%s'%username,
'password':u"%s"%password,
'name':u'Databank Administrator',
'email':u"%s"%email
}
s.addUser(user_details)
#Add user membership
s.addUserGroup(username, '*', 'administrator')