bazarr/init_db.py

50 lines
1.2 KiB
Python
Raw Normal View History

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
# Check if config_dir exist
if os.path.exists(config_dir) == True:
2017-09-16 08:49:46 +08:00
pass
else:
# Create config_dir directory tree
2017-10-22 11:01:24 +08:00
try:
os.mkdir(os.path.join(config_dir))
2017-10-22 11:01:24 +08:00
except OSError:
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
# 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:
os.mkdir(os.path.join(config_dir, 'db'))
2017-10-22 11:01:24 +08:00
except OSError:
pass
try:
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()
# Close SQL script file
fd.close()
2017-09-16 08:49:46 +08:00
# Open database connection
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')