From b7cbf7d85cb61ab4ec7ce0ba7f4c8e9921e05af7 Mon Sep 17 00:00:00 2001 From: Jochem Berends Date: Mon, 24 Jun 2024 10:03:05 +0200 Subject: [PATCH] Refactor code and improve readability Several changes were made across various modules and tests to improve readability and maintainability. The key edits include modifying verbose instructions to make them more succinct, discarding the "snapcraft.yaml" file, and refactoring certain functions. Unnecessary import statements were also removed. This refactor should make the code easier to understand and work with. --- .idea/inspectionProfiles/Project_Default.xml | 18 +++++++---- kecpkg/commands/build.py | 16 +++++---- kecpkg/commands/sign.py | 9 ++++-- kecpkg/create.py | 26 ++++++--------- kecpkg/files/templates/script.py.template | 1 - kecpkg/gpg.py | 28 +++++++--------- kecpkg/utils.py | 6 ++-- snapcraft.yaml | 34 -------------------- tests/commands/test_build.py | 6 ++-- tests/commands/test_upload.py | 3 +- tests/utils.py | 9 +++--- 11 files changed, 60 insertions(+), 96 deletions(-) delete mode 100644 snapcraft.yaml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index cbd2af5..7d88f00 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -4,13 +4,17 @@ diff --git a/kecpkg/commands/build.py b/kecpkg/commands/build.py index 46b0f3e..a2ec128 100644 --- a/kecpkg/commands/build.py +++ b/kecpkg/commands/build.py @@ -55,23 +55,26 @@ "update_package_info", is_flag=True, default=True, - help="Update the `package-info.json` for the KE-crunch execution to point to correct entrypoint based on " - "settings. This is okay to leave ON. Use `--no-update` if you have a custom `package-info.json`.", + help="Update the `package-info.json` for the KE-crunch execution to point to correct " + "entrypoint based on settings. This is okay to leave ON. Use `--no-update` if you " + "have a custom `package-info.json`.", ) @click.option( "--sign/--no-sign", "do_sign", is_flag=True, default=False, - help="Sign the contents of the package with a cryptographic key from the keyring. Defaults to not sign.", + help="Sign the contents of the package with a cryptographic key from the keyring. Defaults " + "to not sign.", ) @click.option( "--keyid", "--key-id", "-k", "sign_keyid", - help="ID of the cryptographic key to do the sign the contents of the built package. If not provided it " - "will use the default key from the KECPKG keystore. Use in combination with `--sign`", + help="ID of the cryptographic key to do the sign the contents of the built package. If not" + "provided it will use the default key from the KECPKG keystore. Use in combination " + "with `--sign`", ) @click.option( "--passphrase", @@ -167,7 +170,8 @@ def generate_artifact_hashes(package_dir, artifacts, settings, verbose=False): artifacts_content = [] for af in artifacts: - # we do not need to create a hash from the ARTIFACTS and ARTIFACTS.SIG file if they are present in the list + # we do not need to create a hash from the ARTIFACTS and ARTIFACTS.SIG file if they + # are present in the list if af not in [artifacts_fn, artifacts_fn + ".SIG"]: af_fp = os.path.join(package_dir, af) artifacts_content.append( diff --git a/kecpkg/commands/sign.py b/kecpkg/commands/sign.py index c135612..e30fbd5 100644 --- a/kecpkg/commands/sign.py +++ b/kecpkg/commands/sign.py @@ -56,7 +56,8 @@ "do_import", type=click.Path(exists=True), help="Import secret keyfile (in .asc) to the KECPKG keyring which will be used for signing. " - "You can export a created key in gpg with `gpg -a --export-secret-key [keyID] > secret_key.asc`.", + "You can export a created key in gpg with `gpg -a --export-secret-key [keyID] > " + "secret_key.asc`.", ) @click.option( "--delete-key", @@ -78,7 +79,8 @@ "-e", "do_export_key", type=click.Path(), - help="Export public key to filename with `--keyid KeyID` in .ASC format for public distribution.", + help="Export public key to filename with `--keyid KeyID` in .ASC format for " + "public distribution.", ) @click.option( "--clear-keyring", @@ -137,7 +139,8 @@ def _do_list(gpg, explain=False): else: if explain: echo_info( - "No keys found in KECPKG keyring. Use `--import-key` or `--create-key` to add a " + "No keys found in KECPKG keyring. Use `--import-key` or " + "`--create-key` to add a " "secret key to the KECPKG keyring in order to sign KECPKG's." ) sys.exit(1) diff --git a/kecpkg/create.py b/kecpkg/create.py index e1c2703..06459b6 100644 --- a/kecpkg/create.py +++ b/kecpkg/create.py @@ -76,16 +76,18 @@ def create_venv(package_dir, settings, pypath=None, use_global=False, verbose=Fa :param package_dir: the full path to the package directory :param settings: the settings dict (including the venv_dir name to create the right venv) - :param pypath: absolute path to the python binary interpreter to create the virtual environment with - :param use_global: Use global sysem site packages when creating virtual environment (default False) + :param pypath: absolute path to the python binary interpreter to create the virtual + environment with + :param use_global: Use global sysem site packages when creating virtual environment + (default False) :param verbose: Use verbosity (default False) """ venv_dir = os.path.join(package_dir, settings.get("venv_dir")) if not pypath: - from distutils.spawn import find_executable + import shutil - pypath = find_executable(get_proper_python()) + pypath = shutil.which(get_proper_python()) command = [sys.executable, "-m", "virtualenv", venv_dir, "-p", pypath] if use_global: # no cov @@ -118,11 +120,8 @@ def pip_install_venv(package_dir, settings, verbose=False): if not os.path.exists( os.path.join(package_dir, settings.get("requirements_filename")) ): - echo_failure( - "could not find requirements.txt to install, check if `{}` exists or update settings".format( - settings.get("requirements_filename") - ) - ) + echo_failure(f"could not find requirements.txt to install, check if " + f"`{settings.get('requirements_filename')}` exists or update settings") sys.exit(1) install_command = [ @@ -144,14 +143,7 @@ def pip_install_venv(package_dir, settings, verbose=False): ) ) result = None - if six.PY3: - result = subprocess.run(install_command, shell=NEED_SUBPROCESS_SHELL) - return result.returncode - elif six.PY2: - result = subprocess.check_output( - install_command, shell=NEED_SUBPROCESS_SHELL - ) - return result and 0 or -1 + result = subprocess.run(install_command, shell=NEED_SUBPROCESS_SHELL) if result: echo_success(str(result)) diff --git a/kecpkg/files/templates/script.py.template b/kecpkg/files/templates/script.py.template index d9f184a..7640375 100644 --- a/kecpkg/files/templates/script.py.template +++ b/kecpkg/files/templates/script.py.template @@ -6,7 +6,6 @@ This script is part of package `{{ package_name }}` Created by: {{ name }} <{{ email }}> {%- endif -%} """ -from pykechain import get_project import sys __version__ = '{{ version }}' diff --git a/kecpkg/gpg.py b/kecpkg/gpg.py index d25e977..bb57669 100644 --- a/kecpkg/gpg.py +++ b/kecpkg/gpg.py @@ -7,33 +7,26 @@ import subprocess import sys from datetime import datetime +from typing import Any, Optional import six -from typing import Any, List, Optional - from gnupg import GPG from kecpkg.settings import GNUPG_KECPKG_HOME from kecpkg.utils import ( - ON_LINUX, - ON_WINDOWS, - ON_MACOS, - echo_failure, - read_chunks, - echo_info, - ensure_dir_exists, + ON_LINUX, ON_MACOS, ON_WINDOWS, echo_failure, echo_info, ensure_dir_exists, read_chunks, ) LOGLEVEL = logging.INFO def hash_of_file(path, algorithm="sha256"): - """Return the hash digest of a file.""" + """Return the my_hash digest of a file.""" with open(path, "rb") as archive: - hash = hashlib.new(algorithm) + my_hash = hashlib.new(algorithm) for chunk in read_chunks(archive): - hash.update(chunk) - return hash.hexdigest() + my_hash.update(chunk) + return my_hash.hexdigest() __gpg: Optional[GPG] = None @@ -89,12 +82,14 @@ def get_gpg() -> GPG: "We checked: '{}'".format(gpg_bin) ) echo_failure( - "- For Linux please install GnuPG using your package manager. In Ubuntu/Debian this can be " + "- For Linux please install GnuPG using your package manager. In " + "Ubuntu/Debian this can be " "achieved with `sudo apt install gnupg`." ) echo_failure("- For Mac OSX please install GnuPG using `brew install gpg`.") echo_failure( - "- For Windows please install GnuPG using the downloads via: https://gnupg.org/download/" + "- For Windows please install GnuPG using the downloads via: " + "https://gnupg.org/download/" ) sys.exit(1) @@ -137,7 +132,8 @@ def tabulate_keys(gpg: GPG, explain: Optional[bool] = False) -> None: If explain = Truem, it will exit with returncode 1 when no keys are present. :param gpg: GPG objects - :param explain: With explain is True, more text is added and will exit(1) when no keys are present. + :param explain: With explain is True, more text is added and will exit(1) when no + keys are present. :return: None. """ result = gpg.list_keys(secret=True) diff --git a/kecpkg/utils.py b/kecpkg/utils.py index b490c6e..f8b8ca2 100644 --- a/kecpkg/utils.py +++ b/kecpkg/utils.py @@ -10,7 +10,6 @@ import platform import re import shutil -import six import sys from contextlib import contextmanager @@ -33,8 +32,9 @@ def create_file(filepath, content=None, overwrite=True): Will overwrite file already in place if overwrite flag is set. - If a list is provided each line in the list is written on a new line in the file (`fp.writelines`) - otherwise the string will be written as such and newline characters (`\\\\n`) will be respected. + If a list is provided each line in the list is written on a new line in the file ( + `fp.writelines`) otherwise the string will be written as such and newline characters ( + `\\\\n`) will be respected. :param filepath: full path to a file to create :param content: textual content. diff --git a/snapcraft.yaml b/snapcraft.yaml deleted file mode 100644 index 749430f..0000000 --- a/snapcraft.yaml +++ /dev/null @@ -1,34 +0,0 @@ -name: kecpkg -version: git -version-script: git describe --tags -summary: A set of tools to easily create and manage KE-chain packages. -description: | - kecpkg provide a set of tools to easily create and manage KE-chain packages. - These are executable python scripts aimed for execution on the KE-chain SIM platform. - A valid user license of KE-chain is needed. See https://ke-chain.com for more information. -license: Apache-2.0 - -grade: devel # must be 'stable' to release into candidate/stable channels -confinement: devmode # use 'strict' once you have the right plugs and slots -base: core18 - -parts: - kecpkg: - plugin: python - python-version: python3 - source: . - stage-packages: - - python-six - - gpg - - gpg-agent -apps: - kecpkg: - command: bin/kecpkg - environment: - LC_ALL: C.UTF-8 - LANG: C.UTF-8 - plugs: - - gpg-keys - - gpg-public-keys - - network-bind - diff --git a/tests/commands/test_build.py b/tests/commands/test_build.py index bd6bbc8..3ac40a2 100644 --- a/tests/commands/test_build.py +++ b/tests/commands/test_build.py @@ -4,9 +4,9 @@ from click.testing import CliRunner from kecpkg.cli import kecpkg -from kecpkg.settings import load_settings, save_settings, copy_default_settings -from kecpkg.utils import get_package_dir, ensure_dir_exists -from tests.utils import temp_chdir, BaseTestCase, touch_file +from kecpkg.settings import copy_default_settings, save_settings +from kecpkg.utils import ensure_dir_exists, get_package_dir +from tests.utils import BaseTestCase, temp_chdir, touch_file class TestCommandPurge(BaseTestCase): diff --git a/tests/commands/test_upload.py b/tests/commands/test_upload.py index ef74e1d..22e51f8 100644 --- a/tests/commands/test_upload.py +++ b/tests/commands/test_upload.py @@ -12,7 +12,8 @@ @skipIf(running_on_ci(), reason="Test do not work on a CI environment") @skipIf( "os.getenv('KECHAIN_URL') is None", - reason="Skipping test as the KECHAIN_URL is not available as environment variable. Cannot upload kecpkg to " + reason="Skipping test as the KECHAIN_URL is not available as environment variable. " + "Cannot upload kecpkg to " "test this functionality. Provice a `.env` file locally to enable these tests.", ) class TestCommandUpload(BaseTestCase): diff --git a/tests/utils.py b/tests/utils.py index 721a7cd..b7ad023 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -3,7 +3,6 @@ from contextlib import contextmanager from unittest import TestCase -import six from click.testing import CliRunner @@ -61,7 +60,7 @@ def connected_to_internet(): # no cov # Test connection socket.create_connection((host, 80), 2) return True - except: + except OSError: return False @@ -69,9 +68,9 @@ def running_on_ci(): # no cov # type: () -> bool """If the system is running on a CI platform.""" if ( - os.environ.get("CI") - or os.environ.get("TRAVIS") - or os.environ.get("GITHUB_ACTIONS") + os.environ.get("CI") + or os.environ.get("TRAVIS") + or os.environ.get("GITHUB_ACTIONS") ): return True return False