From 5a61d8c8693e5e06ebaa19cca1e64bdb8a65baa7 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 6 Feb 2020 14:42:29 +0100 Subject: [PATCH] objectstore: extract method to open a TreeObject Extract the opening a TreeObject out of the treesum property so that the latter is easier to read. --- osbuild/objectstore.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/osbuild/objectstore.py b/osbuild/objectstore.py index 9c4dd03a..1a640d9c 100644 --- a/osbuild/objectstore.py +++ b/osbuild/objectstore.py @@ -40,16 +40,21 @@ class TreeObject: @property def treesum(self) -> str: """Calculate the treesum of the tree""" - fd = os.open(self.path, os.O_DIRECTORY) - try: + with self.open() as fd: m = hashlib.sha256() treesum.treesum(m, fd) treesum_hash = m.hexdigest() + return treesum_hash + + @contextlib.contextmanager + def open(self): + """Open the directory and return the file descriptor""" + try: + fd = os.open(self.path, os.O_DIRECTORY) + yield fd finally: os.close(fd) - return treesum_hash - def move(self, destination: str): """Move the tree to destination