2018-08-16 10:01:49 +08:00
|
|
|
from get_argv import config_dir
|
|
|
|
|
2017-10-20 20:59:21 +08:00
|
|
|
import os
|
2017-09-16 08:49:46 +08:00
|
|
|
import sqlite3
|
2017-12-06 12:07:37 +08:00
|
|
|
import logging
|
2017-09-16 08:49:46 +08:00
|
|
|
|
2018-08-16 10:01:49 +08:00
|
|
|
# Check if config_dir exist
|
|
|
|
if os.path.exists(config_dir) == True:
|
2017-09-16 08:49:46 +08:00
|
|
|
pass
|
|
|
|
else:
|
2018-08-16 10:01:49 +08:00
|
|
|
# Create config_dir directory tree
|
2017-10-22 11:01:24 +08:00
|
|
|
try:
|
2018-08-16 10:01:49 +08:00
|
|
|
os.mkdir(os.path.join(config_dir))
|
2017-10-22 11:01:24 +08:00
|
|
|
except OSError:
|
2018-08-16 10:01:49 +08:00
|
|
|
logging.exception("The configuration directory doesn't exist and Bazarr cannot create it (permission issue?).")
|
|
|
|
exit(2)
|
2017-10-22 11:01:24 +08:00
|
|
|
|
2018-08-16 10:01:49 +08:00
|
|
|
# Check if database exist
|
|
|
|
if os.path.exists(os.path.join(config_dir, 'db/bazarr.db')) == True:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Create data directory tree
|
2017-10-22 11:01:24 +08:00
|
|
|
try:
|
2018-08-16 10:01:49 +08:00
|
|
|
os.mkdir(os.path.join(config_dir, 'db'))
|
2017-10-22 11:01:24 +08:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2018-08-16 10:01:49 +08:00
|
|
|
os.mkdir(os.path.join(config_dir, 'log'))
|
2017-10-22 11:01:24 +08:00
|
|
|
except OSError:
|
|
|
|
pass
|
2017-10-22 10:51:15 +08:00
|
|
|
|
2017-09-16 08:49:46 +08:00
|
|
|
# Get SQL script from file
|
2017-10-22 08:02:25 +08:00
|
|
|
fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r')
|
2017-09-16 08:49:46 +08:00
|
|
|
script = fd.read()
|
|
|
|
|
2017-12-01 03:31:31 +08:00
|
|
|
# Close SQL script file
|
|
|
|
fd.close()
|
|
|
|
|
2017-09-16 08:49:46 +08:00
|
|
|
# Open database connection
|
2018-08-16 10:01:49 +08:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2017-09-16 08:49:46 +08:00
|
|
|
c = db.cursor()
|
|
|
|
|
|
|
|
# Execute script and commit change to database
|
|
|
|
c.executescript(script)
|
|
|
|
|
|
|
|
# Close database connection
|
2017-12-06 12:07:37 +08:00
|
|
|
db.close()
|
|
|
|
|
|
|
|
logging.info('Database created successfully')
|