From 9d0097fa686bf6e1ed18fa92ea74cc59b8733c5b Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Wed, 19 Dec 2018 19:05:40 -0500 Subject: [PATCH 1/7] Refactor module_root -> module_path --- core/__init__.py | 2 +- libs/__init__.py | 2 +- libs/util.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/__init__.py b/core/__init__.py index 8e726e48..9bcadb8c 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -23,7 +23,7 @@ except ImportError: if sys.platform == 'win32': sys.ext('Please install pywin32') -PROGRAM_DIR = libs.util.module_root() +PROGRAM_DIR = libs.util.module_path() # init preliminaries SYS_ARGV = sys.argv[1:] diff --git a/libs/__init__.py b/libs/__init__.py index ed03df1a..ea030fef 100644 --- a/libs/__init__.py +++ b/libs/__init__.py @@ -4,7 +4,7 @@ import sys import libs.util -ROOT_DIR = libs.util.module_root() +ROOT_DIR = libs.util.module_path() LIB_DIR = os.path.join(ROOT_DIR, 'libs') COMMON = 'common' diff --git a/libs/util.py b/libs/util.py index 3b597d69..df43c3fb 100644 --- a/libs/util.py +++ b/libs/util.py @@ -4,12 +4,12 @@ import sys import os __all__ = [ - 'module_root', + 'module_path', 'add_path', ] -def module_root(module=__file__): +def module_path(module=__file__): try: path = module.__file__ except AttributeError: From 32e0d7dba2a24ff0a55038c3e8dcf84fc0e47428 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Wed, 19 Dec 2018 19:10:35 -0500 Subject: [PATCH 2/7] Refactor PROGRAM_DIR -> APP_DIR --- core/__init__.py | 16 ++++++++-------- core/extractor/__init__.py | 2 +- core/main_db.py | 2 +- core/version_check.py | 16 ++++++++-------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/core/__init__.py b/core/__init__.py index 9bcadb8c..278a16fe 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -23,20 +23,20 @@ except ImportError: if sys.platform == 'win32': sys.ext('Please install pywin32') -PROGRAM_DIR = libs.util.module_path() +APP_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 +747,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..e8f99a69 100644 --- a/core/version_check.py +++ b/core/version_check.py @@ -48,7 +48,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' @@ -174,9 +174,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 +345,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 +437,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 +485,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 From 200a8d882707b6f901555a65ff73183e107e8c00 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Wed, 19 Dec 2018 19:19:13 -0500 Subject: [PATCH 3/7] Refactor LIB_DIR -> LIB_ROOT --- libs/__init__.py | 5 ++--- libs/__main__.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/libs/__init__.py b/libs/__init__.py index ea030fef..6fab928e 100644 --- a/libs/__init__.py +++ b/libs/__init__.py @@ -4,8 +4,7 @@ import sys import libs.util -ROOT_DIR = libs.util.module_path() -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 From 339d2878b4a6044c3d35c51242da2dffaeb66dff Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Wed, 19 Dec 2018 19:09:48 -0500 Subject: [PATCH 4/7] Add path parent option to module path and default to using local path --- core/__init__.py | 2 +- libs/util.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/__init__.py b/core/__init__.py index 278a16fe..8f8e19fc 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -23,7 +23,7 @@ except ImportError: if sys.platform == 'win32': sys.ext('Please install pywin32') -APP_ROOT = libs.util.module_path() +APP_ROOT = libs.util.module_path(parent=True) # init preliminaries SYS_ARGV = sys.argv[1:] diff --git a/libs/util.py b/libs/util.py index df43c3fb..5ac9f943 100644 --- a/libs/util.py +++ b/libs/util.py @@ -9,14 +9,15 @@ __all__ = [ ] -def module_path(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 From 7aff860390e1999a7d6a66d2e1447cd1c46772a7 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Wed, 19 Dec 2018 19:16:17 -0500 Subject: [PATCH 5/7] Add git clean functionality --- libs/util.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/libs/util.py b/libs/util.py index 5ac9f943..c0281715 100644 --- a/libs/util.py +++ b/libs/util.py @@ -59,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) + subprocess.call(command) From 70acfc22e7b32e6a7aaaa9b813d5de4b21220226 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Wed, 19 Dec 2018 19:27:52 -0500 Subject: [PATCH 6/7] Add cleanup upon update --- core/__init__.py | 1 + core/version_check.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/__init__.py b/core/__init__.py index 8f8e19fc..50b283dd 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -24,6 +24,7 @@ except ImportError: sys.ext('Please install pywin32') APP_ROOT = libs.util.module_path(parent=True) +SOURCE_ROOT = libs.util.module_path() # init preliminaries SYS_ARGV = sys.argv[1:] diff --git a/core/version_check.py b/core/version_check.py index e8f99a69..37445d16 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): @@ -79,7 +80,22 @@ 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 + libs.util.git_clean( + remove_directories=True, + force=True, + ignore_rules=True, + paths=[ + libs.LIB_ROOT, + core.SOURCE_ROOT, + ], + ) class UpdateManager(object): From 2fce0e40c3a757054af14d4ffd35bc3c5226ccf7 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Wed, 19 Dec 2018 19:40:13 -0500 Subject: [PATCH 7/7] Add logging for cleanup --- core/version_check.py | 3 ++- libs/util.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/version_check.py b/core/version_check.py index 37445d16..b24c9a17 100644 --- a/core/version_check.py +++ b/core/version_check.py @@ -87,7 +87,7 @@ class CheckVersion(object): @staticmethod def clean(): # Clean libs - libs.util.git_clean( + result = libs.util.git_clean( remove_directories=True, force=True, ignore_rules=True, @@ -96,6 +96,7 @@ class CheckVersion(object): core.SOURCE_ROOT, ], ) + logger.debug(result) class UpdateManager(object): diff --git a/libs/util.py b/libs/util.py index c0281715..cde820f2 100644 --- a/libs/util.py +++ b/libs/util.py @@ -86,4 +86,4 @@ def git_clean(remove_directories=False, force=False, dry_run=False, interactive= except AttributeError: pass command.extend(paths) - subprocess.call(command) + return subprocess.check_output(command)