Clean .pyc files before starting

This commit is contained in:
Ruud
2014-06-15 22:22:34 +02:00
parent a196a499ae
commit e659aba176
3 changed files with 38 additions and 28 deletions
+6 -1
View File
@@ -19,7 +19,12 @@ base_path = dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(base_path, 'libs')) sys.path.insert(0, os.path.join(base_path, 'libs'))
from couchpotato.environment import Env from couchpotato.environment import Env
from couchpotato.core.helpers.variable import getDataDir from couchpotato.core.helpers.variable import getDataDir, removePyc
# Remove pyc files before dynamic load (sees .pyc files regular .py modules)
removePyc(base_path)
class Loader(object): class Loader(object):
+3 -26
View File
@@ -11,6 +11,7 @@ from threading import RLock
from couchpotato.api import addApiView from couchpotato.api import addApiView
from couchpotato.core.event import addEvent, fireEvent, fireEventAsync from couchpotato.core.event import addEvent, fireEvent, fireEventAsync
from couchpotato.core.helpers.encoding import sp from couchpotato.core.helpers.encoding import sp
from couchpotato.core.helpers.variable import removePyc
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin from couchpotato.core.plugins.base import Plugin
from couchpotato.environment import Env from couchpotato.environment import Env
@@ -143,7 +144,7 @@ class Updater(Plugin):
def doShutdown(self): def doShutdown(self):
if not Env.get('dev'): if not Env.get('dev'):
self.updater.deletePyc(show_logs = False) removePyc(Env.get('app_dir'), show_logs = False)
return super(Updater, self).doShutdown() return super(Updater, self).doShutdown()
@@ -181,30 +182,6 @@ class BaseUpdater(Plugin):
def check(self): def check(self):
pass pass
def deletePyc(self, only_excess = True, show_logs = True):
for root, dirs, files in os.walk(Env.get('app_dir')):
pyc_files = filter(lambda filename: filename.endswith('.pyc'), files)
py_files = set(filter(lambda filename: filename.endswith('.py'), files))
excess_pyc_files = filter(lambda pyc_filename: pyc_filename[:-1] not in py_files, pyc_files) if only_excess else pyc_files
for excess_pyc_file in excess_pyc_files:
full_path = os.path.join(root, excess_pyc_file)
if show_logs: log.debug('Removing old PYC file: %s', full_path)
try:
os.remove(full_path)
except:
log.error('Couldn\'t remove %s: %s', (full_path, traceback.format_exc()))
for dir_name in dirs:
full_path = os.path.join(root, dir_name)
if len(os.listdir(full_path)) == 0:
try:
os.rmdir(full_path)
except:
log.error('Couldn\'t remove empty directory %s: %s', (full_path, traceback.format_exc()))
class GitUpdater(BaseUpdater): class GitUpdater(BaseUpdater):
@@ -328,7 +305,7 @@ class SourceUpdater(BaseUpdater):
data_dir = Env.get('data_dir') data_dir = Env.get('data_dir')
# Get list of files we want to overwrite # Get list of files we want to overwrite
self.deletePyc() removePyc(app_dir)
existing_files = [] existing_files = []
for root, subfiles, filenames in os.walk(app_dir): for root, subfiles, filenames in os.walk(app_dir):
for filename in filenames: for filename in filenames:
+29 -1
View File
@@ -6,8 +6,9 @@ import random
import re import re
import string import string
import sys import sys
import traceback
from couchpotato.core.helpers.encoding import simplifyString, toSafeString, ss from couchpotato.core.helpers.encoding import simplifyString, toSafeString, ss, sp
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
import six import six
from six.moves import map, zip, filter from six.moves import map, zip, filter
@@ -313,3 +314,30 @@ under_pat = re.compile(r'_([a-z])')
def underscoreToCamel(name): def underscoreToCamel(name):
return under_pat.sub(lambda x: x.group(1).upper(), name) return under_pat.sub(lambda x: x.group(1).upper(), name)
def removePyc(folder, only_excess = True, show_logs = True):
folder = sp(folder)
for root, dirs, files in os.walk(folder):
pyc_files = filter(lambda filename: filename.endswith('.pyc'), files)
py_files = set(filter(lambda filename: filename.endswith('.py'), files))
excess_pyc_files = filter(lambda pyc_filename: pyc_filename[:-1] not in py_files, pyc_files) if only_excess else pyc_files
for excess_pyc_file in excess_pyc_files:
full_path = os.path.join(root, excess_pyc_file)
if show_logs: log.debug('Removing old PYC file: %s', full_path)
try:
os.remove(full_path)
except:
log.error('Couldn\'t remove %s: %s', (full_path, traceback.format_exc()))
for dir_name in dirs:
full_path = os.path.join(root, dir_name)
if len(os.listdir(full_path)) == 0:
try:
os.rmdir(full_path)
except:
log.error('Couldn\'t remove empty directory %s: %s', (full_path, traceback.format_exc()))