From 1512feb4ddae7af210c8dcb0a814883d970b8888 Mon Sep 17 00:00:00 2001 From: realiti4 Date: Wed, 10 Nov 2021 22:42:18 +0300 Subject: [PATCH 1/2] package tools and cprint func --- pip_upgrade/main.py | 11 +++++-- pip_upgrade/tool.py | 7 +++-- pip_upgrade/tools/__init__.py | 5 +++ pip_upgrade/{utils => tools}/config.py | 0 pip_upgrade/tools/cprint.py | 31 +++++++++++++++++++ .../__init__.py => tools/golkhe_dev.py} | 0 pip_upgrade/{utils => tools}/updater_dev.py | 0 setup.py | 4 +-- test/run_test.py | 31 +++++++++++++++++++ 9 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 pip_upgrade/tools/__init__.py rename pip_upgrade/{utils => tools}/config.py (100%) create mode 100644 pip_upgrade/tools/cprint.py rename pip_upgrade/{utils/__init__.py => tools/golkhe_dev.py} (100%) rename pip_upgrade/{utils => tools}/updater_dev.py (100%) create mode 100644 test/run_test.py diff --git a/pip_upgrade/main.py b/pip_upgrade/main.py index 5f6b78c..beb9a4f 100644 --- a/pip_upgrade/main.py +++ b/pip_upgrade/main.py @@ -5,7 +5,7 @@ from pathlib import Path from pip_upgrade.tool import PipUpgrade -from pip_upgrade.utils.config import Config +from pip_upgrade.tools import Config parser = argparse.ArgumentParser() parser.add_argument('-e', '--exclude', nargs='+', help="Exclude packages you don't want to upgrade") @@ -15,6 +15,7 @@ parser.add_argument('--clean', action='store_true', help="Clears pip's cache") parser.add_argument('-y', '--yes', action='store_true', help="Accept all upgrades and skip user prompt") parser.add_argument('--reset-config', action='store_true', help='Reset config file to default') +parser.add_argument('--dev', action='store_true', help="Doesn't actually call upgrade at the end") parser.add_argument('-q', '--query', help="Query package dependency info from pypi") args = parser.parse_args() @@ -53,9 +54,13 @@ def clear_cache(): else: print('Aborted, if the folder was wrong, please fill an issue.') -def main(): +def main(dev=False): config = Config() + if dev: + print('Developer Mode') + args.dev = True + if args.reset_config: config._reset() sys.exit() @@ -72,4 +77,4 @@ def main(): pip_upgrade.upgrade() if __name__ == "__main__": - main() + main(dev=False) diff --git a/pip_upgrade/tool.py b/pip_upgrade/tool.py index 3259d20..6a25e29 100644 --- a/pip_upgrade/tool.py +++ b/pip_upgrade/tool.py @@ -4,6 +4,7 @@ import subprocess from pip_upgrade.dependencies_base import DependenciesBase +from pip_upgrade.tools import cprint class PipUpgrade(DependenciesBase): @@ -132,8 +133,7 @@ def upgrade(self): if len(packages) > 0: # Info - print(f"{self.colored}These packages will be upgraded: \033[m{list(packages.keys())}") - # print(f'These packages will be upgraded: {list(packages.keys())}') + cprint('These packages will be upgraded: ', list(packages.keys()), color='green') if self.restorable: restore = self.config['restore']['last_exclude'] print(f'(-r, --repeat : -e {restore})') @@ -145,13 +145,14 @@ def upgrade(self): packages = list(packages.items()) packages = [''.join(x) for x in packages] - if cont_upgrade: + if cont_upgrade and not self.args.dev: subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-U', *packages]) print('All packages are up to date! 🎉') if self.self_check: print("A new update avaliable for pip-upgrade-tool.\nPlease manually upgrade the tool using 'python -m pip install -U pip-upgrade-tool'") + # cprint("A new update avaliable for pip-upgrade-tool.\nPlease manually upgrade the tool using 'python -m pip install -U pip-upgrade-tool'", color='yellow') def _help(self): print("") diff --git a/pip_upgrade/tools/__init__.py b/pip_upgrade/tools/__init__.py new file mode 100644 index 0000000..df06dfb --- /dev/null +++ b/pip_upgrade/tools/__init__.py @@ -0,0 +1,5 @@ +from pip_upgrade.tools.cprint import ColoredPrint +from pip_upgrade.tools.config import Config + +# Initialize cprint +cprint = ColoredPrint() \ No newline at end of file diff --git a/pip_upgrade/utils/config.py b/pip_upgrade/tools/config.py similarity index 100% rename from pip_upgrade/utils/config.py rename to pip_upgrade/tools/config.py diff --git a/pip_upgrade/tools/cprint.py b/pip_upgrade/tools/cprint.py new file mode 100644 index 0000000..9bee70c --- /dev/null +++ b/pip_upgrade/tools/cprint.py @@ -0,0 +1,31 @@ +import os +import sys + +class ColoredPrint(): + def __init__(self): + self.enabled = True + self.color_dict = { + 'green': '\033[32m', + 'yellow': 'notimplemented', + 'default': '\033[m' + } + if not self.terminal_check: + self.enabled = False + + def terminal_check(self): + """Don't print colored if it is cmd""" + return True + + def __call__(self, *input, color='green', disabled=False): + if disabled or not self.enabled: + print(*input) + else: + if isinstance(input, tuple): + print(f"{self.color_dict[color]}{input[0]}{self.color_dict['default']}", *input[1:]) + else: + print(f"{self.color_dict[color]}{input}{self.color_dict['default']}") + +if __name__ == '__main__': + cprint = ColoredPrint() + cprint('heey', 'ha') + print('de') \ No newline at end of file diff --git a/pip_upgrade/utils/__init__.py b/pip_upgrade/tools/golkhe_dev.py similarity index 100% rename from pip_upgrade/utils/__init__.py rename to pip_upgrade/tools/golkhe_dev.py diff --git a/pip_upgrade/utils/updater_dev.py b/pip_upgrade/tools/updater_dev.py similarity index 100% rename from pip_upgrade/utils/updater_dev.py rename to pip_upgrade/tools/updater_dev.py diff --git a/setup.py b/setup.py index d7ac2a9..0b83c64 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pip-upgrade-tool", - version="0.6.1", + version="0.6.2", author="Onur Cetinkol", author_email="realiti44@gmail.com", description="An easy tool for upgrading all of your packages while not breaking dependencies", @@ -21,6 +21,6 @@ "Operating System :: OS Independent", ], python_requires='>=3.6', - packages=["pip_upgrade", "pip_upgrade.utils"], + packages=["pip_upgrade", "pip_upgrade.tools"], install_requires=["packaging"], ) diff --git a/test/run_test.py b/test/run_test.py new file mode 100644 index 0000000..412a869 --- /dev/null +++ b/test/run_test.py @@ -0,0 +1,31 @@ +import pytest +import argparse + +from pip_upgrade.tool import PipUpgrade +from pip_upgrade.utils.config import Config + + +parser = argparse.ArgumentParser() +parser.add_argument('-e', '--exclude', nargs='+', help="Exclude packages you don't want to upgrade") +parser.add_argument('--local', action='store_true', help="Upgrades local packages as well") +parser.add_argument('--novenv', action='store_true', help="Disables venv check") +parser.add_argument('--clear', action='store_true', help="Clears pip's cache") # Deprecated +parser.add_argument('--clean', action='store_true', help="Clears pip's cache") +parser.add_argument('-y', '--yes', action='store_true', help="Accept all upgrades and skip user prompt") +parser.add_argument('--reset-config', action='store_true', help='Reset config file to default') +parser.add_argument('--dev', action='store_true', help="Doesn't actually call upgrade at the end") + +args = parser.parse_args() + +def test_main(): + config = Config() + + args.dev = True + args.yes = True + + pip_upgrade = PipUpgrade(args, config) + + pip_upgrade.get_dependencies() + + pip_upgrade.upgrade() + From f72b4f75bac1493270462fc4ac57c9d416a9ec1e Mon Sep 17 00:00:00 2001 From: realiti4 Date: Wed, 1 Dec 2021 14:38:23 +0300 Subject: [PATCH 2/2] fix key error for _ and - --- pip_upgrade/dependencies_base.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pip_upgrade/dependencies_base.py b/pip_upgrade/dependencies_base.py index b01f191..60389de 100644 --- a/pip_upgrade/dependencies_base.py +++ b/pip_upgrade/dependencies_base.py @@ -38,7 +38,12 @@ def get_dependencies(self): try: pkg_store = self.dict[pkg_name.lower()] except Exception as e: - raise e + if '_' in pkg_name: # Fix for '_' + pkg_name = pkg_name.replace('_', '-') + try: + pkg_store = self.dict[pkg_name] + except Exception as e: + raise e pkg_store.current_version = current_version pkg_store.latest_version = latest_version @@ -104,4 +109,8 @@ def retrieve_dependencies(self): # raise e print(f'Skipping {name}, warning: Name mismatch. This will be improved. Manually upgrade if needed') - + def check_name_in_dict(self): + """ + Case and '_', '-' checks. Returns the true name on dict + """ + return