From f894d2180c4e9d3097c66d78444c5495c8f26ccb Mon Sep 17 00:00:00 2001 From: AttilaGombosER Date: Fri, 6 Sep 2024 08:56:31 +0200 Subject: [PATCH] Refactor using python-common-utility (#7) --- .devcontainer | 1 + .github/workflows/python-release.yml | 30 ------------ .github/workflows/python-test.yml | 29 ----------- .github/workflows/test_and_release.yml | 66 ++++++++++++++++++++++++++ .gitmodules | 3 ++ README.md | 12 +++-- bin/debian-package-installer.py | 9 ++-- package_installer/packageInstaller.py | 15 ++++-- package_installer/sourceAdder.py | 12 +++-- setup.cfg | 5 ++ setup.py | 19 +++++--- tests/__init__.py | 0 12 files changed, 121 insertions(+), 80 deletions(-) create mode 160000 .devcontainer delete mode 100644 .github/workflows/python-release.yml delete mode 100644 .github/workflows/python-test.yml create mode 100644 .github/workflows/test_and_release.yml create mode 100644 .gitmodules delete mode 100644 tests/__init__.py diff --git a/.devcontainer b/.devcontainer new file mode 160000 index 0000000..190f80b --- /dev/null +++ b/.devcontainer @@ -0,0 +1 @@ +Subproject commit 190f80babcba5a15b7ea3a0129c2f72cbec2ca0f diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml deleted file mode 100644 index 929f0f9..0000000 --- a/.github/workflows/python-release.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Python release - -on: - push: - tags: - - "v*.*.*" - -jobs: - publish-and-release: - name: Publish and release distributions - - runs-on: ubuntu-latest - - permissions: - contents: write - discussions: write - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install -y python-apt-dev python3-apt - - name: Package and publish - uses: EffectiveRange/python-package-github-action@v1 - with: - debian-dist-type: 'library' - - name: Release - uses: EffectiveRange/version-release-github-action@v1 diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml deleted file mode 100644 index 7b3ad10..0000000 --- a/.github/workflows/python-test.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Python test - -on: - pull_request: - branches: [ "main" ] - -jobs: - build: - name: Build and test - - runs-on: ubuntu-latest - - permissions: - # Gives the action the necessary permissions for publishing new - # comments in pull requests. - pull-requests: write - contents: write - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install -y python-apt-dev python3-apt - - name: Verify changes - uses: EffectiveRange/python-verify-github-action@v1 - with: - coverage-threshold: '95' diff --git a/.github/workflows/test_and_release.yml b/.github/workflows/test_and_release.yml new file mode 100644 index 0000000..563aa2a --- /dev/null +++ b/.github/workflows/test_and_release.yml @@ -0,0 +1,66 @@ +name: Test and Release + +on: + push: + branches: main + tags: v*.*.* + + pull_request: + branches: [ "main" ] + types: + - synchronize + - opened + - reopened + +concurrency: + group: ${{ github.workflow }}-${{ github.sha }} + cancel-in-progress: true + +jobs: + test: + name: Build and test + + runs-on: ubuntu-latest + + permissions: + # Gives the action the necessary permissions for publishing new + # comments in pull requests. + pull-requests: write + contents: write + statuses: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y python-apt-dev python3-apt + - name: Verify changes + uses: EffectiveRange/python-verify-github-action@v1 + with: + coverage-threshold: '95' + + release: + if: startsWith(github.ref, 'refs/tags/') + needs: test + + name: Publish and release + + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + submodules: true + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y python-apt-dev python3-apt + - name: Package and publish + uses: EffectiveRange/python-package-github-action@v2 + with: + debian-dist-type: 'fpm-deb' + - name: Release + uses: EffectiveRange/version-release-github-action@v1 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f1b0753 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule ".devcontainer"] + path = .devcontainer + url = https://github.com/EffectiveRange/devcontainer-defs diff --git a/README.md b/README.md index 0b98b6f..574593c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ + +[![Test and Release](https://github.com/EffectiveRange/debian-package-installer/actions/workflows/test_and_release.yml/badge.svg)](https://github.com/EffectiveRange/debian-package-installer/actions/workflows/test_and_release.yml) +[![Coverage badge](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/EffectiveRange/debian-package-installer/python-coverage-comment-action-data/endpoint.json)](https://htmlpreview.github.io/?https://github.com/EffectiveRange/debian-package-installer/blob/python-coverage-comment-action-data/htmlcov/index.html) + # debian-package-installer Debian package installer that supports installing from APT repository, .deb file URL and .deb GitHub release asset @@ -59,7 +63,7 @@ pip install . ### Command line reference -```commandline +```bash $ bin/debian-package-installer.py --help usage: debian-package-installer.py [-h] [-f LOG_FILE] [-l LOG_LEVEL] [-s SOURCE_CONFIG] [-d DOWNLOAD] package_config @@ -80,7 +84,7 @@ options: ### Example -```commandline +```bash $ bin/debian-package-installer.py ~/config/package-config.json ``` @@ -115,7 +119,7 @@ Example configuration (example `package-config.json` config file content): Needs root privileges to add APT keys: -```commandline +```bash $ sudo bin/debian-package-installer.py ~/config/package-config.json -s ~/config/source-config.json ``` @@ -146,7 +150,7 @@ Example source configuration (example `source-config.json` config file content): Output: -```commandline +```bash 2024-07-04T07:16:37.793684Z [info ] Starting package installer [PackageInstallerApp] app_version=1.0.0 application=debian-package-installer arguments={'log_file': None, 'log_level': 'info', 'source_config': 'build/source-config.json', 'download': '/tmp/packages', 'package_config': 'build/package-config.json'} hostname=Legion7iPro 2024-07-04T07:16:37.794110Z [info ] Local file path provided, skipping download [FileDownloader] app_version=1.0.0 application=debian-package-installer file=/home/attilagombos/EffectiveRange/debian-package-installer/build/source-config.json hostname=Legion7iPro 2024-07-04T07:16:37.906907Z [info ] Local file path provided, skipping download [FileDownloader] app_version=1.0.0 application=debian-package-installer file=/home/attilagombos/EffectiveRange/debian-package-installer/build/package-config.json hostname=Legion7iPro diff --git a/bin/debian-package-installer.py b/bin/debian-package-installer.py index aff0b8d..4a967aa 100644 --- a/bin/debian-package-installer.py +++ b/bin/debian-package-installer.py @@ -9,9 +9,9 @@ from apt.cache import Cache from aptsources.sourceslist import SourcesList +from common_utility import JsonLoader, SessionProvider, FileDownloader from context_logger import get_logger, setup_logging -from package_downloader import FileDownloader, DebDownloader, AssetDownloader, RepositoryProvider, \ - SessionProvider, JsonLoader +from package_downloader import DebDownloader, AssetDownloader, RepositoryProvider from package_installer import PackageInstaller, DebInstaller, AptInstaller, DebProvider, SourceAdder, KeyAdder @@ -48,8 +48,9 @@ def main() -> None: package_config_path = file_downloader.download(arguments.package_config, skip_if_exists=False) - package_installer = PackageInstaller(package_config_path, json_loader, - apt_cache, apt_installer, deb_installer, source_adder) + package_installer = PackageInstaller( + package_config_path, json_loader, apt_cache, apt_installer, deb_installer, source_adder + ) package_installer.install_packages() diff --git a/package_installer/packageInstaller.py b/package_installer/packageInstaller.py index bb7ed17..4e2dab1 100644 --- a/package_installer/packageInstaller.py +++ b/package_installer/packageInstaller.py @@ -1,11 +1,13 @@ # SPDX-FileCopyrightText: 2024 Ferenc Nandor Janky # SPDX-FileCopyrightText: 2024 Attila Gombos # SPDX-License-Identifier: MIT + from typing import Optional from apt import Cache +from common_utility import IJsonLoader from context_logger import get_logger -from package_downloader import IJsonLoader, PackageConfig +from package_downloader import PackageConfig from package_installer import IAptInstaller, IDebInstaller, ISourceAdder @@ -14,8 +16,15 @@ class PackageInstaller(object): - def __init__(self, config_path: str, json_loader: IJsonLoader, apt_cache: Cache, apt_installer: IAptInstaller, - deb_installer: IDebInstaller, source_adder: Optional[ISourceAdder] = None) -> None: + def __init__( + self, + config_path: str, + json_loader: IJsonLoader, + apt_cache: Cache, + apt_installer: IAptInstaller, + deb_installer: IDebInstaller, + source_adder: Optional[ISourceAdder] = None, + ) -> None: self._config_path = config_path self._json_loader = json_loader self._apt_cache = apt_cache diff --git a/package_installer/sourceAdder.py b/package_installer/sourceAdder.py index a75cf60..a5c32c9 100644 --- a/package_installer/sourceAdder.py +++ b/package_installer/sourceAdder.py @@ -5,8 +5,8 @@ from urllib.parse import urlparse from aptsources.sourceslist import SourcesList, SourceEntry +from common_utility import IJsonLoader, IFileDownloader from context_logger import get_logger -from package_downloader import IFileDownloader, IJsonLoader from package_installer import SourceConfig, IKeyAdder @@ -21,8 +21,14 @@ def add_sources(self) -> None: class SourceAdder(ISourceAdder): - def __init__(self, config_path: str, json_loader: IJsonLoader, sources_list: SourcesList, key_adder: IKeyAdder, - file_downloader: IFileDownloader) -> None: + def __init__( + self, + config_path: str, + json_loader: IJsonLoader, + sources_list: SourcesList, + key_adder: IKeyAdder, + file_downloader: IFileDownloader, + ) -> None: self._config_path = config_path self._json_loader = json_loader self._sources_list = sources_list diff --git a/setup.cfg b/setup.cfg index 6b3e01e..676fd07 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,8 @@ +[pack-python] +packaging = + wheel + fpm-deb + [mypy] packages = bin,package_installer strict = True diff --git a/setup.py b/setup.py index 39b31eb..21ab4c9 100644 --- a/setup.py +++ b/setup.py @@ -6,8 +6,9 @@ def get_python_apt_version() -> str: - proc = subprocess.Popen(['dpkg-query', '-W', '-f', '${Version}', 'python3-apt'], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen( + ['dpkg-query', '-W', '-f', '${Version}', 'python3-apt'], stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) out, _ = proc.communicate() @@ -20,7 +21,10 @@ def get_python_apt_version() -> str: version = 'a1ecf380cb6688a239c30f2786424f864e9b32af' return version - logging.error('Failed to get python-apt version. Please install python3-apt debian package.') + logging.error( + 'Failed to get python-apt version. Please install python3-apt and python-apt-dev debian packages:\n' + '"sudo apt install -y python3-apt python-apt-dev"' + ) exit(1) @@ -39,8 +43,9 @@ def clean_version(version: str) -> str: packages=['package_installer'], scripts=['bin/debian-package-installer.py'], package_data={'package_installer': ['py.typed']}, - install_requires=[f'python-apt@git+https://salsa.debian.org/apt-team/python-apt@{get_python_apt_version()}', - 'python-context-logger@git+https://github.com/EffectiveRange/python-context-logger.git@latest', - 'debian-package-downloader' - '@git+https://github.com/EffectiveRange/debian-package-downloader.git@latest'] + install_requires=[ + f'python-apt@git+https://salsa.debian.org/apt-team/python-apt@{get_python_apt_version()}', + 'python-context-logger@git+https://github.com/EffectiveRange/python-context-logger.git@latest', + 'debian-package-downloader@git+https://github.com/EffectiveRange/debian-package-downloader.git@latest', + ], ) diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000