From b829654a5a72840013f0929d2574e0a6bb6d8533 Mon Sep 17 00:00:00 2001 From: Joshua Lock Date: Wed, 10 Aug 2011 18:07:23 -0700 Subject: bb/ui/crumbs/configurator: introduce writeConfFile method for all writes Configuration files are written in several places, this refactors the code to use a common method. (Bitbake rev: 2843645755abb736220d7404dc6e853929093ff9) Signed-off-by: Joshua Lock Signed-off-by: Richard Purdie --- bitbake/lib/bb/ui/crumbs/configurator.py | 46 +++++++++++++------------------- 1 file changed, 19 insertions(+), 27 deletions(-) (limited to 'bitbake') diff --git a/bitbake/lib/bb/ui/crumbs/configurator.py b/bitbake/lib/bb/ui/crumbs/configurator.py index e17d5559ff..c37e9175ea 100644 --- a/bitbake/lib/bb/ui/crumbs/configurator.py +++ b/bitbake/lib/bb/ui/crumbs/configurator.py @@ -207,6 +207,19 @@ class Configurator(gobject.GObject): return "".join(layer_entry) + def writeConfFile(self, conffile, contents): + """ + Make a backup copy of conffile and write a new file in its stead with + the lines in the contents list. + """ + # Create a backup of the conf file + bkup = "%s~" % conffile + os.rename(conffile, bkup) + + # Write the contents list object to the conf file + with open(conffile, "w") as new: + new.write("".join(contents)) + def writeLocalConf(self): # Dictionary containing only new or modified variables changed_values = {} @@ -218,12 +231,8 @@ class Configurator(gobject.GObject): if not len(changed_values): return - # Create a backup of the local.conf - bkup = "%s~" % self.local - os.rename(self.local, bkup) - # read the original conf into a list - with open(bkup, 'r') as config: + with open(self.local, 'r') as config: config_lines = config.readlines() new_config_lines = ["\n"] @@ -259,20 +268,14 @@ class Configurator(gobject.GObject): # Add the modified variables config_lines.extend(new_config_lines) - # Write the updated lines list object to the local.conf - with open(self.local, "w") as n: - n.write("".join(config_lines)) + self.writeConfFile(self.local, config_lines) del self.orig_config self.orig_config = copy.deepcopy(self.config) def insertTempBBPath(self, bbpath, bbfiles): - # Create a backup of the local.conf - bkup = "%s~" % self.local - os.rename(self.local, bkup) - # read the original conf into a list - with open(bkup, 'r') as config: + with open(self.local, 'r') as config: config_lines = config.readlines() if bbpath: @@ -280,9 +283,7 @@ class Configurator(gobject.GObject): if bbfiles: config_lines.append("BBFILES := \"${BBFILES} %s\"\n" % bbfiles) - # Write the updated lines list object to the local.conf - with open(self.local, "w") as n: - n.write("".join(config_lines)) + self.writeConfFile(self.local, config_lines) def writeLayerConf(self): # If we've not added/removed new layers don't write @@ -292,23 +293,14 @@ class Configurator(gobject.GObject): # This pattern should find the existing BBLAYERS pattern = 'BBLAYERS\s=\s\".*\"' - # Backup the users bblayers.conf - bkup = "%s~" % self.bblayers - os.rename(self.bblayers, bkup) - replacement = self._constructLayerEntry() - with open(bkup, "r") as f: + with open(self.bblayers, "r") as f: contents = f.read() p = re.compile(pattern, re.DOTALL) new = p.sub(replacement, contents) - with open(self.bblayers, "w") as n: - n.write(new) - - # At some stage we should remove the backup we've created - # though we should probably verify it first - #os.remove(bkup) + self.writeConfFile(self.bblayers, new) # set loaded_layers for dirtiness tracking self.loaded_layers = copy.deepcopy(self.enabled_layers) -- cgit v1.2.3-54-g00ecf