From 143a6e0ae8b2a64ce7381556c48d87230662b626 Mon Sep 17 00:00:00 2001 From: Corey Bryant Date: Thu, 11 Dec 2014 13:34:39 +0000 Subject: [PATCH] Sync charm-helpers. --- hooks/charmhelpers/__init__.py | 22 ++++++++++++++++++++++ hooks/charmhelpers/__init__.pyc | Bin 0 -> 117 bytes hooks/charmhelpers/core/hookenv.py | 18 ++++++++++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 hooks/charmhelpers/__init__.pyc diff --git a/hooks/charmhelpers/__init__.py b/hooks/charmhelpers/__init__.py index e69de29b..b46e2e23 100644 --- a/hooks/charmhelpers/__init__.py +++ b/hooks/charmhelpers/__init__.py @@ -0,0 +1,22 @@ +# Bootstrap charm-helpers, installing its dependencies if necessary using +# only standard libraries. +import subprocess +import sys + +try: + import six # flake8: noqa +except ImportError: + if sys.version_info.major == 2: + subprocess.check_call(['apt-get', 'install', '-y', 'python-six']) + else: + subprocess.check_call(['apt-get', 'install', '-y', 'python3-six']) + import six # flake8: noqa + +try: + import yaml # flake8: noqa +except ImportError: + if sys.version_info.major == 2: + subprocess.check_call(['apt-get', 'install', '-y', 'python-yaml']) + else: + subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml']) + import yaml # flake8: noqa diff --git a/hooks/charmhelpers/__init__.pyc b/hooks/charmhelpers/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b4469deb1b88a1cfa5fc2e98d17007f28e636d39 GIT binary patch literal 117 zcmZSn%*&;b)De=*00oRd+5w1*S%5?e14FO|NW@PANHCxg#d1KgjQsrUV*TWd#G>4c o)SQCUqGJ8{_{_Y_lK6PNg31yOpc0$h{FKt1R6CGC#X!se03rMpr2qf` literal 0 HcmV?d00001 diff --git a/hooks/charmhelpers/core/hookenv.py b/hooks/charmhelpers/core/hookenv.py index 07d1f690..69ae4564 100644 --- a/hooks/charmhelpers/core/hookenv.py +++ b/hooks/charmhelpers/core/hookenv.py @@ -395,21 +395,31 @@ def relations_of_type(reltype=None): return relation_data +@cached +def metadata(): + """Get the current charm metadata.yaml contents as a python object""" + with open(os.path.join(charm_dir(), 'metadata.yaml')) as md: + return yaml.safe_load(md) + + @cached def relation_types(): """Get a list of relation types supported by this charm""" - charmdir = os.environ.get('CHARM_DIR', '') - mdf = open(os.path.join(charmdir, 'metadata.yaml')) - md = yaml.safe_load(mdf) rel_types = [] + md = metadata() for key in ('provides', 'requires', 'peers'): section = md.get(key) if section: rel_types.extend(section.keys()) - mdf.close() return rel_types +@cached +def charm_name(): + """Get the name of the current charm as is specified on metadata.yaml""" + return metadata().get('name') + + @cached def relations(): """Get a nested dictionary of relation data for all related units"""