Skip to content

Commit

Permalink
Store the python version on startup in config (pymedusa#5892)
Browse files Browse the repository at this point in the history
* Store the python (major) version on startup in config.
This can be used to check when a user starts medusa the next time with a different version.

* Replace dogpile cache, if we up/down the running python interpreter.

* Move the python check to start.py

* Update tmdb.py

revert

* * Moved the logic into the migrate_python_version function
* Removed the save_config, as we should depend on the saven on shutdown.
* store full semver version, instead of the major version.

* Forgot to remove this

* Small changes to make code more concise

* More specific message
  • Loading branch information
p0psicles authored and medariox committed Dec 17, 2018
1 parent e0d79ea commit e769def
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
7 changes: 6 additions & 1 deletion SickBeard.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/usr/bin/env python2.7
#!/usr/bin/env python
# -*- coding: utf-8 -*
"""Script for backwards compatibility."""
from __future__ import unicode_literals

import sys

from medusa.__main__ import main

if __name__ == '__main__':
if sys.version_info.major == 3 and sys.version_info.minor < 5:
print('Medusa supports Python 2 from version 2.7.10 and Python 3 from version 3.5.0, exiting!')
raise Exception('Incorrect Python version. Shutting down!')
main()
28 changes: 27 additions & 1 deletion medusa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def initialize(self, console_logging=True):
app.GIT_PASSWORD = check_setting_str(app.CFG, 'General', 'git_password', '', censor_log='low')
app.GIT_TOKEN = check_setting_str(app.CFG, 'General', 'git_token', '', censor_log='low', encrypted=True)
app.DEVELOPER = bool(check_setting_int(app.CFG, 'General', 'developer', 0))
app.PYTHON_VERSION = check_setting_list(app.CFG, 'General', 'python_version', [], transform=int)

# debugging
app.DEBUG = bool(check_setting_int(app.CFG, 'General', 'debug', 0))
Expand Down Expand Up @@ -1077,9 +1078,12 @@ def initialize(self, console_logging=True):
# Disable flag to erase cache
app.SUBTITLES_ERASE_CACHE = False

# Check if we start with a different Python version since last start
python_version_changed = self.migrate_python_version()

# Check if we need to perform a restore of the cache folder
Application.restore_cache_folder(app.CACHE_DIR)
cache.configure(app.CACHE_DIR)
cache.configure(app.CACHE_DIR, replace=python_version_changed)

# Rebuild the censored list
app_logger.rebuild_censored_list()
Expand Down Expand Up @@ -1247,6 +1251,27 @@ def path_leaf(path):
folder_path = os.path.join(cache_folder, name)
helpers.remove_folder(folder_path)

@staticmethod
def migrate_python_version():
"""
Perform some cleanups in case we switch between major Python versions.
It's possible to switch from Python version 2 to 3 or vice versa.
In that case we might wanna run some sanity actions, to make sure everything keeps working.
:return: True if the major Python version has changed since last start
:return type: Boolean
"""
# TODO: Leaving this here as a marking for when we merge the python3 changes.
current_version = app.PYTHON_VERSION
app.PYTHON_VERSION = list(sys.version_info)[:3]

# Run some sanitation when switching between Python versions
if current_version and current_version[0] != app.PYTHON_VERSION[0]:
return True

return False

@staticmethod
def start_threads():
"""Start application threads."""
Expand Down Expand Up @@ -1545,6 +1570,7 @@ def save_config():
new_config['General']['calendar_icons'] = int(app.CALENDAR_ICONS)
new_config['General']['no_restart'] = int(app.NO_RESTART)
new_config['General']['developer'] = int(app.DEVELOPER)
new_config['General']['python_version'] = app.PYTHON_VERSION
new_config['General']['display_all_seasons'] = int(app.DISPLAY_ALL_SEASONS)
new_config['General']['news_last_read'] = app.NEWS_LAST_READ
new_config['General']['broken_providers'] = helpers.get_broken_providers() or app.BROKEN_PROVIDERS
Expand Down
1 change: 1 addition & 0 deletions medusa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self):
# static configuration
self.LOCALE = None, None
self.OS_USER = None
self.PYTHON_VERSION = []
self.OPENSSL_VERSION = None
self.APP_VERSION = None
self.MAJOR_DB_VERSION = None
Expand Down
10 changes: 5 additions & 5 deletions medusa/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,34 @@ def release_write_lock(self):
anidb_cache = make_region()


def configure(cache_dir):
def configure(cache_dir, replace=False):
"""Configure caches."""
# memory cache
from subliminal.cache import region as subliminal_cache

memory_cache.configure('dogpile.cache.memory', expiration_time=timedelta(hours=1))

# subliminal cache
subliminal_cache.configure('dogpile.cache.dbm',
subliminal_cache.configure('dogpile.cache.dbm', replace_existing_backend=replace,
expiration_time=timedelta(days=30),
arguments={
'filename': os.path.join(cache_dir, 'subliminal.dbm'),
'lock_factory': MutexLock})

# application cache
cache.configure('dogpile.cache.dbm',
cache.configure('dogpile.cache.dbm', replace_existing_backend=replace,
expiration_time=timedelta(days=1),
arguments={'filename': os.path.join(cache_dir, 'application.dbm'),
'lock_factory': MutexLock})

# recommended series cache
recommended_series_cache.configure('dogpile.cache.dbm',
recommended_series_cache.configure('dogpile.cache.dbm', replace_existing_backend=replace,
expiration_time=timedelta(days=7),
arguments={'filename': os.path.join(cache_dir, 'recommended.dbm'),
'lock_factory': MutexLock})

# anidb (adba) series cache
anidb_cache.configure('dogpile.cache.dbm',
anidb_cache.configure('dogpile.cache.dbm', replace_existing_backend=replace,
expiration_time=timedelta(days=3),
arguments={'filename': os.path.join(cache_dir, 'anidb.dbm'),
'lock_factory': MutexLock})
Expand Down
7 changes: 6 additions & 1 deletion start.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/usr/bin/env python2.7
#!/usr/bin/env python
# -*- coding: utf-8 -*
"""Startup script."""
from __future__ import unicode_literals

import sys

from medusa.__main__ import main

if __name__ == '__main__':
if sys.version_info.major == 3 and sys.version_info.minor < 5:
print('Medusa supports Python 2 from version 2.7.10 and Python 3 from version 3.5.0, exiting!')
raise Exception('Incorrect Python version. Shutting down!')
main()

0 comments on commit e769def

Please sign in to comment.