Skip to content

Commit

Permalink
Use python10
Browse files Browse the repository at this point in the history
  • Loading branch information
samhotep committed Jul 15, 2024
1 parent 0b91b8b commit 6f443f3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions build.sh

This file was deleted.

56 changes: 30 additions & 26 deletions dotrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# Packages
import docker
import dockerpty
import requests
from dotenv import dotenv_values
from slugify import slugify

Expand All @@ -32,22 +31,22 @@ 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
canonicalwebteam/dotrun-image:<release_tag>
"""
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:
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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"]
Expand All @@ -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)

Expand All @@ -287,5 +290,6 @@ def cli():

return status_code


if __name__ == "__main__":
cli()
cli()
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>']
license = 'LGPL-3.0'
Expand All @@ -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'
Expand Down

0 comments on commit 6f443f3

Please sign in to comment.