Browse Source

Add git pull with cooldown interval

pull/5/head
Stefan Haun 2 years ago
parent
commit
50ebba0df6
  1. 42
      gitmgr.py

42
gitmgr.py

@ -2,6 +2,7 @@ import git
import os
import shutil
import tempfile
import time
from util import load_env
@ -12,18 +13,21 @@ class GitManagerConfiguration:
origin = load_env("GIT_ORIGIN", None)
wc_path = load_env("GIT_WC_PATH", None)
git_pw = load_env("GIT_PASSWORD", None)
pull_intv = load_env("GIT_PULL_INTV", None)
return GitManagerConfiguration(origin=origin,
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:
raise ValueError("Git origin cannot be empty!")
self._origin = origin
self._git_pw = git_pw
self._wc_path = wc_path
self._pull_intv = 30 if pull_intv is None else int(pull_intv)
@property
def origin(self):
@ -37,6 +41,10 @@ class GitManagerConfiguration:
def wc_path(self):
return self._wc_path
@property
def pull_intv(self):
return self._pull_intv
class GitManager:
def __init__(self, configuration):
@ -45,6 +53,7 @@ class GitManager:
self._configuration = configuration
self._wc = None
self._last_pull = 0
@property
def configuration(self):
@ -138,6 +147,7 @@ class GitManager:
def setup(self):
self._setup_wc()
self._init_repo()
self.pull(force=True)
def teardown(self):
self._teardown_wc()
@ -148,3 +158,31 @@ class GitManager:
print(f"\tUsing working copy path %s" % self._wc)
if not self._wc == self.configuration.wc_path:
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

Loading…
Cancel
Save