2018-10-31 19:34:40 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2018-08-23 19:58:15 +00:00
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import logging
|
2018-10-31 19:34:40 +00:00
|
|
|
import time
|
2018-08-23 19:58:15 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
from cork import Cork
|
2018-08-23 19:58:15 +00:00
|
|
|
from configparser import ConfigParser
|
2018-10-31 19:34:40 +00:00
|
|
|
from get_args import args
|
2018-08-23 19:58:15 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
# Check if args.config_dir exist
|
|
|
|
if not os.path.exists(args.config_dir):
|
2018-08-23 19:58:15 +00:00
|
|
|
# Create config_dir directory tree
|
|
|
|
try:
|
2018-10-31 19:34:40 +00:00
|
|
|
os.mkdir(os.path.join(args.config_dir))
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug("BAZARR Created data directory")
|
2018-08-23 19:58:15 +00:00
|
|
|
except OSError:
|
2018-10-31 19:34:40 +00:00
|
|
|
logging.exception(
|
|
|
|
"BAZARR The configuration directory doesn't exist and Bazarr cannot create it (permission issue?).")
|
2018-08-23 19:58:15 +00:00
|
|
|
exit(2)
|
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
if not os.path.exists(os.path.join(args.config_dir, 'config')):
|
|
|
|
os.mkdir(os.path.join(args.config_dir, 'config'))
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug("BAZARR Created config folder")
|
2018-10-31 19:34:40 +00:00
|
|
|
if not os.path.exists(os.path.join(args.config_dir, 'db')):
|
|
|
|
os.mkdir(os.path.join(args.config_dir, 'db'))
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug("BAZARR Created db folder")
|
2018-10-31 19:34:40 +00:00
|
|
|
if not os.path.exists(os.path.join(args.config_dir, 'log')):
|
|
|
|
os.mkdir(os.path.join(args.config_dir, 'log'))
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug("BAZARR Created log folder")
|
2018-11-02 19:08:07 +00:00
|
|
|
|
|
|
|
if not os.path.exists(os.path.join(config_dir, 'config', 'releases.txt')):
|
|
|
|
from check_update import check_releases
|
|
|
|
check_releases()
|
|
|
|
logging.debug("BAZARR Created releases file")
|
2018-08-23 19:58:15 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
config_file = os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini'))
|
2018-08-23 19:58:15 +00:00
|
|
|
|
2018-09-12 02:32:09 +00:00
|
|
|
cfg = ConfigParser()
|
2018-08-23 19:58:15 +00:00
|
|
|
try:
|
|
|
|
# Get SQL script from file
|
|
|
|
fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r')
|
|
|
|
script = fd.read()
|
|
|
|
|
|
|
|
# Close SQL script file
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
# Open database connection
|
2018-10-31 19:34:40 +00:00
|
|
|
db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
|
2018-08-23 19:58:15 +00:00
|
|
|
c = db.cursor()
|
|
|
|
|
|
|
|
# Execute script and commit change to database
|
|
|
|
c.executescript(script)
|
|
|
|
|
|
|
|
# Close database connection
|
|
|
|
db.close()
|
|
|
|
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.info('BAZARR Database created successfully')
|
2018-08-23 19:58:15 +00:00
|
|
|
except:
|
|
|
|
pass
|
2018-09-12 02:32:09 +00:00
|
|
|
|
|
|
|
# Remove unused settings
|
|
|
|
try:
|
|
|
|
with open(config_file, 'r') as f:
|
|
|
|
cfg.read_file(f)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2018-09-17 12:18:52 +00:00
|
|
|
if cfg.has_section('auth'):
|
|
|
|
if cfg.has_option('auth', 'enabled'):
|
|
|
|
enabled = cfg.getboolean('auth', 'enabled')
|
2018-10-31 19:34:40 +00:00
|
|
|
if enabled:
|
2018-09-17 12:18:52 +00:00
|
|
|
cfg.set('auth', 'type', 'basic')
|
2018-10-31 19:34:40 +00:00
|
|
|
else:
|
2018-09-17 12:20:41 +00:00
|
|
|
cfg.set('auth', 'type', 'None')
|
2018-09-17 12:18:52 +00:00
|
|
|
cfg.remove_option('auth', 'enabled')
|
|
|
|
with open(config_file, 'w+') as configfile:
|
|
|
|
cfg.write(configfile)
|
2018-10-30 18:06:30 +00:00
|
|
|
|
|
|
|
if cfg.has_section('general'):
|
|
|
|
if cfg.has_option('general', 'log_level'):
|
|
|
|
cfg.remove_option('general', 'log_level')
|
|
|
|
cfg.set('general', 'debug', 'False')
|
|
|
|
with open(config_file, 'w+') as configfile:
|
|
|
|
cfg.write(configfile)
|
2018-09-12 02:32:09 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
if not os.path.exists(os.path.normpath(os.path.join(args.config_dir, 'config', 'users.json'))):
|
|
|
|
cork = Cork(os.path.normpath(os.path.join(args.config_dir, 'config')), initialize=True)
|
2018-09-12 02:32:09 +00:00
|
|
|
|
|
|
|
cork._store.roles[''] = 100
|
|
|
|
cork._store.save_roles()
|
|
|
|
|
|
|
|
tstamp = str(time.time())
|
|
|
|
username = password = ''
|
|
|
|
cork._store.users[username] = {
|
|
|
|
'role': '',
|
|
|
|
'hash': cork._hash(username, password),
|
|
|
|
'email_addr': username,
|
|
|
|
'desc': username,
|
|
|
|
'creation_date': tstamp
|
|
|
|
}
|
|
|
|
cork._store.save_users()
|