Skip to content

Commit 0bac4ef

Browse files
authored
Merge pull request #509 from juju/johnsca/metadata-referenced-before-assignment
#509 For the life of me, I can't figure out how this wasn't caught by the test added in #486.
2 parents a175d76 + ffcc640 commit 0bac4ef

4 files changed

Lines changed: 22 additions & 30 deletions

File tree

juju/application.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
import logging
1818
import os
1919

20-
from . import model, tag
20+
from . import model, tag, utils
2121
from .status import derive_status
2222
from .annotationhelper import _get_annotations, _set_annotations
2323
from .client import client
2424
from .errors import JujuError
2525
from .bundle import get_charm_series
2626
from .placement import parse as parse_placement
27-
from .charm import get_local_charm_metadata
2827

2928
log = logging.getLogger(__name__)
3029

@@ -718,7 +717,7 @@ async def local_refresh(
718717
if default_series:
719718
series = default_series.value
720719
charm_url = await self.model.add_local_charm_dir(charm_dir, series)
721-
metadata = get_local_charm_metadata(path)
720+
metadata = utils.get_local_charm_metadata(path)
722721
if resources is not None:
723722
resources = await self.model.add_local_resources(self.entity_id,
724723
charm_url,

juju/charm.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,10 @@
1313
# limitations under the License.
1414

1515
import logging
16-
import zipfile
17-
import yaml
18-
from pathlib import Path
1916
from . import model
2017

2118
log = logging.getLogger(__name__)
2219

2320

24-
def get_local_charm_metadata(path):
25-
"""Retrieve Metadata of a Charm from its path
26-
27-
:patam str path: Path of charm directory or .charm file
28-
29-
:return: Object of charm metadata
30-
"""
31-
if str(path).endswith('.charm'):
32-
with zipfile.ZipFile(path, 'r') as charm_file:
33-
metadata = yaml.load(charm_file.read('metadata.yaml'), Loader=yaml.FullLoader)
34-
else:
35-
entity_path = Path(path)
36-
metadata_path = entity_path / 'metadata.yaml'
37-
metadata = yaml.load(metadata_path.read_text(), Loader=yaml.FullLoader)
38-
39-
return metadata
40-
41-
4221
class Charm(model.ModelEntity):
4322
pass

juju/model.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,6 @@ async def deploy(
14191419
entity_url = str(entity_url) # allow for pathlib.Path objects
14201420
entity_path = Path(entity_url.replace('local:', ''))
14211421
bundle_path = entity_path / 'bundle.yaml'
1422-
metadata_path = entity_path / 'metadata.yaml'
14231422

14241423
is_local = (
14251424
entity_url.startswith('local:') or
@@ -1470,12 +1469,8 @@ async def deploy(
14701469
entity_id,
14711470
entity=entity)
14721471
else:
1472+
metadata = utils.get_local_charm_metadata(entity_path)
14731473
if not application_name:
1474-
if str(entity_path).endswith('.charm'):
1475-
with zipfile.ZipFile(entity_path, 'r') as charm_file:
1476-
metadata = yaml.load(charm_file.read('metadata.yaml'), Loader=yaml.FullLoader)
1477-
else:
1478-
metadata = yaml.load(metadata_path.read_text(), Loader=yaml.FullLoader)
14791474
application_name = metadata['name']
14801475
# We have a local charm dir that needs to be uploaded
14811476
charm_dir = os.path.abspath(

juju/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pyasn1.type import univ, char
99
from pyasn1.codec.der.encoder import encode
1010
import yaml
11+
import zipfile
1112

1213

1314
async def execute_process(*cmd, log=None, loop=None):
@@ -185,3 +186,21 @@ def generate_user_controller_access_token(username, controller_endpoints, secret
185186
remainder = len(registration_string) % 3
186187
registration_string += b"\0" * (3 - remainder)
187188
return base64.urlsafe_b64encode(registration_string)
189+
190+
191+
def get_local_charm_metadata(path):
192+
"""Retrieve Metadata of a Charm from its path
193+
194+
:patam str path: Path of charm directory or .charm file
195+
196+
:return: Object of charm metadata
197+
"""
198+
if str(path).endswith('.charm'):
199+
with zipfile.ZipFile(path, 'r') as charm_file:
200+
metadata = yaml.load(charm_file.read('metadata.yaml'), Loader=yaml.FullLoader)
201+
else:
202+
entity_path = Path(path)
203+
metadata_path = entity_path / 'metadata.yaml'
204+
metadata = yaml.load(metadata_path.read_text(), Loader=yaml.FullLoader)
205+
206+
return metadata

0 commit comments

Comments
 (0)