diff --git a/core/__init__.py b/core/__init__.py index 8e726e48..50b283dd 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -23,20 +23,21 @@ except ImportError: if sys.platform == 'win32': sys.ext('Please install pywin32') -PROGRAM_DIR = libs.util.module_root() +APP_ROOT = libs.util.module_path(parent=True) +SOURCE_ROOT = libs.util.module_path() # init preliminaries SYS_ARGV = sys.argv[1:] APP_FILENAME = sys.argv[0] APP_NAME = os.path.basename(APP_FILENAME) -LOG_DIR = os.path.join(PROGRAM_DIR, 'logs') +LOG_DIR = os.path.join(APP_ROOT, 'logs') LOG_FILE = os.path.join(LOG_DIR, 'nzbtomedia.log') PID_FILE = os.path.join(LOG_DIR, 'nzbtomedia.pid') -CONFIG_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg') -CONFIG_SPEC_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg.spec') -CONFIG_MOVIE_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMovie.cfg') -CONFIG_TV_FILE = os.path.join(PROGRAM_DIR, 'autoProcessTv.cfg') -TEST_FILE = os.path.join(PROGRAM_DIR, 'tests', 'test.mp4') +CONFIG_FILE = os.path.join(APP_ROOT, 'autoProcessMedia.cfg') +CONFIG_SPEC_FILE = os.path.join(APP_ROOT, 'autoProcessMedia.cfg.spec') +CONFIG_MOVIE_FILE = os.path.join(APP_ROOT, 'autoProcessMovie.cfg') +CONFIG_TV_FILE = os.path.join(APP_ROOT, 'autoProcessTv.cfg') +TEST_FILE = os.path.join(APP_ROOT, 'tests', 'test.mp4') MYAPP = None import six @@ -747,7 +748,7 @@ def initialize(section=None): if platform.system() == 'Windows': FFMPEG = os.path.join(FFMPEG_PATH, 'ffmpeg.exe') FFPROBE = os.path.join(FFMPEG_PATH, 'ffprobe.exe') - SEVENZIP = os.path.join(PROGRAM_DIR, 'core', 'extractor', 'bin', platform.machine(), '7z.exe') + SEVENZIP = os.path.join(APP_ROOT, 'core', 'extractor', 'bin', platform.machine(), '7z.exe') SHOWEXTRACT = int(str(CFG["Windows"]["show_extraction"]), 0) if not (os.path.isfile(FFMPEG)): # problem diff --git a/core/extractor/__init__.py b/core/extractor/__init__.py index 4796bb3e..a1ed8457 100644 --- a/core/extractor/__init__.py +++ b/core/extractor/__init__.py @@ -19,7 +19,7 @@ def extract(file_path, output_destination): core.logger.error("EXTRACTOR: Could not find 7-zip, Exiting") return False wscriptlocation = os.path.join(os.environ['WINDIR'], 'system32', 'wscript.exe') - invislocation = os.path.join(core.PROGRAM_DIR, 'core', 'extractor', 'bin', 'invisible.vbs') + invislocation = os.path.join(core.APP_ROOT, 'core', 'extractor', 'bin', 'invisible.vbs') cmd_7zip = [wscriptlocation, invislocation, str(core.SHOWEXTRACT), core.SEVENZIP, "x", "-y"] ext_7zip = [".rar", ".zip", ".tar.gz", "tgz", ".tar.bz2", ".tbz", ".tar.lzma", ".tlz", ".7z", ".xz"] extract_commands = dict.fromkeys(ext_7zip, cmd_7zip) diff --git a/core/main_db.py b/core/main_db.py index 939af5b0..c782e763 100644 --- a/core/main_db.py +++ b/core/main_db.py @@ -22,7 +22,7 @@ def db_filename(filename="nzbtomedia.db", suffix=None): """ if suffix: filename = "{0}.{1}".format(filename, suffix) - return core.os.path.join(core.PROGRAM_DIR, filename) + return core.os.path.join(core.APP_ROOT, filename) class DBConnection(object): diff --git a/core/version_check.py b/core/version_check.py index 41c3ccb9..b24c9a17 100644 --- a/core/version_check.py +++ b/core/version_check.py @@ -15,6 +15,7 @@ from six.moves.urllib.request import urlretrieve import core from core import github_api as github, logger +import libs.util class CheckVersion(object): @@ -48,7 +49,7 @@ class CheckVersion(object): """ # check if we're a windows build - if os.path.isdir(os.path.join(core.PROGRAM_DIR, u'.git')): + if os.path.isdir(os.path.join(core.APP_ROOT, u'.git')): install_type = 'git' else: install_type = 'source' @@ -79,7 +80,23 @@ class CheckVersion(object): def update(self): if self.updater.need_update(): - return self.updater.update() + result = self.updater.update() + self.clean() + return result + + @staticmethod + def clean(): + # Clean libs + result = libs.util.git_clean( + remove_directories=True, + force=True, + ignore_rules=True, + paths=[ + libs.LIB_ROOT, + core.SOURCE_ROOT, + ], + ) + logger.debug(result) class UpdateManager(object): @@ -174,9 +191,9 @@ class GitUpdateManager(UpdateManager): try: logger.log(u"Executing {cmd} with your shell in {directory}".format - (cmd=cmd, directory=core.PROGRAM_DIR), logger.DEBUG) + (cmd=cmd, directory=core.APP_ROOT), logger.DEBUG) p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - shell=True, cwd=core.PROGRAM_DIR) + shell=True, cwd=core.APP_ROOT) output, err = p.communicate() exit_status = p.returncode @@ -345,7 +362,7 @@ class SourceUpdateManager(UpdateManager): def _find_installed_version(self): - version_file = os.path.join(core.PROGRAM_DIR, u'version.txt') + version_file = os.path.join(core.APP_ROOT, u'version.txt') if not os.path.isfile(version_file): self._cur_commit_hash = None @@ -437,11 +454,11 @@ class SourceUpdateManager(UpdateManager): """ tar_download_url = 'https://github.com/{org}/{repo}/tarball/{branch}'.format( org=self.github_repo_user, repo=self.github_repo, branch=self.branch) - version_path = os.path.join(core.PROGRAM_DIR, u'version.txt') + version_path = os.path.join(core.APP_ROOT, u'version.txt') try: # prepare the update dir - sb_update_dir = os.path.join(core.PROGRAM_DIR, u'sb-update') + sb_update_dir = os.path.join(core.APP_ROOT, u'sb-update') if os.path.isdir(sb_update_dir): logger.log(u"Clearing out update folder {dir} before extracting".format(dir=sb_update_dir)) @@ -485,12 +502,12 @@ class SourceUpdateManager(UpdateManager): # walk temp folder and move files to main folder logger.log(u"Moving files from {source} to {destination}".format - (source=content_dir, destination=core.PROGRAM_DIR)) + (source=content_dir, destination=core.APP_ROOT)) for dirname, dirnames, filenames in os.walk(content_dir): # @UnusedVariable dirname = dirname[len(content_dir) + 1:] for curfile in filenames: old_path = os.path.join(content_dir, dirname, curfile) - new_path = os.path.join(core.PROGRAM_DIR, dirname, curfile) + new_path = os.path.join(core.APP_ROOT, dirname, curfile) # Avoid DLL access problem on WIN32/64 # These files needing to be updated manually diff --git a/libs/__init__.py b/libs/__init__.py index ed03df1a..6fab928e 100644 --- a/libs/__init__.py +++ b/libs/__init__.py @@ -4,8 +4,7 @@ import sys import libs.util -ROOT_DIR = libs.util.module_root() -LIB_DIR = os.path.join(ROOT_DIR, 'libs') +LIB_ROOT = libs.util.module_path() COMMON = 'common' CUSTOM = 'custom' @@ -18,7 +17,7 @@ MANDATORY = { CUSTOM, } DIRECTORY = { - lib: os.path.join(LIB_DIR, lib) + lib: os.path.join(LIB_ROOT, lib) for lib in [COMMON, CUSTOM, PY2, WIN] } diff --git a/libs/__main__.py b/libs/__main__.py index f82ad647..767f0d6e 100644 --- a/libs/__main__.py +++ b/libs/__main__.py @@ -5,7 +5,7 @@ import time import libs if __name__ == '__main__': - os.chdir(libs.LIB_DIR) + os.chdir(libs.LIB_ROOT) for lib, directory in libs.DIRECTORY.items(): if lib == 'custom': continue diff --git a/libs/util.py b/libs/util.py index 3b597d69..cde820f2 100644 --- a/libs/util.py +++ b/libs/util.py @@ -4,19 +4,20 @@ import sys import os __all__ = [ - 'module_root', + 'module_path', 'add_path', ] -def module_root(module=__file__): +def module_path(module=__file__, parent=False): try: path = module.__file__ except AttributeError: path = module directory = os.path.dirname(path) - parent = os.path.join(directory, os.pardir) - absolute = os.path.abspath(parent) + if parent: + directory = os.path.join(directory, os.pardir) + absolute = os.path.abspath(directory) normalized = os.path.normpath(absolute) return normalized @@ -58,3 +59,31 @@ def install_requirements( args.append(path) subprocess.call(args) + + +def git_clean(remove_directories=False, force=False, dry_run=False, interactive=False, quiet=False, exclude=None, + ignore_rules=False, clean_ignored=False, paths=None): + command = ['git', 'clean'] + if remove_directories: + command.append('-d') + if force: + command.append('--force') + if interactive: + command.append('--interactive') + if quiet: + command.append('--quiet') + if dry_run: + command.append('--dry-run') + if exclude: + command.append('--exclude={pattern}'.format(pattern=exclude)) + if ignore_rules: + command.append('-x') + if clean_ignored: + command.append('-X') + if paths: + try: + paths = paths.split(' ') + except AttributeError: + pass + command.extend(paths) + return subprocess.check_output(command)