From 6f443f35c361cd5f64d5ac586cd4c6410072df88 Mon Sep 17 00:00:00 2001 From: Olwe Samuel Date: Tue, 16 Jul 2024 01:15:38 +0300 Subject: [PATCH] Use python10 --- .github/workflows/pr.yaml | 3 ++- README.md | 13 ++++----- build.sh | 4 --- dotrun.py | 56 +++++++++++++++++++++------------------ poetry.lock | 4 +-- pyproject.toml | 4 +-- 6 files changed, 43 insertions(+), 41 deletions(-) delete mode 100755 build.sh diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 50ffe19..31c0db6 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -10,7 +10,8 @@ jobs: uses: actions/checkout@v2 - name: Install dotrun - run: pip3 install . + run: | + pip3 install . requests==2.31.0 - name: Install newest version of curl for --retry-all-errors support run: sudo snap install curl diff --git a/README.md b/README.md index 3618723..2fc58e3 100644 --- a/README.md +++ b/README.md @@ -111,16 +111,17 @@ docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 -- ## Hacking -To quickly build and test changes to the package, first install poetry: +You can install the package locally using either pip or poetry. + +### Using pip ```bash -pip install poetry -poetry install -chmod +x build.sh +pip3 install . requests==2.31.0 ``` -Then you can build and install .whl packages using: +### Using Poetry ```bash -./build.sh +pip install poetry +poetry install --no-interaction ``` To run dotrun off alternative base images such as local images, you can use the `--image` flag. diff --git a/build.sh b/build.sh deleted file mode 100755 index a64895c..0000000 --- a/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -poetry build -pip uninstall -y dotrun -pip install --no-index --find-links dist/ dotrun \ No newline at end of file diff --git a/dotrun.py b/dotrun.py index 33b754d..9f06a07 100644 --- a/dotrun.py +++ b/dotrun.py @@ -11,7 +11,6 @@ # Packages import docker import dockerpty -import requests from dotenv import dotenv_values from slugify import slugify @@ -32,11 +31,11 @@ def __init__(self): sys.platform.startswith("linux") and "microsoft" not in platform.platform() ) - + self._get_docker_client() self._check_image_updates() self._create_cache_volume() - + def _get_release_image_name(self, image_tag="latest"): """ Return the image name with the tag in the format @@ -44,10 +43,10 @@ def _get_release_image_name(self, image_tag="latest"): """ base_image = self.base_image_name.split(":")[0] return f"{base_image}:{image_tag}" - + def _get_image_name(self, image_name): """ - Return a fully qualified image name from a given image + Return a fully qualified image name from a given image name, defaulting to the :latest tag if none is provided. """ if ":" not in image_name: @@ -68,9 +67,7 @@ def _get_docker_client(self): def _check_image_updates(self): try: - self.docker_client.images.get( - self.base_image_name - ) + self.docker_client.images.get(self.base_image_name) # Pull the image in the background print("Checking for dotrun image updates...") threading.Thread(target=self._pull_image) @@ -82,12 +79,10 @@ def _pull_image(self, image_name=None, no_exit=False): """Pull the dotrun image (if updated) from Docker Hub""" if not image_name: image_name = self.base_image_name - image_uri = self._get_image_name(image_name) + image_uri = self._get_image_name(image_name) repository, tag = image_uri.split(":") try: - self.docker_client.images.pull( - repository=repository, tag=tag - ) + self.docker_client.images.pull(repository=repository, tag=tag) except (docker.errors.APIError, docker.errors.ImageNotFound) as e: print(f"Unable to download image: {image_name}") # Optionally quit if image download fails @@ -208,7 +203,7 @@ def create_container(self, command, image_name=None): # network_mode host is incompatible with ports option ports = None network_mode = "host" - + return self.docker_client.containers.create( image=image_name, name=name, @@ -223,15 +218,18 @@ def create_container(self, command, image_name=None): network_mode=network_mode, ) -def _start_container_with_image(dotrun, command_list, command_match, format="tag"): + +def _start_container_with_image( + dotrun, command_list, command_match, format="tag" +): """ - Utility function to start dotrun using a specified + Utility function to start dotrun using a specified image. """ # Extract the argument from the cli arg image_command = command_match.group(0) try: - image_data = image_command.split(' ')[1] + image_data = image_command.split(" ")[1] except IndexError: print("Image name not supplied.") sys.exit(1) @@ -240,19 +238,22 @@ def _start_container_with_image(dotrun, command_list, command_match, format="tag if format == "release": image_uri = dotrun._get_release_image_name(image_data) else: - image_uri = dotrun._get_image_name(image_data) + image_uri = dotrun._get_image_name(image_data) print(f"Using image: {image_uri}") # Download the image dotrun._pull_image(image_uri, no_exit=True) - + # Remove the image command from command list - new_command_list = ' '.join(command_list).replace(image_command, '').replace(' ', ' ') - command_list = new_command_list.split(' ') + new_command_list = ( + " ".join(command_list).replace(image_command, "").replace(" ", " ") + ) + command_list = new_command_list.split(" ") # Start dotrun from the supplied base image return dotrun.create_container(command_list, image_name=image_uri) + def cli(): dotrun = Dotrun() command = ["dotrun"] @@ -261,17 +262,19 @@ def cli(): if command[-1] == "version": print(f"dotrun v{__version__}") sys.exit(1) - + if command[-1] == "refresh": dotrun._pull_image() print("Latest image pulled successfully.") sys.exit(1) - + # Options for starting the container on different base images - if match := re.search(r'--image [^\s]+', ' '.join(command)): + if match := re.search(r"--image [^\s]+", " ".join(command)): container = _start_container_with_image(dotrun, command, match) - elif match := re.search(r'--release [^\s]+', ' '.join(command)): - container = _start_container_with_image(dotrun, command, match, format="release") + elif match := re.search(r"--release [^\s]+", " ".join(command)): + container = _start_container_with_image( + dotrun, command, match, format="release" + ) else: container = dotrun.create_container(command) @@ -287,5 +290,6 @@ def cli(): return status_code + if __name__ == "__main__": - cli() \ No newline at end of file + cli() diff --git a/poetry.lock b/poetry.lock index 493f73f..6cd71cd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -212,5 +212,5 @@ test = ["websockets"] [metadata] lock-version = "2.0" -python-versions = "^3.12" -content-hash = "4d8c629384c0dcfa188d54c3bbf9c5bba7f8d82deb672fc661a99d94ccfaaf1d" +python-versions = "^3.10" +content-hash = "34169362595085367730a23528a5c5c2f7da09c7f6ab9e9ea421f5d44d540b65" diff --git a/pyproject.toml b/pyproject.toml index 27d0e5e..ac70ced 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = 'dotrun' -version = '2.2.1' +version = '2.3.0' description = 'A tool for developing Node.js and Python projects' authors = ['Canonical Web Team '] license = 'LGPL-3.0' @@ -10,7 +10,7 @@ readme = 'README.md' dotrun = "dotrun:cli" [tool.poetry.dependencies] -python = '^3.12' +python = '^3.10' python-dotenv = '0.20.0' python-slugify = '6.1.2' docker = '6.1.3'