You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bazarr/init_db.py

50 lines
1.2 KiB

from get_argv import config_dir
import os
7 years ago
import sqlite3
7 years ago
import logging
7 years ago
# Check if config_dir exist
if os.path.exists(config_dir) == True:
7 years ago
pass
else:
# Create config_dir directory tree
7 years ago
try:
os.mkdir(os.path.join(config_dir))
7 years ago
except OSError:
logging.exception("The configuration directory doesn't exist and Bazarr cannot create it (permission issue?).")
exit(2)
7 years ago
# Check if database exist
if os.path.exists(os.path.join(config_dir, 'db/bazarr.db')) == True:
pass
else:
# Create data directory tree
7 years ago
try:
os.mkdir(os.path.join(config_dir, 'db'))
7 years ago
except OSError:
pass
try:
os.mkdir(os.path.join(config_dir, 'log'))
7 years ago
except OSError:
pass
7 years ago
# Get SQL script from file
7 years ago
fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r')
7 years ago
script = fd.read()
# Close SQL script file
fd.close()
7 years ago
# Open database connection
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
7 years ago
c = db.cursor()
# Execute script and commit change to database
c.executescript(script)
# Close database connection
7 years ago
db.close()
logging.info('Database created successfully')