Merge pull request #887 from baszoetekouw/fix-oldpython

Fix support for older python versions
This commit is contained in:
Povilas Kanapickas 2024-03-09 23:29:32 +02:00 committed by GitHub
commit f18c8092cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 69 additions and 16 deletions

View File

@ -1,4 +1,4 @@
[codespell]
skip = .git,*.pdf,*.svg
skip = .git,*.pdf,*.svg,requirements.txt,test-requirements.txt
# poped - loved variable name
ignore-words-list = poped

19
.editorconfig Normal file
View File

@ -0,0 +1,19 @@
root = true
[*]
indent_style = space
indent_size = tab
tab_width = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
[*.{yml,yaml}]
indent_style = space
indent_size = 2
[*.py]
indent_style = space

View File

@ -20,3 +20,6 @@ jobs:
pip install -r test-requirements.txt
ruff format --check
ruff check
- name: Analysing the code with pylint
run: |
pylint podman_compose.py

View File

@ -6,9 +6,14 @@ on:
jobs:
test:
strategy:
fail-fast: false
matrix:
python-version: [ '3.8', '3.9', '3.10', '3.11', '3.12' ]
runs-on: ubuntu-latest
container:
image: docker.io/library/python:3.11-bookworm
image: "docker.io/library/python:${{ matrix.python-version }}-bookworm"
# cgroupns needed to address the following error:
# write /sys/fs/cgroup/cgroup.subtree_control: operation not supported
options: --privileged --cgroupns=host
@ -21,11 +26,15 @@ jobs:
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f test-requirements.txt ]; then pip install -r test-requirements.txt; fi
- name: Test with unittest
- name: Run tests in tests/
run: |
coverage run --source podman_compose -m unittest pytests/*.py
python -m unittest tests/*.py
coverage combine
coverage report
env:
TESTS_DEBUG: 1
- name: Run tests in pytests/
run: |
coverage run --source podman_compose -m unittest pytests/*.py
- name: Report coverage
run: |
coverage combine
coverage report

View File

@ -1173,15 +1173,15 @@ class Podman:
xargs = self.compose.get_podman_args(cmd) if cmd else []
cmd_ls = [self.podman_path, *podman_args, cmd] + xargs + cmd_args
log.info(str(cmd_ls))
p = await asyncio.subprocess.create_subprocess_exec(
p = await asyncio.create_subprocess_exec(
*cmd_ls, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout_data, stderr_data = await p.communicate()
if p.returncode == 0:
return stdout_data
else:
raise subprocess.CalledProcessError(p.returncode, " ".join(cmd_ls), stderr_data)
raise subprocess.CalledProcessError(p.returncode, " ".join(cmd_ls), stderr_data)
def exec(
self,
@ -1195,7 +1195,7 @@ class Podman:
log.info(" ".join([str(i) for i in cmd_ls]))
os.execlp(self.podman_path, *cmd_ls)
async def run(
async def run( # pylint: disable=dangerous-default-value
self,
podman_args,
cmd="",
@ -1223,7 +1223,7 @@ class Podman:
if stdout.at_eof():
break
p = await asyncio.subprocess.create_subprocess_exec(
p = await asyncio.create_subprocess_exec(
*cmd_ls, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
) # pylint: disable=consider-using-with
@ -1238,7 +1238,7 @@ class Podman:
err_t.add_done_callback(task_reference.discard)
else:
p = await asyncio.subprocess.create_subprocess_exec(*cmd_ls) # pylint: disable=consider-using-with
p = await asyncio.create_subprocess_exec(*cmd_ls) # pylint: disable=consider-using-with
try:
exit_code = await p.wait()
@ -1916,9 +1916,12 @@ class PodmanCompose:
podman_compose = PodmanCompose()
###################
# decorators to add commands and parse options
###################
class PodmanComposeError(Exception):
pass
class cmd_run: # pylint: disable=invalid-name,too-few-public-methods
@ -1932,7 +1935,7 @@ class cmd_run: # pylint: disable=invalid-name,too-few-public-methods
return func(*args, **kw)
if not asyncio.iscoroutinefunction(func):
raise Exception("Command must be async")
raise PodmanComposeError("Command must be async")
wrapped._compose = self.compose
# Trim extra indentation at start of multiline docstrings.
wrapped.desc = self.cmd_desc or re.sub(r"^\s+", "", func.__doc__)
@ -2014,8 +2017,8 @@ async def compose_systemd(compose, args):
f.write(f"{k}={v}\n")
log.debug("writing [%s]: done.", fn)
log.info("\n\ncreating the pod without starting it: ...\n\n")
process = await asyncio.subprocess.create_subprocess_exec(script, ["up", "--no-start"])
log.info("\nfinal exit code is ", process)
process = await asyncio.create_subprocess_exec(script, ["up", "--no-start"])
log.info("\nfinal exit code is %d", process)
username = getpass.getuser()
print(
f"""
@ -2299,6 +2302,14 @@ async def compose_up(compose: PodmanCompose, args):
)
)
def _task_cancelled(task: Task) -> bool:
if task.cancelled():
return True
# Task.cancelling() is new in python 3.11
if sys.version_info >= (3, 11) and task.cancelling():
return True
return False
exit_code = 0
exiting = False
while tasks:
@ -2309,7 +2320,9 @@ async def compose_up(compose: PodmanCompose, args):
# cause the status to overwrite. Sleeping for 1 seems to fix this and make it match
# docker-compose
await asyncio.sleep(1)
[_.cancel() for _ in tasks if not _.cancelling() and not _.cancelled()]
for t in tasks:
if not _task_cancelled(t):
t.cancel()
t: Task
exiting = True
for t in done:

View File

@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
from __future__ import annotations
import argparse
import os

View File

@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
from __future__ import annotations
import argparse
import copy

View File

@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
# pylint: disable=protected-access
from __future__ import annotations
import argparse
import os

View File

@ -4,6 +4,7 @@ parameterized==0.9.0
pytest==8.0.2
tox==4.13.0
ruff==0.3.1
pylint==3.1.0
# The packages below are transitive dependencies of the packages above and are included here
# to make testing reproducible.
@ -12,16 +13,21 @@ ruff==0.3.1
# pip freeze > test-requirements.txt
# and edit test-requirements.txt to add this comment
astroid==3.1.0
cachetools==5.3.3
chardet==5.2.0
colorama==0.4.6
dill==0.3.8
distlib==0.3.8
filelock==3.13.1
iniconfig==2.0.0
isort==5.13.2
mccabe==0.7.0
packaging==23.2
platformdirs==4.2.0
pluggy==1.4.0
pyproject-api==1.6.1
python-dotenv==1.0.1
PyYAML==6.0.1
tomlkit==0.12.4
virtualenv==20.25.1