diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 55840dd5..70bbcf95 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -1,5 +1,9 @@ #!/usr/bin/env python2 # coding=utf-8 + +import cleanup +cleanup.clean('core', 'libs') + import datetime import os import sys diff --git a/changelog.txt b/changelog.txt index 077747c3..50c57ac0 100644 --- a/changelog.txt +++ b/changelog.txt @@ -41,6 +41,15 @@ Delete invisible.cmd Use args instead of Wscript.Arguments +V11.7 12/25/2018 + +Merry Christmas and Happy Holidays! + +Add cleanup script to clean up bytecode +Add automatic cleanup on update + +NOTE: Cleanup will force-run every time during a transitional period to minimize issues with upcoming refactoring + V11.06 11/03/2018 updates to incorporate importMode for NzbDrone/Sonarr and Radarr. diff --git a/cleanup.py b/cleanup.py new file mode 100644 index 00000000..be13558d --- /dev/null +++ b/cleanup.py @@ -0,0 +1,86 @@ +from __future__ import print_function + +import subprocess +import sys + + +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): + """Execute git clean commands.""" + 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: + try: + exclude = exclude.split(' ') + except AttributeError: + pass + for exclusion in exclude: + command.append('--exclude={pattern}'.format(pattern=exclusion)) + 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) + + +def clean_bytecode(): + """Clean bytecode files.""" + try: + result = git_clean( + remove_directories=True, + force=True, + exclude=[ + '*.*', # exclude everything + '!*.py[co]', # except bytecode + '!**/__pycache__/', # and __pycache__ folders + ], + ) + except subprocess.CalledProcessError as error: + sys.exit(error.returncode) + else: + return result + + +def clean_folders(*paths): + """Clean obsolete folders.""" + try: + result = git_clean( + remove_directories=True, + force=True, + ignore_rules=True, + paths=paths, + ) + except subprocess.CalledProcessError as error: + sys.exit(error.returncode) + else: + return result + + +def clean(*paths): + """Clean up bytecode and obsolete folders.""" + print('-- Cleaning bytecode --') + result = clean_bytecode() + print(result or 'No bytecode to clean\n') + if paths: + print('-- Cleaning folders: {} --'.format(paths)) + result = clean_folders(*paths) + print(result or 'No folders to clean\n') + + +if __name__ == '__main__': + clean('libs', 'core') diff --git a/core/version_check.py b/core/version_check.py index b24c9a17..e8bad6ca 100644 --- a/core/version_check.py +++ b/core/version_check.py @@ -13,6 +13,7 @@ import traceback from six.moves.urllib.request import urlretrieve +import cleanup import core from core import github_api as github, logger import libs.util @@ -81,23 +82,9 @@ class CheckVersion(object): def update(self): if self.updater.need_update(): result = self.updater.update() - self.clean() + cleanup.clean('core', 'libs') 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): def get_github_repo_user(self): diff --git a/libs/util.py b/libs/util.py index cde820f2..5ac9f943 100644 --- a/libs/util.py +++ b/libs/util.py @@ -59,31 +59,3 @@ 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) diff --git a/nzbToMedia.py b/nzbToMedia.py index c29a3de4..d3b09e7f 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -623,6 +623,9 @@ from __future__ import print_function +import cleanup +cleanup.clean('core', 'libs') + import datetime import os import sys