Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
refactor: make platform-filter callable
Browse files Browse the repository at this point in the history
  • Loading branch information
ccwienk committed Mar 20, 2023
1 parent 6c726b3 commit 64ec29e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
4 changes: 2 additions & 2 deletions ctt.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def main():

platform_filter = None
if parsed.included_platforms:
platform_filter = platform.PlatformFilter.create(
included_platforms=parsed.included_platforms,
platform_filter = platform.PlatformFilter(
included_platform_regexes=parsed.included_platforms,
)

if parsed.rbsc_git_url and not parsed.rbsc_git_branch:
Expand Down
39 changes: 27 additions & 12 deletions ctt/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,41 @@
import re
import typing

import deprecated

import oci.model as om


class PlatformFilter:
@staticmethod
def create(
included_platforms: typing.List[str],
) -> typing.Callable[[om.OciPlatform], bool]:
def __init__(
self,
included_platform_regexes: typing.Iterable[str],
):
'''
@param included_platforms: a list of regexes to filter for platforms in format os/arch/variant
@param included_platforms: regexes to filter for platforms in format os/arch/variant
'''
def filter(platform_to_match: om.OciPlatform) -> bool:
normalised_platform = normalise(platform_to_match)
for regex_platform in included_platforms:
if re.fullmatch(regex_platform, normalised_platform):
return True
self.included_platform_regexes = tuple(included_platform_regexes)

return False
def __call__(self, platform: om.OciPlatform) -> bool:
'''
returns True if the passed platform matches this filter (i.e. should be inclued), else False
'''
normalised_platform = normalise(platform)
for platform_regex in self.included_platform_regexes:
if re.fullmatch(platform_regex, normalised_platform):
return True

return filter
return False

def __repr__(self):
return f'{self.__class__.__name__}({self.included_platform_regexes=})'

@deprecated.deprecated
@staticmethod
def create(
included_platforms: typing.List[str],
) -> typing.Callable[[om.OciPlatform], bool]:
return PlatformFilter(included_platform_regexes=included_platforms)


def normalise(p: om.OciPlatform):
Expand Down
24 changes: 12 additions & 12 deletions ctt/test/platform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@


def test_platform_filter():
filter_func = platform.PlatformFilter.create(
included_platforms=[
filter_func = platform.PlatformFilter(
included_platform_regexes=(
'linux/.*',
],
),
)

matches = filter_func(om.OciPlatform(
Expand All @@ -44,10 +44,10 @@ def test_platform_filter():
))
assert matches is False

filter_func = platform.PlatformFilter.create(
included_platforms=[
filter_func = platform.PlatformFilter(
included_platform_regexes=(
'linux/arm64',
],
),
)

matches = filter_func(om.OciPlatform(
Expand All @@ -69,10 +69,10 @@ def test_platform_filter():
))
assert matches is False

filter_func = platform.PlatformFilter.create(
included_platforms=[
filter_func = platform.PlatformFilter(
included_platform_regexes=(
'linux/arm64/v7',
],
),
)

matches = filter_func(om.OciPlatform(
Expand All @@ -89,10 +89,10 @@ def test_platform_filter():
))
assert matches is False

filter_func = platform.PlatformFilter.create(
included_platforms=[
filter_func = platform.PlatformFilter(
included_platform_regexes=(
'.*/arm64/.*',
],
),
)

matches = filter_func(om.OciPlatform(
Expand Down

0 comments on commit 64ec29e

Please sign in to comment.