Skip to content

Commit 8a2296b

Browse files
committed
Clean up workarounds for older Python versions
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]>
1 parent 9b32fc5 commit 8a2296b

File tree

6 files changed

+16
-60
lines changed

6 files changed

+16
-60
lines changed

kas/attestation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ def _make_relative_path(self, path: Path):
108108
top_repo_path = Path(self._ctx.config.handler.get_top_repo_path())
109109
workdir = Path(self._ctx.kas_work_dir)
110110

111-
# is_relative_to implementation for python < 3.9
112-
try:
111+
if path.is_relative_to(workdir):
113112
return path.relative_to(workdir)
114-
except ValueError:
113+
else:
115114
return path.relative_to(top_repo_path)
116115

117116
def type_(self):

kas/context.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,20 @@
2323
This module contains the implementation of the kas context.
2424
"""
2525

26+
import distro
2627
import os
2728
import logging
2829
from enum import Enum
2930
from kas.kasusererror import KasUserError
3031

31-
try:
32-
import distro
33-
34-
def get_distro_id_base():
35-
"""
36-
Returns a compatible distro id.
37-
"""
38-
return distro.like() or distro.id()
39-
40-
except ImportError:
41-
import platform
42-
43-
def get_distro_id_base():
44-
"""
45-
Wrapper around platform.dist to simulate distro.id
46-
platform.dist is deprecated and will be removed in python 3.7
47-
Use the 'distro' package instead.
48-
"""
49-
return platform.dist()[0]
32+
__context__ = None
5033

5134

52-
__context__ = None
35+
def get_distro_id_base():
36+
"""
37+
Returns a compatible distro id.
38+
"""
39+
return distro.like() or distro.id()
5340

5441

5542
def create_global_context(args):

kas/kas.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@ def cleanup_logger():
8585
logging.root.removeHandler(handler)
8686

8787

88-
def get_pending_tasks(loop):
89-
try:
90-
return asyncio.all_tasks(loop)
91-
except AttributeError:
92-
# for Python < 3.7
93-
return asyncio.Task.all_tasks(loop)
94-
95-
9688
def interruption():
9789
"""
9890
Gracefully cancel all tasks in the event loop
@@ -101,7 +93,7 @@ def interruption():
10193
for sig in [signal.SIGINT, signal.SIGTERM]:
10294
loop.remove_signal_handler(sig)
10395
loop.add_signal_handler(sig, termination)
104-
pending = get_pending_tasks(loop)
96+
pending = asyncio.all_tasks(loop)
10597
if pending:
10698
logging.debug(f'waiting for {len(pending)} tasks to terminate')
10799
[t.cancel() for t in pending]
@@ -119,13 +111,8 @@ def shutdown_loop(loop):
119111
"""
120112
Waits for completion of the event loop
121113
"""
122-
pending = get_pending_tasks(loop)
123-
# Ignore exceptions in final shutdown (Python3.6 workaround).
124-
# These are related to the cancellation of tasks.
125-
try:
126-
loop.run_until_complete(asyncio.gather(*pending))
127-
except KasUserError:
128-
pass
114+
pending = asyncio.all_tasks(loop)
115+
loop.run_until_complete(asyncio.gather(*pending))
129116
loop.close()
130117

131118

kas/libcmds.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import logging
2828
import shutil
2929
import os
30-
import sys
3130
import pprint
3231
import configparser
3332
import json
@@ -550,11 +549,7 @@ def execute(self, ctx):
550549
# now fetch everything with complete config
551550
repos_fetch(ctx.config.get_repos())
552551

553-
if sys.version_info < (3, 8):
554-
config_str = pprint.pformat(ctx.config.get_config())
555-
else:
556-
config_str = pprint.pformat(ctx.config.get_config(),
557-
sort_dicts=False)
552+
config_str = pprint.pformat(ctx.config.get_config(), sort_dicts=False)
558553
logging.debug('Configuration from config file:\n%s', config_str)
559554

560555

kas/libkas.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ async def run_cmd_async(cmd, cwd, env=None, fail=True, liveupdate=False):
120120

121121
logo = LogOutput(liveupdate)
122122

123-
try:
124-
orig_fd = signal.set_wakeup_fd(-1, warn_on_full_buffer=False)
125-
signal.set_wakeup_fd(orig_fd, warn_on_full_buffer=False)
126-
except TypeError:
127-
# Python < 3.7 - we tried our best
128-
pass
123+
orig_fd = signal.set_wakeup_fd(-1, warn_on_full_buffer=False)
124+
signal.set_wakeup_fd(orig_fd, warn_on_full_buffer=False)
129125

130126
try:
131127
process = await asyncio.create_subprocess_exec(

kas/repos.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import re
2727
import os
28-
import sys
2928
import linecache
3029
import logging
3130
import shutil
@@ -35,11 +34,7 @@
3534
from .context import get_context
3635
from .libkas import run_cmd_async, run_cmd
3736
from .kasusererror import KasUserError
38-
39-
if sys.version_info < (3, 8):
40-
from cached_property import cached_property
41-
else:
42-
from functools import cached_property
37+
from functools import cached_property
4338

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

0 commit comments

Comments
 (0)