commit:     5cce2772eda7d0885c9d7cfea184a3f5606dddef
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Mon Nov  9 04:46:28 2015 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Mon Nov  9 04:46:28 2015 +0000
URL:        https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=5cce2772

catalyst: Convert nearly all use of open() to use the with statement.

There was one use in stagebase that was too large a code block to convert at 
this time.

 catalyst/base/stagebase.py        |  5 ++---
 catalyst/config.py                |  5 ++---
 catalyst/support.py               |  5 ++---
 catalyst/targets/livecd_stage2.py | 19 ++++++++++---------
 4 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index ffd84de..edbcaa6 100644
--- a/catalyst/base/stagebase.py
+++ b/catalyst/base/stagebase.py
@@ -848,9 +848,8 @@ class StageBase(TargetBase, ClearBase, GenBase):
                                log.error('%s', unpack_errmsg % unpack_info)
 
                        if "snapcache" in self.settings["options"]:
-                               myf = open(snapshot_cache_hash_path, 'w')
-                               myf.write(self.settings["snapshot_path_hash"])
-                               myf.close()
+                               with open(snapshot_cache_hash_path, 'w') as myf:
+                                       
myf.write(self.settings["snapshot_path_hash"])
                        else:
                                log.info('Setting snapshot autoresume point')
                                self.resume.enable("unpack_portage",

diff --git a/catalyst/config.py b/catalyst/config.py
index 5f72e15..ee73abd 100644
--- a/catalyst/config.py
+++ b/catalyst/config.py
@@ -27,12 +27,11 @@ class ParserBase(object):
 
        def parse_file(self, filename):
                try:
-                       myf = open(filename, "r")
+                       with open(filename, "r") as myf:
+                               self.lines = myf.readlines()
                except:
                        raise CatalystError("Could not open file " + filename,
                                print_traceback=True)
-               self.lines = myf.readlines()
-               myf.close()
                self.filename = filename
                self.parse()
 

diff --git a/catalyst/support.py b/catalyst/support.py
index 4bf1b22..e132568 100644
--- a/catalyst/support.py
+++ b/catalyst/support.py
@@ -155,9 +155,8 @@ def read_makeconf(mymakeconffile):
                                                import portage_util
                                                return 
portage_util.getconfig(mymakeconffile, tolerant=1, allow_sourcing=True)
                                        except ImportError:
-                                               myf=open(mymakeconffile,"r")
-                                               mylines=myf.readlines()
-                                               myf.close()
+                                               with open(mymakeconffile, "r") 
as myf:
+                                                       mylines=myf.readlines()
                                                return parse_makeconf(mylines)
                except Exception:
                        raise CatalystError("Could not parse make.conf file " +

diff --git a/catalyst/targets/livecd_stage2.py 
b/catalyst/targets/livecd_stage2.py
index fa76421..943466e 100644
--- a/catalyst/targets/livecd_stage2.py
+++ b/catalyst/targets/livecd_stage2.py
@@ -68,8 +68,17 @@ class livecd_stage2(StageBase):
        def run_local(self):
                # what modules do we want to blacklist?
                if "livecd/modblacklist" in self.settings:
+                       path = normpath(self.settings["chroot_path"],
+                                                       
"/etc/modprobe.d/blacklist.conf")
                        try:
-                               
myf=open(self.settings["chroot_path"]+"/etc/modprobe.d/blacklist.conf","a")
+                               with open(path, "a") as myf:
+                                       myf.write("\n#Added by Catalyst:")
+                                       # workaround until config.py is using 
configparser
+                                       if 
isinstance(self.settings["livecd/modblacklist"], str):
+                                               
self.settings["livecd/modblacklist"] = self.settings[
+                                                               
"livecd/modblacklist"].split()
+                                       for x in 
self.settings["livecd/modblacklist"]:
+                                               myf.write("\nblacklist "+x)
                        except:
                                self.unbind()
                                raise CatalystError("Couldn't open " +
@@ -77,14 +86,6 @@ class livecd_stage2(StageBase):
                                        "/etc/modprobe.d/blacklist.conf.",
                                        print_traceback=True)
 
-                       myf.write("\n#Added by Catalyst:")
-                       # workaround until config.py is using configparser
-                       if isinstance(self.settings["livecd/modblacklist"], 
str):
-                               self.settings["livecd/modblacklist"] = 
self.settings["livecd/modblacklist"].split()
-                       for x in self.settings["livecd/modblacklist"]:
-                               myf.write("\nblacklist "+x)
-                       myf.close()
-
        def set_action_sequence(self):
                self.settings["action_sequence"]=["unpack","unpack_snapshot",\
                                
"config_profile_link","setup_confdir","portage_overlay",\

Reply via email to