Merge lp:~xtrusia/charm-helpers/fixing-filenotfounderror into lp:charm-helpers

Proposed by Seyeong Kim on 2017-08-28
Status: Merged
Merged at revision: 786
Proposed branch: lp:~xtrusia/charm-helpers/fixing-filenotfounderror
Merge into: lp:charm-helpers
Diff against target: 22 lines (+6/-1)
1 file modified
charmhelpers/core/hookenv.py (+6/-1)
To merge this branch: bzr merge lp:~xtrusia/charm-helpers/fixing-filenotfounderror
Reviewer Review Type Date Requested Status
Stuart Bishop 2017-08-28 Approve on 2017-08-28
Review via email: mp+329727@code.launchpad.net

This proposal supersedes a proposal from 2017-08-28.

Description of the change

ignore FileNotFoundError when getting metadata file on the other machine's unit

related bug is https://bugs.launchpad.net/nrpe-charm/+bug/1712977

To post a comment you must log in.
Stuart Bishop (stub) wrote : Posted in a previous version of this proposal

FileNotFoundError doesn't exist with older Python releases. Its probably best to return None if 'not os.path.exists' rather than catch the exception.

review: Needs Fixing
Stuart Bishop (stub) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2017-08-10 14:31:31 +0000
3+++ charmhelpers/core/hookenv.py 2017-08-28 14:14:31 +0000
4@@ -218,6 +218,8 @@
5 for rid in relation_ids(reltype):
6 for unit in related_units(rid):
7 md = _metadata_unit(unit)
8+ if not md:
9+ continue
10 subordinate = md.pop('subordinate', None)
11 if not subordinate:
12 return unit
13@@ -511,7 +513,10 @@
14 """
15 basedir = os.sep.join(charm_dir().split(os.sep)[:-2])
16 unitdir = 'unit-{}'.format(unit.replace(os.sep, '-'))
17- with open(os.path.join(basedir, unitdir, 'charm', 'metadata.yaml')) as md:
18+ joineddir = os.path.join(basedir, unitdir, 'charm', 'metadata.yaml')
19+ if not os.path.exists(joineddir):
20+ return None
21+ with open(joineddir) as md:
22 return yaml.safe_load(md)
23
24

Subscribers

People subscribed via source and target branches