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.
This commit is contained in:
Christian Kellner 2020-02-06 14:42:29 +01:00 committed by Tom Gundersen
parent 6f4d286ff4
commit 5a61d8c869

View file

@ -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