Add git pull with cooldown interval

This commit is contained in:
Stefan Haun 2021-02-12 16:33:11 +01:00
parent 78671e9ad9
commit 50ebba0df6

View file

@ -2,6 +2,7 @@ import git
import os import os
import shutil import shutil
import tempfile import tempfile
import time
from util import load_env from util import load_env
@ -12,18 +13,21 @@ class GitManagerConfiguration:
origin = load_env("GIT_ORIGIN", None) origin = load_env("GIT_ORIGIN", None)
wc_path = load_env("GIT_WC_PATH", None) wc_path = load_env("GIT_WC_PATH", None)
git_pw = load_env("GIT_PASSWORD", None) git_pw = load_env("GIT_PASSWORD", None)
pull_intv = load_env("GIT_PULL_INTV", None)
return GitManagerConfiguration(origin=origin, return GitManagerConfiguration(origin=origin,
git_pw=git_pw, git_pw=git_pw,
wc_path=wc_path) wc_path=wc_path,
pull_intv=pull_intv)
def __init__(self, origin, git_pw=None, wc_path=None): def __init__(self, origin, git_pw=None, wc_path=None, pull_intv=None):
if not origin: if not origin:
raise ValueError("Git origin cannot be empty!") raise ValueError("Git origin cannot be empty!")
self._origin = origin self._origin = origin
self._git_pw = git_pw self._git_pw = git_pw
self._wc_path = wc_path self._wc_path = wc_path
self._pull_intv = 30 if pull_intv is None else int(pull_intv)
@property @property
def origin(self): def origin(self):
@ -37,6 +41,10 @@ class GitManagerConfiguration:
def wc_path(self): def wc_path(self):
return self._wc_path return self._wc_path
@property
def pull_intv(self):
return self._pull_intv
class GitManager: class GitManager:
def __init__(self, configuration): def __init__(self, configuration):
@ -45,6 +53,7 @@ class GitManager:
self._configuration = configuration self._configuration = configuration
self._wc = None self._wc = None
self._last_pull = 0
@property @property
def configuration(self): def configuration(self):
@ -138,6 +147,7 @@ class GitManager:
def setup(self): def setup(self):
self._setup_wc() self._setup_wc()
self._init_repo() self._init_repo()
self.pull(force=True)
def teardown(self): def teardown(self):
self._teardown_wc() self._teardown_wc()
@ -148,3 +158,31 @@ class GitManager:
print(f"\tUsing working copy path %s" % self._wc) print(f"\tUsing working copy path %s" % self._wc)
if not self._wc == self.configuration.wc_path: if not self._wc == self.configuration.wc_path:
print("\tUsing a temporary working copy.") print("\tUsing a temporary working copy.")
@property
def head_sha(self):
return None if self.repo is None else self.repo.head.object.hexsha
def pull(self, force=False):
"""Pull from origin.
Arguments:
`force` -- Do a pull even though the pull interval has not elapsed
Returns: True if pull was executed
"""
if not force and (time.time() - self._last_pull < self.configuration.pull_intv):
return False
self._last_pull = time.time()
old_head = self.head_sha
# get the origin
# (We verified during initialization that this origin exists.)
origin = self.repo.remote('origin')
origin.pull(rebase=True)
return self.head_sha != old_head