Skip to content

Commit

Permalink
cleanup: rm dead code
Browse files Browse the repository at this point in the history
also, replace last remaining usage of FluentIterable w/ Generator.
  • Loading branch information
ccwienk committed Jan 7, 2025
1 parent 6924b72 commit 6d1cfd9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 118 deletions.
82 changes: 0 additions & 82 deletions ci/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import logging
import os
import pathlib
import shutil
import string
import sys
import typing
Expand Down Expand Up @@ -166,10 +165,6 @@ def _quiet():
return ctx().args and ctx().args.quiet


def _verbose():
return ctx().args and ctx().args.verbose


def _print(msg, colour, outfh=sys.stdout):
if not msg:
return
Expand Down Expand Up @@ -212,14 +207,6 @@ def warning(msg:str):
_print('WARNING: ' + str(msg), colour='yellow', outfh=sys.stderr)


@deprecated.deprecated
def verbose(msg:str):
if not _verbose():
return
if msg:
_print('VERBOSE: ' + msg, colour=None, outfh=sys.stdout)


@deprecated.deprecated
def success(msg:str):
if msg:
Expand Down Expand Up @@ -427,21 +414,6 @@ def urlparse(url: str) -> urllib.parse.ParseResult:
return urllib.parse.urlparse(url)


def file_extension_join(path: str, extension: str) -> str:
return '.'.join((path, extension))


def which(cmd_name: str) -> str:
'''
wrapper around shutil.which that calls ci.util.fail if the requested executable is not
found in the PATH.
'''
cmd_path = shutil.which(cmd_name)
if not cmd_path:
fail("{cmd} not found in PATH".format(cmd=cmd_name))
return cmd_path


def merge_dicts(base: dict, *other: dict, list_semantics='merge'):
'''
merges copies of the given dict instances and returns the merge result.
Expand Down Expand Up @@ -484,47 +456,6 @@ def merge_dicts(base: dict, *other: dict, list_semantics='merge'):
)


class FluentIterable:
''' a fluent object stream processing chain builder inspired by guava's FluentIterable
Example:
result = FluentIterable(items=(1,2,3))
.filter(lambda e: e < 2)
.map(lambda e: e * 2)
.as_generator()
'''

def __init__(self, items):
def starter():
yield from items
self.ops = [starter]

def filter(self, filter_func):
last_op = self.ops[-1]

def f():
yield from filter(filter_func, last_op())

self.ops.append(f)
return self

def map(self, map_func):
last_op = self.ops[-1]

def m():
yield from map(map_func, last_op())

self.ops.append(m)
return self

def as_generator(self):
return self.ops[-1]()

def as_list(self):
return list(self.as_generator())


def dict_factory_enum_serialisiation(data):

def convert_value(obj):
Expand All @@ -549,16 +480,3 @@ def convert_value(obj):
return obj

return dict((k, convert_value(v)) for k, v in data)


class MultilineYamlDumper(yaml.SafeDumper):
def represent_data(self, data):
# by default, the SafeDumper includes an extra empty line for each line in the data for
# string-blocks. As all provided ways to configure the dumper differently affect all
# rendered types we create our own Dumper.
if isinstance(data, str) and '\n' in data:
return self.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')
# Also, don't include keys with None/null values.
if data is None:
return self.represent_scalar('tag:yaml.org,2002:null', '')
return super().represent_data(data)
6 changes: 4 additions & 2 deletions concourse/steps/update_component_deps.mako
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ git_helper.rebase(
commit_ish=REPO_BRANCH,
)
upgrade_pull_requests = pull_request_util.enumerate_upgrade_pull_requests(
state='all',
upgrade_pull_requests = tuple(
pull_request_util.enumerate_upgrade_pull_requests(
state='all',
),
)
own_component = current_component()
Expand Down
41 changes: 16 additions & 25 deletions github/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import enum
import io
import logging
import re

import typing
Expand All @@ -26,6 +27,8 @@
import ci.util
import version

logger = logging.getLogger(__name__)


class RepoPermission(enum.Enum):
PULL = "pull"
Expand Down Expand Up @@ -310,37 +313,25 @@ def enumerate_upgrade_pull_requests(
self,
state: str='all',
pattern: re.Pattern=None,
):
'''returns a sequence of `UpgradePullRequest` for all found pull-requests
'''
) -> typing.Generator[UpgradePullRequest, None, None]:
def has_upgrade_pr_title(pull_request):
return bool(pattern.fullmatch(pull_request.title))

def pr_to_upgrade_pr(pull_request):
return self._pr_to_upgrade_pull_request(
pull_request=pull_request,
pattern=pattern,
)

def strip_title(pull_request):
pull_request.title = pull_request.title.strip()
return pull_request

if not pattern:
pattern = self._pr_title_pattern

parsed_prs = ci.util.FluentIterable(
self.repository.pull_requests(
state=state,
number=128, # avoid issueing more than one github-api-request
for pull_request in self.repository.pull_requests(
state=state,
number=128, # avoid issueing more than one github-api-request
):
pull_request.title = pull_request.title.strip()
if not has_upgrade_pr_title(pull_request):
continue

yield self._pr_to_upgrade_pull_request(
pull_request=pull_request,
pattern=pattern,
)
) \
.map(strip_title) \
.filter(has_upgrade_pr_title) \
.map(pr_to_upgrade_pr) \
.filter(lambda e: e) \
.as_list()
return parsed_prs

def retrieve_pr_template_text(self):
'''Return the content for the PR template file looking in predefined directories.
Expand Down Expand Up @@ -380,7 +371,7 @@ def create_or_update_file(
decoded_contents = contents.decoded.decode('utf-8')
if decoded_contents == file_contents:
# Nothing to do
return ci.util.info(
return logger.info(
'Repository file contents are identical to passed file contents.'
)
else:
Expand Down
8 changes: 0 additions & 8 deletions test/ci/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ def test_fail():
assert len(stdout.getvalue()) == 0


def test_success():
with capture_out() as (stdout, stderr):
examinee.success('xxx')

assert 'SUCCESS: xxx' == stdout.getvalue().strip()
assert len(stderr.getvalue()) == 0


class UtilTest(unittest.TestCase):
def test_not_empty(self):
result = examinee.not_empty('foo')
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def is_semver_parseable(version_string: str):
try:
parse_to_semver(version_string)
except ValueError:
ci.util.verbose(f"Could not parse '{version_string}' as semver version")
logger.debug(f"Could not parse '{version_string}' as semver version")
return False
return True

Expand Down

0 comments on commit 6d1cfd9

Please sign in to comment.