Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache loaded drivers and adopt lru_cache #2259

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions molecule/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pkg_resources

try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache


@lru_cache()
def molecule_drivers(as_dict=False):

plugins = {
Expand Down
24 changes: 15 additions & 9 deletions molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
from molecule.verifier import inspec
from molecule.verifier import testinfra

try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache


LOG = logger.get_logger(__name__)
MOLECULE_DEBUG = boolean(os.environ.get('MOLECULE_DEBUG', 'False'))
MOLECULE_DIRECTORY = 'molecule'
Expand Down Expand Up @@ -139,7 +145,7 @@ def molecule_directory(self):
return molecule_directory(self.project_directory)

@property
@util.memoize
@lru_cache()
def dependency(self):
dependency_name = self.config['dependency']['name']
if dependency_name == 'galaxy':
Expand All @@ -150,7 +156,7 @@ def dependency(self):
return shell.Shell(self)

@property
@util.memoize
@lru_cache()
def driver(self):
driver_name = self._get_driver_name()
driver = None
Expand Down Expand Up @@ -188,36 +194,36 @@ def env(self):
}

@property
@util.memoize
@lru_cache()
def lint(self):
lint_name = self.config['lint']['name']
if lint_name == 'yamllint':
return yamllint.Yamllint(self)

@property
@util.memoize
@lru_cache()
def platforms(self):
return platforms.Platforms(self, parallelize_platforms=self.is_parallel)

@property
@util.memoize
@lru_cache()
def provisioner(self):
provisioner_name = self.config['provisioner']['name']
if provisioner_name == 'ansible':
return ansible.Ansible(self)

@property
@util.memoize
@lru_cache()
def scenario(self):
return scenario.Scenario(self)

@property
@util.memoize
@lru_cache()
def state(self):
return state.State(self)

@property
@util.memoize
@lru_cache()
def verifier(self):
verifier_name = self.config['verifier']['name']
if verifier_name == 'testinfra':
Expand All @@ -230,7 +236,7 @@ def verifier(self):
return ansible_verifier.Ansible(self)

@property
@util.memoize
@lru_cache()
def verifiers(self):
return molecule_verifiers()

Expand Down
7 changes: 6 additions & 1 deletion molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
import os
import shutil

try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache

from molecule import logger
from molecule import util
from molecule.provisioner import base
Expand Down Expand Up @@ -609,7 +614,7 @@ def config_file(self):
return os.path.join(self._config.scenario.ephemeral_directory, 'ansible.cfg')

@property
@util.memoize
@lru_cache()
def playbooks(self):
return ansible_playbooks.AnsiblePlaybooks(self._config)

Expand Down
8 changes: 6 additions & 2 deletions molecule/provisioner/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

import abc

from molecule import util
try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache

from molecule.provisioner.lint import ansible_lint


Expand Down Expand Up @@ -65,7 +69,7 @@ def name(self): # pragma: no cover
pass

@property
@util.memoize
@lru_cache()
def lint(self):
lint_name = self._config.config['provisioner']['lint']['name']
if lint_name == 'ansible-lint':
Expand Down
14 changes: 0 additions & 14 deletions molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,6 @@ def merge_dicts(a, b):
return a


def memoize(function):
memo = {}

def wrapper(*args, **kwargs):
if args not in memo:
rv = function(*args, **kwargs)
memo[args] = rv

return rv
return memo[args]

return wrapper


def validate_parallel_cmd_args(cmd_args):
if cmd_args.get('parallel') and cmd_args.get('destroy') == 'never':
msg = 'Combining "--parallel" and "--destroy=never" is not supported'
Expand Down
7 changes: 6 additions & 1 deletion molecule/verifier/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

import abc

try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache

from molecule import util
from molecule.verifier.lint import flake8
from molecule.verifier.lint import precommit
Expand Down Expand Up @@ -102,7 +107,7 @@ def env(self):
)

@property
@util.memoize
@lru_cache()
def lint(self):
lint_name = self._config.config['verifier']['lint']['name']
if lint_name == 'flake8':
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ install_requires =
# https://github.com/ssato/python-anyconfig/issues/110
# 0.9.8, 0.9.9 are known to be broken
anyconfig == 0.9.7
backports.functools_lru_cache; python_version<"3.3"
flake8 >=3.6.0
cerberus >= 1.3.1
click >= 6.7
Expand Down