Skip to content

Commit

Permalink
refactor(cached_property): remove python3.8 backports
Browse files Browse the repository at this point in the history
this backport isn't needed now that we move to python3.8
  • Loading branch information
fruch authored and Bentsi committed Jul 5, 2020
1 parent 214b6ba commit 6d69f9f
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 72 deletions.
3 changes: 2 additions & 1 deletion sdcm/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from typing import List, Optional, Dict
from textwrap import dedent
from datetime import datetime
from functools import cached_property

from invoke.exceptions import UnexpectedExit, Failure, CommandTimedOut
import yaml
Expand All @@ -51,7 +52,7 @@

from sdcm.utils.health_checker import check_nodes_status, check_node_status_in_gossip_and_nodetool_status, \
check_schema_version, check_nulls_in_peers
from sdcm.utils.decorators import cached_property, retrying, log_run_info
from sdcm.utils.decorators import retrying, log_run_info
from sdcm.utils.get_username import get_username
from sdcm.utils.remotewebbrowser import WebDriverContainerMixin
from sdcm.utils.version_utils import SCYLLA_VERSION_RE, get_gemini_version
Expand Down
3 changes: 2 additions & 1 deletion sdcm/cluster_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from textwrap import dedent
from datetime import datetime
from distutils.version import LooseVersion # pylint: disable=no-name-in-module,import-error
from functools import cached_property

import yaml
from botocore.exceptions import WaiterError, ClientError
Expand All @@ -22,7 +23,7 @@
from sdcm import ec2_client
from sdcm.cluster import INSTANCE_PROVISION_ON_DEMAND
from sdcm.utils.common import list_instances_aws, get_ami_tags
from sdcm.utils.decorators import retrying, cached_property
from sdcm.utils.decorators import retrying
from sdcm.sct_events import SpotTerminationEvent, DbEventsFilter
from sdcm import wait
from sdcm.remote import LocalCmdRunner, NETWORK_EXCEPTIONS
Expand Down
3 changes: 2 additions & 1 deletion sdcm/cluster_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import re
import logging
from typing import Optional, Union, Dict
from functools import cached_property

from sdcm import cluster
from sdcm.remote import LOCALRUNNER
from sdcm.utils.docker_utils import get_docker_bridge_gateway, Container, ContainerManager, DockerException
from sdcm.utils.decorators import cached_property
from sdcm.utils.health_checker import check_nodes_status


DEFAULT_SCYLLA_DB_IMAGE = "scylladb/scylla-nightly"
DEFAULT_SCYLLA_DB_IMAGE_TAG = "latest"
AIO_MAX_NR_RECOMMENDED_VALUE = 1048576
Expand Down
2 changes: 1 addition & 1 deletion sdcm/cluster_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import logging
from textwrap import dedent
from typing import Dict
from functools import cached_property

from libcloud.common.google import GoogleBaseError, ResourceNotFoundError

from sdcm import cluster
from sdcm.sct_events import SpotTerminationEvent
from sdcm.utils.common import list_instances_gce, gce_meta_to_dict
from sdcm.utils.decorators import cached_property


SPOT_TERMINATION_CHECK_DELAY = 1
Expand Down
5 changes: 3 additions & 2 deletions sdcm/logcollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import zipfile
import fnmatch
import traceback
import requests
from functools import cached_property

import requests
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Expand All @@ -19,7 +20,7 @@
ParallelObject, remove_files, get_builder_by_test_id,
get_testrun_dir, search_test_id_in_latest, filter_aws_instances_by_type,
filter_gce_instances_by_type, get_sct_root_path)
from sdcm.utils.decorators import retrying, cached_property
from sdcm.utils.decorators import retrying
from sdcm.utils.get_username import get_username
from sdcm.db_stats import PrometheusDBStats
from sdcm.remote import RemoteCmdRunner, LocalCmdRunner
Expand Down
2 changes: 1 addition & 1 deletion sdcm/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from functools import cached_property

import jinja2

from sdcm.keystore import KeyStore
from sdcm.utils.common import list_instances_gce, list_instances_aws, list_resources_docker
from sdcm.utils.decorators import cached_property

LOGGER = logging.getLogger(__name__)

Expand Down
3 changes: 1 addition & 2 deletions sdcm/utils/auto_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
# Copyright (c) 2020 ScyllaDB

import os

from sdcm.utils.decorators import cached_property
from functools import cached_property


AUTO_SSH_IMAGE = "jnovack/autossh:latest"
Expand Down
62 changes: 0 additions & 62 deletions sdcm/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,12 @@
import time
import logging
import datetime
import threading
from functools import wraps, partial


LOGGER = logging.getLogger(__name__)


try:
from functools import cached_property # pylint: disable=unused-import,ungrouped-imports
except ImportError:
# Copy/pasted from https://github.com/python/cpython/blob/3.8/Lib/functools.py#L923

# TODO: remove this code after switching to Python 3.8+

################################################################################
# cached_property() - computed once per instance, cached as attribute
################################################################################

_NOT_FOUND = object()

class cached_property: # pylint: disable=invalid-name,too-few-public-methods
def __init__(self, func):
self.func = func
self.attrname = None
self.__doc__ = func.__doc__
self.lock = threading.RLock()

def __set_name__(self, owner, name):
if self.attrname is None:
self.attrname = name
elif name != self.attrname:
raise TypeError(
"Cannot assign the same cached_property to two different names "
f"({self.attrname!r} and {name!r})."
)

def __get__(self, instance, owner=None):
if instance is None:
return self
if self.attrname is None:
raise TypeError(
"Cannot use cached_property instance without calling __set_name__ on it.")
try:
cache = instance.__dict__
except AttributeError: # not all objects have __dict__ (e.g. class defines slots)
msg = (
f"No '__dict__' attribute on {type(instance).__name__!r} "
f"instance to cache {self.attrname!r} property."
)
raise TypeError(msg) from None
val = cache.get(self.attrname, _NOT_FOUND)
if val is _NOT_FOUND:
with self.lock:
# check if another thread filled cache while we awaited lock
val = cache.get(self.attrname, _NOT_FOUND)
if val is _NOT_FOUND:
val = self.func(instance)
try:
cache[self.attrname] = val
except TypeError:
msg = (
f"The '__dict__' attribute on {type(instance).__name__!r} instance "
f"does not support item assignment for caching {self.attrname!r} property."
)
raise TypeError(msg) from None
return val


class Retry(Exception):
pass

Expand Down
2 changes: 1 addition & 1 deletion sdcm/utils/remotewebbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import time
import logging
from typing import Optional
from functools import cached_property

from docker import DockerClient # pylint: disable=wrong-import-order
from selenium.webdriver import Remote, ChromeOptions

from sdcm.utils.docker_utils import ContainerManager, DOCKER_API_CALL_TIMEOUT
from sdcm.utils.common import get_free_port, wait_for_port
from sdcm.utils.ssh_agent import SSHAgent
from sdcm.utils.decorators import cached_property


WEB_DRIVER_IMAGE = "selenium/standalone-chrome:latest"
Expand Down

0 comments on commit 6d69f9f

Please sign in to comment.