From fd07b0aa486830167ffbc39b7f4ab28b6fc08063 Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Mon, 10 Aug 2020 18:29:21 +0200 Subject: [PATCH 1/3] Add an option to download a package locally --- CHANGELOG | 3 ++ VERSION | 2 +- src/pip2spack/actions/action_dispatcher.py | 3 +- src/pip2spack/actions/dowload/__init__.py | 0 .../actions/dowload/download_action.py | 44 +++++++++++++++++++ src/pip2spack/core/package.py | 2 +- src/pip2spack/core/pypi_package.py | 27 +++++++++++- 7 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/pip2spack/actions/dowload/__init__.py create mode 100644 src/pip2spack/actions/dowload/download_action.py diff --git a/CHANGELOG b/CHANGELOG index d3bc5d9..7ff1121 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +# 1.1 +- Add an option to download latest package locally + # 1.0 - Introduce refactored code - Better messages diff --git a/VERSION b/VERSION index 9f8e9b6..b123147 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0 \ No newline at end of file +1.1 \ No newline at end of file diff --git a/src/pip2spack/actions/action_dispatcher.py b/src/pip2spack/actions/action_dispatcher.py index 67e8268..a9701cf 100644 --- a/src/pip2spack/actions/action_dispatcher.py +++ b/src/pip2spack/actions/action_dispatcher.py @@ -1,13 +1,14 @@ from argparse import ArgumentParser from pip2spack.actions.create.create_action import CreateAction +from pip2spack.actions.dowload.download_action import DownloadAction from pip2spack.actions.update.update_action import UpdateAction from pip2spack.actions.version import ShowVersion from pip2spack.framework.messages import Messages class ActionDispatcher: - ACTION_HANDLERS = [CreateAction, ShowVersion, UpdateAction] + ACTION_HANDLERS = [CreateAction, DownloadAction, ShowVersion, UpdateAction] def __init__(self): self.parser = ArgumentParser() diff --git a/src/pip2spack/actions/dowload/__init__.py b/src/pip2spack/actions/dowload/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/pip2spack/actions/dowload/download_action.py b/src/pip2spack/actions/dowload/download_action.py new file mode 100644 index 0000000..a53ce94 --- /dev/null +++ b/src/pip2spack/actions/dowload/download_action.py @@ -0,0 +1,44 @@ +from pip2spack.actions.action import Action +from pip2spack.framework.helpers import create_directory, create_package +from pip2spack.framework.messages import Messages +from pip2spack.core.pypi_package import PyPiPackage +from pip2spack.core.verification import Verification + + +class DownloadAction(Action): + ACTION = "download" + PARAM_NAME = "ACTION" + + def fill_parser_arguments(self): + self.parser.add_argument('name', type=str, nargs='+', help='Package name on the pypi.org') + + def process_action(self, configuration): + ready_packages = Verification(configuration.name).available_packages + # self.generate_packages(ready_packages) + self.download(ready_packages) + + def download(self, ready_packages): + for key, value in ready_packages.items(): + Messages.info(f"Download latest package for the {key}") + + d_package = PyPiPackage(value) + d_package.download_versions() + + + @staticmethod + def generate_packages(ready_packages): + for key, value in ready_packages.items(): + Messages.info(f"Generating package for the {key}") + + s_package = PyPiPackage(value) + generated_file = s_package.generate_file() + + create_directory(key) + created_package_uri = create_package(key, generated_file) + print(f" OK :: Package for {key} was generated\n\t " + f":: If \'spack install py-{key}\' return any problems, try to uncomment:\n\t " + f":: \tdepends_on('py-setuptools', type='build')\n\t " + f":: at {created_package_uri}") + + + diff --git a/src/pip2spack/core/package.py b/src/pip2spack/core/package.py index f80d121..d17ab16 100644 --- a/src/pip2spack/core/package.py +++ b/src/pip2spack/core/package.py @@ -73,7 +73,7 @@ def replace_marker_with_template(self): def update_newest_versions(self, package_name): sp = PyPiPackage(package_name) - file = sp.generate_custom_file(os.path.dirname(os.path.realpath(self.package_path)), sp.get_versions) + file = sp.generate_custom_file(os.path.dirname(os.path.realpath(self.package_path)), sp._get_versions) return file def save(self, new_file): diff --git a/src/pip2spack/core/pypi_package.py b/src/pip2spack/core/pypi_package.py index c19438d..42a7d09 100644 --- a/src/pip2spack/core/pypi_package.py +++ b/src/pip2spack/core/pypi_package.py @@ -1,8 +1,12 @@ import json +from typing import List +from urllib.request import Request import requests from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader +from pip2spack.framework.messages import Messages + class PyPiPackage: content: dict = {} @@ -25,7 +29,7 @@ def __init__(self, content): self.homepage_builder() @property - def get_versions(self): + def _get_versions(self): self.versions = [] self._versions_formatter() self.version_builder() @@ -131,3 +135,24 @@ def generate_custom_file(abspath: str, versions): env = Environment(loader=FileSystemLoader(abspath)) template = env.get_template('package.py') return template.render({"versions": versions}) + + def download_versions(self, versions: List = None): + if not versions: + latest_version = self.content['info']['version'] + latest_filename = self.content['releases'].get(latest_version)[0]["filename"] + Messages.warn(f"Missing versions parameter, downloading a latest one v{latest_version}") + r = requests.get(url=self.url) + self.__save_content(latest_filename, r.content) + + @staticmethod + def __save_content(name: str, content: bytes): + import os + import tempfile + path = tempfile.gettempdir() + pip2spack = os.path.join(path, "pip2spack") + content_filename = os.path.join(pip2spack, name) + os.makedirs(pip2spack, exist_ok=True) + + with open(content_filename, 'wb') as content_to_save: + content_to_save.write(content) + Messages.info(f"Content was saved into {content_filename}") From d5b2a5a3127101a4037fa52a646d374ce504dcbb Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Mon, 10 Aug 2020 18:33:05 +0200 Subject: [PATCH 2/3] Remove unused code --- .../actions/dowload/download_action.py | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/src/pip2spack/actions/dowload/download_action.py b/src/pip2spack/actions/dowload/download_action.py index a53ce94..9c79488 100644 --- a/src/pip2spack/actions/dowload/download_action.py +++ b/src/pip2spack/actions/dowload/download_action.py @@ -1,5 +1,4 @@ from pip2spack.actions.action import Action -from pip2spack.framework.helpers import create_directory, create_package from pip2spack.framework.messages import Messages from pip2spack.core.pypi_package import PyPiPackage from pip2spack.core.verification import Verification @@ -14,7 +13,6 @@ def fill_parser_arguments(self): def process_action(self, configuration): ready_packages = Verification(configuration.name).available_packages - # self.generate_packages(ready_packages) self.download(ready_packages) def download(self, ready_packages): @@ -23,22 +21,3 @@ def download(self, ready_packages): d_package = PyPiPackage(value) d_package.download_versions() - - - @staticmethod - def generate_packages(ready_packages): - for key, value in ready_packages.items(): - Messages.info(f"Generating package for the {key}") - - s_package = PyPiPackage(value) - generated_file = s_package.generate_file() - - create_directory(key) - created_package_uri = create_package(key, generated_file) - print(f" OK :: Package for {key} was generated\n\t " - f":: If \'spack install py-{key}\' return any problems, try to uncomment:\n\t " - f":: \tdepends_on('py-setuptools', type='build')\n\t " - f":: at {created_package_uri}") - - - From 43eae9ccabf5059c2122f0fd5c0581438a341372 Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Mon, 10 Aug 2020 18:54:29 +0200 Subject: [PATCH 3/3] Remove unused code --- src/pip2spack/actions/dowload/download_action.py | 3 ++- src/pip2spack/framework/messages.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pip2spack/actions/dowload/download_action.py b/src/pip2spack/actions/dowload/download_action.py index 9c79488..1dc1c14 100644 --- a/src/pip2spack/actions/dowload/download_action.py +++ b/src/pip2spack/actions/dowload/download_action.py @@ -15,7 +15,8 @@ def process_action(self, configuration): ready_packages = Verification(configuration.name).available_packages self.download(ready_packages) - def download(self, ready_packages): + @staticmethod + def download(ready_packages): for key, value in ready_packages.items(): Messages.info(f"Download latest package for the {key}") diff --git a/src/pip2spack/framework/messages.py b/src/pip2spack/framework/messages.py index 99d278b..3a46043 100644 --- a/src/pip2spack/framework/messages.py +++ b/src/pip2spack/framework/messages.py @@ -3,7 +3,7 @@ class Messages: def package_availability(available: list, unavailable: list): Messages.info("Package status") if available: - print("Available to convert:") + print("Available:") for key in available: print(f"\t{key}") @@ -12,7 +12,7 @@ def package_availability(available: list, unavailable: list): print() return - print("\nUnavailable to convert:") + print("\nUnavailable:") for p in packages_not_found: print(f'\t{p}') else: