Skip to content

Commit

Permalink
Support Python 3.7 (#1)
Browse files Browse the repository at this point in the history
* Support Python 3.7

* Update test.yml
  • Loading branch information
ofek authored Jan 12, 2022
1 parent b9df4ad commit 087a501
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ['3.8', '3.9', '3.10']
python-version: ['3.7', '3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v2
Expand All @@ -35,7 +35,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install Hatch
run: pip install --upgrade hatch
run: pip install --upgrade --pre hatch

- if: matrix.python-version == '3.9' && runner.os == 'Linux'
name: Lint
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

***Added:***

- Support Python 3.7
- Bump the minimum supported version of Hatch

## 0.2 - 2022-01-02

***Added:***
Expand Down
93 changes: 57 additions & 36 deletions hatch_containers/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
import sys
from contextlib import contextmanager
from functools import cached_property

from hatch.env.plugin.interface import EnvironmentInterface
from hatch.utils.fs import Path, temp_directory
Expand All @@ -21,6 +20,12 @@ class ContainerEnvironment(EnvironmentInterface):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.__config_image = None
self.__config_command = None
self.__config_start_on_creation = None
self.__config_shell = None
self.__python_version = None

self.base_image = self.config_image.format(version=self.python_version)
self.base_image_id = re.sub(r'[^\w.-]', '_', self.base_image)
self.image = f'{self.base_image.replace(":", "_")}:hatch-container'
Expand All @@ -33,56 +38,72 @@ def __init__(self, *args, **kwargs):
def get_option_types():
return {'image': str, 'command': list, 'start-on-creation': bool, 'shell': str}

@cached_property
@property
def config_image(self):
image = self.config.get('image', '')
if not isinstance(image, str):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.image` must be a string')
if self.__config_image is None:
image = self.config.get('image', 'python:{version}')
if not isinstance(image, str):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.image` must be a string')

self.__config_image = image

return image or 'python:{version}'
return self.__config_image

@cached_property
@property
def config_command(self):
command = self.config.get('command', [])
if not isinstance(command, list):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.command` must be an array')
if self.__config_command is None:
command = self.config.get('command', ['/bin/sleep', 'infinity'])
if not isinstance(command, list):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.command` must be an array')

for i, arg in enumerate(command, 1):
if not isinstance(arg, str):
raise TypeError(f'Argument #{i} of field `tool.hatch.envs.{self.name}.command` must be a string')

for i, arg in enumerate(command, 1):
if not isinstance(arg, str):
raise TypeError(f'Argument #{i} of field `tool.hatch.envs.{self.name}.command` must be a string')
self.__config_command = command

return command or ['/bin/sleep', 'infinity']
return self.__config_command

@cached_property
@property
def config_start_on_creation(self):
start_on_creation = self.config.get('start-on-creation', False)
if not isinstance(start_on_creation, bool):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.start-on-creation` must be a boolean')
if self.__config_start_on_creation is None:
start_on_creation = self.config.get('start-on-creation', False)
if not isinstance(start_on_creation, bool):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.start-on-creation` must be a boolean')

return start_on_creation
self.__config_start_on_creation = start_on_creation

@cached_property
return self.__config_start_on_creation

@property
def config_shell(self):
shell = self.config.get('shell', '')
if not isinstance(shell, str):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.shell` must be a string')

if shell:
return shell
elif 'alpine' in self.base_image:
return '/bin/ash'
else:
return '/bin/bash'
if self.__config_shell is None:
shell = self.config.get('shell', '')
if not isinstance(shell, str):
raise TypeError(f'Field `tool.hatch.envs.{self.name}.shell` must be a string')

@cached_property
if not shell:
if 'alpine' in self.base_image:
shell = '/bin/ash'
else:
shell = '/bin/bash'

self.__config_shell = shell

return self.__config_shell

@property
def python_version(self):
if python_version := self.config.get('python', ''):
if python_version.isdigit() and len(python_version) > 1:
if self.__python_version is None:
python_version = self.config.get('python', '')
if not python_version:
python_version = '.'.join(map(str, sys.version_info[:2]))
elif python_version.isdigit() and len(python_version) > 1:
python_version = f'{python_version[0]}.{python_version[1:]}'

return python_version
else:
return '.'.join(map(str, sys.version_info[:2]))
self.__python_version = python_version

return self.__python_version

def _activate(self):
self.platform.check_command_output(['docker', 'start', self.container_name])
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "hatchling.build"
name = "hatch-containers"
description = "Hatch plugin for Docker containers"
readme = "README.md"
requires-python = ">=3.8"
requires-python = ">=3.7"
license = "MIT"
keywords = [
"container",
Expand All @@ -22,14 +22,15 @@ classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"hatch>=1rc5",
"hatch>=1.0.0rc8",
]
dynamic = ["version"]

Expand All @@ -53,7 +54,7 @@ packages = ["hatch_containers"]
include = '\.pyi?$'
line-length = 120
skip-string-normalization = true
target-version = ["py38"]
target-version = ["py37"]

[tool.isort]
default_section = "THIRDPARTY"
Expand Down

0 comments on commit 087a501

Please sign in to comment.