Skip to content

Commit

Permalink
Rely on Python 2.5+ context managers
Browse files Browse the repository at this point in the history
In Python 2.5 context managers were added, which can help ensure open
files are closed. EL 5 shipped with Python 2.4, but EL6 has Python 2.6
meaning we can now rely on this shiny new feature.
  • Loading branch information
ekohl committed Jul 16, 2024
1 parent 3a8380e commit c343bbf
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/katello/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,11 @@ def remove_cache():
pass

def is_valid(self):
cache_file = None
# wrapper try block w/ finally needed for python 2.4
try:
try:
cache_file = open(ENABLED_REPOS_CACHE_FILE, 'r')
try:
return self.data() == json.loads(cache_file.read())
except ValueError:
return False
except IOError:
return False
finally:
if cache_file is not None:
cache_file.close()
with open(ENABLED_REPOS_CACHE_FILE, 'r') as cache_file:
return self.data() == json.loads(cache_file.read())
except (ValueError, IOError):
return False

def data(self):
return {self.consumer_id: self.content}
Expand All @@ -88,10 +79,5 @@ def save(self):
if not os.path.isdir(cache_dir):
os.makedirs(cache_dir)

cache_file = None
try:
cache_file = open(ENABLED_REPOS_CACHE_FILE, 'w')
with open(ENABLED_REPOS_CACHE_FILE, 'w') as cache_file:
cache_file.write(json.dumps(self.data()))
finally:
if cache_file is not None:
cache_file.close()

0 comments on commit c343bbf

Please sign in to comment.