commit:     f0e78af28a5e1c6275cd5b6b3f8c602e7fd16f7f
Author:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
AuthorDate: Fri Apr 17 17:17:49 2020 +0000
Commit:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
CommitDate: Fri Apr 17 18:55:18 2020 +0000
URL:        https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=f0e78af2

catalyst: Simplify GenBase

So much was wrong with this code. Deleting a file if it exists only to
open it with 'w' (which would have truncated it anyway!), checking for
presence of files that must exist and doing it in the wrong place, etc.

Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>

 catalyst/base/genbase.py | 36 ++++++++++++------------------------
 1 file changed, 12 insertions(+), 24 deletions(-)

diff --git a/catalyst/base/genbase.py b/catalyst/base/genbase.py
index 632ee0d9..bc7f9375 100644
--- a/catalyst/base/genbase.py
+++ b/catalyst/base/genbase.py
@@ -3,10 +3,9 @@ import hashlib
 import io
 import os
 
-
 class GenBase():
     """
-    This class does generation of the contents and digests files.
+    Generates CONTENTS and DIGESTS files.
     """
 
     def __init__(self, myspec):
@@ -27,27 +26,16 @@ class GenBase():
         return f'# {name.upper()} HASH\n{h.hexdigest()}  {filename}\n'
 
     def gen_contents_file(self, path):
-        contents = path + ".CONTENTS"
-        if os.path.exists(contents):
-            os.remove(contents)
-
-        contents_map = self.settings["contents_map"]
-        if os.path.exists(path):
-            with io.open(contents, "w", encoding='utf-8') as myf:
-                contents = contents_map.contents(path, '',
-                                                 
verbose=self.settings["VERBOSE"])
-                if contents:
-                    myf.write(contents)
+        c = self.settings['contents_map']
+
+        with io.open(path + '.CONTENTS', 'w', encoding='utf-8') as file:
+            file.write(c.contents(path, '', verbose=self.settings['VERBOSE']))
 
     def gen_digest_file(self, path):
-        digests = path + ".DIGESTS"
-        if os.path.exists(digests):
-            os.remove(digests)
-        if "digests" in self.settings:
-            if os.path.exists(path):
-                with io.open(digests, "w", encoding='utf-8') as myf:
-                    for f in [path, path + '.CONTENTS']:
-                        if os.path.exists(f):
-                            for i in self.settings["digests"].split():
-                                digest = self.generate_hash(f, name=i)
-                                myf.write(digest)
+        if 'digests' not in self.settings:
+            return
+
+        with io.open(path + '.DIGESTS', 'w', encoding='utf-8') as file:
+            for f in [path, path + '.CONTENTS']:
+                for i in self.settings['digests'].split():
+                    file.write(self.generate_hash(f, name=i))

Reply via email to