Skip to content

Commit

Permalink
Clean up workarounds for older Python versions
Browse files Browse the repository at this point in the history
Now that 3.9 is our lower limit, we can drop some code that was catering
older Python versions.

Signed-off-by: Jan Kiszka <[email protected]>
  • Loading branch information
jan-kiszka committed Feb 11, 2025
1 parent 9b32fc5 commit 8a2296b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 60 deletions.
5 changes: 2 additions & 3 deletions kas/attestation.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,9 @@ def _make_relative_path(self, path: Path):
top_repo_path = Path(self._ctx.config.handler.get_top_repo_path())
workdir = Path(self._ctx.kas_work_dir)

# is_relative_to implementation for python < 3.9
try:
if path.is_relative_to(workdir):
return path.relative_to(workdir)
except ValueError:
else:
return path.relative_to(top_repo_path)

def type_(self):
Expand Down
27 changes: 7 additions & 20 deletions kas/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,20 @@
This module contains the implementation of the kas context.
"""

import distro
import os
import logging
from enum import Enum
from kas.kasusererror import KasUserError

try:
import distro

def get_distro_id_base():
"""
Returns a compatible distro id.
"""
return distro.like() or distro.id()

except ImportError:
import platform

def get_distro_id_base():
"""
Wrapper around platform.dist to simulate distro.id
platform.dist is deprecated and will be removed in python 3.7
Use the 'distro' package instead.
"""
return platform.dist()[0]
__context__ = None


__context__ = None
def get_distro_id_base():
"""
Returns a compatible distro id.
"""
return distro.like() or distro.id()


def create_global_context(args):
Expand Down
19 changes: 3 additions & 16 deletions kas/kas.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ def cleanup_logger():
logging.root.removeHandler(handler)


def get_pending_tasks(loop):
try:
return asyncio.all_tasks(loop)
except AttributeError:
# for Python < 3.7
return asyncio.Task.all_tasks(loop)


def interruption():
"""
Gracefully cancel all tasks in the event loop
Expand All @@ -101,7 +93,7 @@ def interruption():
for sig in [signal.SIGINT, signal.SIGTERM]:
loop.remove_signal_handler(sig)
loop.add_signal_handler(sig, termination)
pending = get_pending_tasks(loop)
pending = asyncio.all_tasks(loop)
if pending:
logging.debug(f'waiting for {len(pending)} tasks to terminate')
[t.cancel() for t in pending]
Expand All @@ -119,13 +111,8 @@ def shutdown_loop(loop):
"""
Waits for completion of the event loop
"""
pending = get_pending_tasks(loop)
# Ignore exceptions in final shutdown (Python3.6 workaround).
# These are related to the cancellation of tasks.
try:
loop.run_until_complete(asyncio.gather(*pending))
except KasUserError:
pass
pending = asyncio.all_tasks(loop)
loop.run_until_complete(asyncio.gather(*pending))
loop.close()


Expand Down
7 changes: 1 addition & 6 deletions kas/libcmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import logging
import shutil
import os
import sys
import pprint
import configparser
import json
Expand Down Expand Up @@ -550,11 +549,7 @@ def execute(self, ctx):
# now fetch everything with complete config
repos_fetch(ctx.config.get_repos())

if sys.version_info < (3, 8):
config_str = pprint.pformat(ctx.config.get_config())
else:
config_str = pprint.pformat(ctx.config.get_config(),
sort_dicts=False)
config_str = pprint.pformat(ctx.config.get_config(), sort_dicts=False)
logging.debug('Configuration from config file:\n%s', config_str)


Expand Down
8 changes: 2 additions & 6 deletions kas/libkas.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ async def run_cmd_async(cmd, cwd, env=None, fail=True, liveupdate=False):

logo = LogOutput(liveupdate)

try:
orig_fd = signal.set_wakeup_fd(-1, warn_on_full_buffer=False)
signal.set_wakeup_fd(orig_fd, warn_on_full_buffer=False)
except TypeError:
# Python < 3.7 - we tried our best
pass
orig_fd = signal.set_wakeup_fd(-1, warn_on_full_buffer=False)
signal.set_wakeup_fd(orig_fd, warn_on_full_buffer=False)

try:
process = await asyncio.create_subprocess_exec(
Expand Down
10 changes: 1 addition & 9 deletions kas/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import re
import os
import sys
import linecache
import logging
import shutil
Expand All @@ -35,11 +34,7 @@
from .context import get_context
from .libkas import run_cmd_async, run_cmd
from .kasusererror import KasUserError

if sys.version_info < (3, 8):
from cached_property import cached_property
else:
from functools import cached_property
from functools import cached_property

__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017-2018'
Expand Down Expand Up @@ -327,9 +322,6 @@ async def fetch_async(self):
logging.debug('Created repo ref for %s', self.qualified_name)
try:
os.rename(tmpdir, sdir)
if sys.version_info < (3, 8):
# recreate dir so cleanup handler can delete it
os.makedirs(tmpdir, exist_ok=True)
except OSError:
logging.debug('repo %s already cloned by other instance',
self.qualified_name)
Expand Down

0 comments on commit 8a2296b

Please sign in to comment.