Skip to content

Commit

Permalink
Merge pull request #887 from baszoetekouw/fix-oldpython
Browse files Browse the repository at this point in the history
Fix support for older python versions
  • Loading branch information
p12tic committed Mar 9, 2024
2 parents 43059dc + da63048 commit f18c809
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .codespellrc
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions .github/workflows/static-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 14 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
33 changes: 23 additions & 10 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,15 +1173,15 @@ async def output(self, podman_args, cmd="", cmd_args=None):
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,
Expand All @@ -1195,7 +1195,7 @@ def exec(
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="",
Expand Down Expand Up @@ -1223,7 +1223,7 @@ async def format_out(stdout):
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

Expand All @@ -1238,7 +1238,7 @@ async def format_out(stdout):
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()
Expand Down Expand Up @@ -1916,9 +1916,12 @@ def _init_global_parser(parser):

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
Expand All @@ -1932,7 +1935,7 @@ def wrapped(*args, **kw):
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__)
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pytests/test_can_merge_build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
from __future__ import annotations

import argparse
import os
Expand Down
1 change: 1 addition & 0 deletions pytests/test_can_merge_cmd_ent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
from __future__ import annotations

import argparse
import copy
Expand Down
1 change: 1 addition & 0 deletions pytests/test_normalize_final_build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
# pylint: disable=protected-access
from __future__ import annotations

import argparse
import os
Expand Down
6 changes: 6 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

0 comments on commit f18c809

Please sign in to comment.