Skip to content

Commit c3eebd2

Browse files
committed
Pass metadata into get_charm_series to avoid multiple file reads at each deploy
1 parent fd89d1a commit c3eebd2

3 files changed

Lines changed: 18 additions & 34 deletions

File tree

juju/application.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,9 +823,12 @@ async def local_refresh(
823823

824824
series = (
825825
await self.get_series() or
826-
self.model.info.get('default-series', '') or
827-
await get_charm_series(charm_dir, self.model)
826+
self.model.info.get('default-series', '')
828827
)
828+
if not series:
829+
metadata = utils.get_local_charm_metadata(charm_dir)
830+
await get_charm_series(metadata, self.model)
831+
829832
if not series:
830833
default_series = model_config.get("default-series")
831834
if default_series:

juju/bundle.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,10 @@ async def _handle_local_charms(self, bundle, bundle_dir):
124124
pass
125125
except FileNotFoundError:
126126
continue
127-
series = (
128-
app_dict.get('series') or
129-
default_series or
130-
await get_charm_series(charm_dir, self.model)
131-
)
127+
series = (app_dict.get('series') or default_series)
128+
if not series:
129+
metadata = utils.get_local_charm_metadata(charm_dir)
130+
series = await get_charm_series(metadata, self.model)
132131
if not self.model.connection().is_using_old_client and not series:
133132
raise JujuError(
134133
"Couldn't determine series for charm at {}. "
@@ -430,32 +429,17 @@ def is_local_charm(charm_url):
430429
return charm_url.startswith('.') or charm_url.startswith('local:') or os.path.isabs(charm_url)
431430

432431

433-
async def get_charm_series(path, model):
434-
"""Inspects the charm directory at ``path`` and returns a default
435-
series from its metadata.yaml (the first item in the 'series' list).
432+
async def get_charm_series(metadata, model):
433+
"""Inspects the given metadata and returns a default series from its
434+
metadata.yaml (the first item in the 'series' list).
436435
437-
Tries to extract the informiation from the given model if no
438-
series is determined from the path.
436+
Tries to extract the information from the given model if no
437+
series is determined from the given metadata.
439438
Returns None if no series can be determined.
440439
441440
"""
442-
path = Path(path)
443-
try:
444-
if path.suffix == '.charm':
445-
md = "metadata.yaml in %s" % path
446-
with zipfile.ZipFile(str(path), 'r') as charm_file:
447-
data = yaml.safe_load(charm_file.read('metadata.yaml'))
448-
else:
449-
md = path / "metadata.yaml"
450-
if not md.exists():
451-
return None
452-
data = yaml.safe_load(md.open())
453-
except yaml.YAMLError as exc:
454-
if hasattr(exc, "problem_mark"):
455-
mark = exc.problem_mark
456-
log.error("Error parsing YAML file {}, line {}, column: {}".format(md, mark.line, mark.column))
457-
raise
458-
_series = data.get('series')
441+
442+
_series = metadata.get('series')
459443
series = _series[0] if _series else None
460444

461445
if series is None:

juju/model.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,14 +1795,11 @@ async def deploy(
17951795
identifier)
17961796
else:
17971797
# We have a local charm dir that needs to be uploaded
1798-
charm_dir = os.path.abspath(
1799-
os.path.expanduser(identifier))
1798+
charm_dir = os.path.abspath(os.path.expanduser(identifier))
18001799
charm_origin = res.origin
18011800

18021801
metadata = utils.get_local_charm_metadata(charm_dir)
1803-
# TODO (cderici) : pass the metadata into get_charm_series, as
1804-
# it also reads that file redundantly
1805-
charm_series = charm_series or await get_charm_series(charm_dir,
1802+
charm_series = charm_series or await get_charm_series(metadata,
18061803
self)
18071804

18081805
# If we're using a newer client, then the CharmOrigin needs a

0 commit comments

Comments
 (0)