-
Notifications
You must be signed in to change notification settings - Fork 11
/
db.py
59 lines (46 loc) · 1.56 KB
/
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
import os
import sys
import sqlite3
from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool
from sqlalchemy.orm import sessionmaker
ROOT = os.path.abspath(os.path.dirname(__file__))
path = lambda *x: os.path.normpath(os.path.join(ROOT, *x))
SWCARPENTRY_ADMIN_PATH = os.environ.get('SWCARPENTRY_ADMIN_PATH')
if SWCARPENTRY_ADMIN_PATH is None:
SWCARPENTRY_ADMIN_PATH = os.curdir
ROSTER_DB_PATH = os.path.join(SWCARPENTRY_ADMIN_PATH, 'roster.db')
_engine = None
_Session = None
def get_engine():
global _engine
if _engine is None:
_engine = create_engine(
'sqlite:///%s' % ROSTER_DB_PATH,
connect_args={'check_same_thread':False},
poolclass=StaticPool
)
return _engine
def get_session():
global _Session
if _Session is None:
_Session = sessionmaker(bind=get_engine())
return _Session()
def create_roster_db():
'''
Create roster.db in the Software Carpentry admin directory by
feeding it roster.sql.
This is normally done by the Makefile, but doing it ourselves
here relieves many users of an annoying dependency on make.
'''
roster_sql_path = os.path.join(os.path.dirname(ROSTER_DB_PATH),
'roster.sql')
print "Reading from %s." % roster_sql_path
print "Creating %s." % ROSTER_DB_PATH
conn = sqlite3.connect(ROSTER_DB_PATH)
c = conn.cursor()
c.executescript(open(roster_sql_path).read())
c.close()
conn.close()
if __name__ == '__main__':
print 'Using the database at %s.' % ROSTER_DB_PATH