Commit 260b7c30 authored by Jameson Rollins's avatar Jameson Rollins

archive: context manager for repo

In some versions of git-python, the repo will hold open files in
"git cat-file" subprocess calls.  The solution seems to be to delete the
Repo object after use.

This hopefully closes #4
parent 158f744e
Pipeline #14879 passed with stage
in 4 minutes and 52 seconds
...@@ -44,11 +44,19 @@ class SystemArchiveGitError(Exception): ...@@ -44,11 +44,19 @@ class SystemArchiveGitError(Exception):
class SystemArchiveGit(object): class SystemArchiveGit(object):
def __init__(self, root, rw=False): def __init__(self, root, rw=False):
self.root = os.path.abspath(root) self.root = os.path.abspath(root)
self.repo = git.Repo.init(root) self.repo = None
# config = repo.config_writer() # config = repo.config_writer()
# config.set_value("user", "email", "") # config.set_value("user", "email", "")
# config.set_value("user", "name", "Guardian Daemon") # config.set_value("user", "name", "Guardian Daemon")
def __enter__(self):
self.repo = git.Repo.init(self.root)
return self
def __exit__(self, *args):
del self.repo
self.repo = None
def commit(self, system, message): def commit(self, system, message):
"""Commit system code base to archive git repo. """Commit system code base to archive git repo.
...@@ -57,8 +65,10 @@ class SystemArchiveGit(object): ...@@ -57,8 +65,10 @@ class SystemArchiveGit(object):
Returns SHA1 hex commit id of commit. Returns SHA1 hex commit id of commit.
""" """
if not self.repo:
raise SystemArchiveGitError("repo not open, use context manager")
repo = self.repo repo = self.repo
root = self.repo.working_dir root = repo.working_dir
# clean up the working tree, in case a previous commit was # clean up the working tree, in case a previous commit was
# aborted or hung. # aborted or hung.
...@@ -111,10 +121,10 @@ class SystemArchiveGit(object): ...@@ -111,10 +121,10 @@ class SystemArchiveGit(object):
repo.index.commit(message) repo.index.commit(message)
# return the current commit hex SHA1 # return the current commit hex SHA1
return self.hexsha return self.get_hexsha()
def get_hexsha(self): def get_hexsha(self):
return self.repo.head.commit.hexsha return git.Repo.init(self.root).head.commit.hexsha
def get_intsha(self, digits=7): def get_intsha(self, digits=7):
return git_hex2int(self.get_hexsha(), digits=digits) return git_hex2int(self.get_hexsha(), digits=digits)
......
...@@ -159,9 +159,10 @@ class Daemon(object): ...@@ -159,9 +159,10 @@ class Daemon(object):
guardian version: {} guardian version: {}
'''.format(__version__) '''.format(__version__)
ret = False ret = False
if self.archive.commit(self.system, message): with self.archive as archive:
self.log.info("system archive: code changes detected and committed") if archive.commit(self.system, message):
ret = True self.log.info("system archive: code changes detected and committed")
ret = True
self['ARCHIVE_ID'] = self.archive.get_intsha() self['ARCHIVE_ID'] = self.archive.get_intsha()
return ret return ret
......
...@@ -32,13 +32,13 @@ sys = GuardSystem('TEST0') ...@@ -32,13 +32,13 @@ sys = GuardSystem('TEST0')
sys.load() sys.load()
archive_path = os.getenv('GUARD_ARCHIVE_ROOT') archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name) archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path) with SystemArchiveGit(archive_path) as repo:
msg0 = 'COMMIT 0' msg0 = 'COMMIT 0'
id0 = repo.commit(sys, msg0) id0 = repo.commit(sys, msg0)
# try another commit with no changes to make sure no commit is # try another commit with no changes to make sure no commit is
# registered # registered
msg1 = 'COMMIT 1' msg1 = 'COMMIT 1'
id1 = repo.commit(sys, msg1) id1 = repo.commit(sys, msg1)
print id0, msg0 print id0, msg0
EOF EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST0 \ (cd "$GUARD_ARCHIVE_ROOT"/TEST0 \
...@@ -54,9 +54,9 @@ from guardian.archive import SystemArchiveGit ...@@ -54,9 +54,9 @@ from guardian.archive import SystemArchiveGit
sys = GuardSystem('./TEST1.py') sys = GuardSystem('./TEST1.py')
archive_path = os.getenv('GUARD_ARCHIVE_ROOT') archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name) archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path)
sys.load() sys.load()
repo.commit(sys, 'COMMIT 0') with SystemArchiveGit(archive_path) as repo:
repo.commit(sys, 'COMMIT 0')
EOF EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST1 \ (cd "$GUARD_ARCHIVE_ROOT"/TEST1 \
&& git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|") \ && git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|") \
...@@ -94,9 +94,9 @@ from guardian.archive import SystemArchiveGit ...@@ -94,9 +94,9 @@ from guardian.archive import SystemArchiveGit
sys = GuardSystem('./TEST1.py') sys = GuardSystem('./TEST1.py')
archive_path = os.getenv('GUARD_ARCHIVE_ROOT') archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name) archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path)
sys.load() sys.load()
repo.commit(sys, 'COMMIT 1') with SystemArchiveGit(archive_path) as repo:
repo.commit(sys, 'COMMIT 1')
EOF EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST1 \ (cd "$GUARD_ARCHIVE_ROOT"/TEST1 \
&& git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|" | sort) >OUTPUT && git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|" | sort) >OUTPUT
...@@ -121,9 +121,9 @@ from guardian.archive import SystemArchiveGit ...@@ -121,9 +121,9 @@ from guardian.archive import SystemArchiveGit
sys = GuardSystem('./TEST1.py') sys = GuardSystem('./TEST1.py')
archive_path = os.getenv('GUARD_ARCHIVE_ROOT') archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name) archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path)
sys.load() sys.load()
repo.commit(sys, 'COMMIT 2') with SystemArchiveGit(archive_path) as repo:
repo.commit(sys, 'COMMIT 2')
EOF EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST1 \ (cd "$GUARD_ARCHIVE_ROOT"/TEST1 \
&& git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|" | sort) >OUTPUT && git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|" | sort) >OUTPUT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment