Gitlab will migrate to a new storage backend starting 0300 UTC on 2020-04-04. We do not anticipate a maintenance window for this migration. Performance may be impacted over the weekend. Thanks for your patience.

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):
class SystemArchiveGit(object):
def __init__(self, root, rw=False):
self.root = os.path.abspath(root)
self.repo = git.Repo.init(root)
self.repo = None
# config = repo.config_writer()
# config.set_value("user", "email", "")
# 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):
"""Commit system code base to archive git repo.
......@@ -57,8 +65,10 @@ class SystemArchiveGit(object):
Returns SHA1 hex commit id of commit.
"""
if not self.repo:
raise SystemArchiveGitError("repo not open, use context manager")
repo = self.repo
root = self.repo.working_dir
root = repo.working_dir
# clean up the working tree, in case a previous commit was
# aborted or hung.
......@@ -111,10 +121,10 @@ class SystemArchiveGit(object):
repo.index.commit(message)
# return the current commit hex SHA1
return self.hexsha
return self.get_hexsha()
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):
return git_hex2int(self.get_hexsha(), digits=digits)
......
......@@ -159,9 +159,10 @@ class Daemon(object):
guardian version: {}
'''.format(__version__)
ret = False
if self.archive.commit(self.system, message):
self.log.info("system archive: code changes detected and committed")
ret = True
with self.archive as archive:
if archive.commit(self.system, message):
self.log.info("system archive: code changes detected and committed")
ret = True
self['ARCHIVE_ID'] = self.archive.get_intsha()
return ret
......
......@@ -32,13 +32,13 @@ sys = GuardSystem('TEST0')
sys.load()
archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path)
msg0 = 'COMMIT 0'
id0 = repo.commit(sys, msg0)
# try another commit with no changes to make sure no commit is
# registered
msg1 = 'COMMIT 1'
id1 = repo.commit(sys, msg1)
with SystemArchiveGit(archive_path) as repo:
msg0 = 'COMMIT 0'
id0 = repo.commit(sys, msg0)
# try another commit with no changes to make sure no commit is
# registered
msg1 = 'COMMIT 1'
id1 = repo.commit(sys, msg1)
print id0, msg0
EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST0 \
......@@ -54,9 +54,9 @@ from guardian.archive import SystemArchiveGit
sys = GuardSystem('./TEST1.py')
archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path)
sys.load()
repo.commit(sys, 'COMMIT 0')
with SystemArchiveGit(archive_path) as repo:
repo.commit(sys, 'COMMIT 0')
EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST1 \
&& git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|") \
......@@ -94,9 +94,9 @@ from guardian.archive import SystemArchiveGit
sys = GuardSystem('./TEST1.py')
archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path)
sys.load()
repo.commit(sys, 'COMMIT 1')
with SystemArchiveGit(archive_path) as repo:
repo.commit(sys, 'COMMIT 1')
EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST1 \
&& git ls-files | sed "s|${TMP_DIRECTORY#/}|TMP_DIR|" | sort) >OUTPUT
......@@ -121,9 +121,9 @@ from guardian.archive import SystemArchiveGit
sys = GuardSystem('./TEST1.py')
archive_path = os.getenv('GUARD_ARCHIVE_ROOT')
archive_path = os.path.join(archive_path, sys.name)
repo = SystemArchiveGit(archive_path)
sys.load()
repo.commit(sys, 'COMMIT 2')
with SystemArchiveGit(archive_path) as repo:
repo.commit(sys, 'COMMIT 2')
EOF
(cd "$GUARD_ARCHIVE_ROOT"/TEST1 \
&& 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