Try NoSQL
This commit is contained in:
1 parent
aab10fb599
commit
a254886bad
65 files changed
+7372
-761
No files matched your search
+14
-67
@@ -1,3 +1,4 @@
|
||||
from CodernityDB.database_thread_safe import ThreadSafeDatabase
|
||||
from argparse import ArgumentParser
|
||||
from cache import FileSystemCache
|
||||
from couchpotato import KeyHandler, LoginHandler, LogoutHandler
|
||||
@@ -82,52 +83,25 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
|
||||
Env.set('encoding', encoding)
|
||||
|
||||
# Do db stuff
|
||||
db_path = toUnicode(os.path.join(data_dir, 'couchpotato.db'))
|
||||
db_path = toUnicode(os.path.join(data_dir, 'database'))
|
||||
|
||||
# Backup before start and cleanup old databases
|
||||
new_backup = toUnicode(os.path.join(data_dir, 'db_backup', str(int(time.time()))))
|
||||
if not os.path.isdir(new_backup): os.makedirs(new_backup)
|
||||
# Check if database exists
|
||||
print db_path
|
||||
|
||||
# Remove older backups, keep backups 3 days or at least 3
|
||||
backups = []
|
||||
for directory in os.listdir(os.path.dirname(new_backup)):
|
||||
backup = toUnicode(os.path.join(os.path.dirname(new_backup), directory))
|
||||
if os.path.isdir(backup):
|
||||
backups.append(backup)
|
||||
db = ThreadSafeDatabase(db_path)
|
||||
db_exists = db.exists()
|
||||
if db_exists:
|
||||
db.open()
|
||||
else:
|
||||
db.create()
|
||||
|
||||
latest_backup = tryInt(os.path.basename(sorted(backups)[-1])) if len(backups) > 0 else 0
|
||||
if latest_backup < time.time() - 3600:
|
||||
# Create path and copy
|
||||
src_files = [options.config_file, db_path, db_path + '-shm', db_path + '-wal']
|
||||
for src_file in src_files:
|
||||
if os.path.isfile(src_file):
|
||||
dst_file = toUnicode(os.path.join(new_backup, os.path.basename(src_file)))
|
||||
shutil.copyfile(src_file, dst_file)
|
||||
|
||||
# Try and copy stats seperately
|
||||
try: shutil.copystat(src_file, dst_file)
|
||||
except: pass
|
||||
|
||||
total_backups = len(backups)
|
||||
for backup in backups:
|
||||
if total_backups > 3:
|
||||
if tryInt(os.path.basename(backup)) < time.time() - 259200:
|
||||
for the_file in os.listdir(backup):
|
||||
file_path = os.path.join(backup, the_file)
|
||||
try:
|
||||
if os.path.isfile(file_path):
|
||||
os.remove(file_path)
|
||||
except:
|
||||
raise
|
||||
|
||||
os.rmdir(backup)
|
||||
total_backups -= 1
|
||||
# TODO:Backup before start and cleanup old databases
|
||||
|
||||
# Register environment settings
|
||||
Env.set('app_dir', toUnicode(base_path))
|
||||
Env.set('data_dir', toUnicode(data_dir))
|
||||
Env.set('log_path', toUnicode(os.path.join(log_dir, 'CouchPotato.log')))
|
||||
Env.set('db_path', toUnicode('sqlite:///' + db_path))
|
||||
Env.set('db', db)
|
||||
Env.set('cache_dir', toUnicode(os.path.join(data_dir, 'cache')))
|
||||
Env.set('cache', FileSystemCache(toUnicode(os.path.join(Env.get('cache_dir'), 'python'))))
|
||||
Env.set('console_log', options.console_log)
|
||||
@@ -183,34 +157,6 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
|
||||
log.warning('%s %s %s line:%s', (category, message, filename, lineno))
|
||||
warnings.showwarning = customwarn
|
||||
|
||||
# Check if database exists
|
||||
db = Env.get('db_path')
|
||||
db_exists = os.path.isfile(toUnicode(db_path))
|
||||
|
||||
# Load migrations
|
||||
if False and db_exists:
|
||||
|
||||
from migrate.versioning.api import version_control, db_version, version, upgrade
|
||||
repo = os.path.join(base_path, 'couchpotato', 'core', 'migration')
|
||||
|
||||
latest_db_version = version(repo)
|
||||
try:
|
||||
current_db_version = db_version(db, repo)
|
||||
except:
|
||||
version_control(db, repo, version = latest_db_version)
|
||||
current_db_version = db_version(db, repo)
|
||||
|
||||
if current_db_version < latest_db_version:
|
||||
if development:
|
||||
log.error('There is a database migration ready, but you are running development mode, so it won\'t be used. If you see this, you are stupid. Please disable development mode.')
|
||||
else:
|
||||
log.info('Doing database upgrade. From %d to %d', (current_db_version, latest_db_version))
|
||||
upgrade(db, repo)
|
||||
|
||||
# Configure Database
|
||||
from couchpotato.core.settings.model import setup
|
||||
setup()
|
||||
|
||||
# Create app
|
||||
from couchpotato import WebHandler
|
||||
web_base = ('/' + Env.setting('url_base').lstrip('/') + '/') if Env.setting('url_base') else '/'
|
||||
@@ -279,6 +225,7 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
|
||||
# Fill database with needed stuff
|
||||
if not db_exists:
|
||||
fireEvent('app.initialize', in_order = True)
|
||||
fireEvent('database.setup')
|
||||
|
||||
# Go go go!
|
||||
from tornado.ioloop import IOLoop
|
||||
@@ -287,7 +234,7 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
|
||||
# Some logging and fire load event
|
||||
try: log.info('Starting server on port %(port)s', config)
|
||||
except: pass
|
||||
fireEventAsync('app.load')
|
||||
fireEventAsync('app.load2')
|
||||
|
||||
if config['ssl_cert'] and config['ssl_key']:
|
||||
server = HTTPServer(application, no_keep_alive = True, ssl_options = {
|
||||
|
||||
Reference in new issue
Block a user