mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 23:51:21 +00:00
122 lines
3.4 KiB
Python
122 lines
3.4 KiB
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
from subprocess import run
|
|
from getpass import getuser
|
|
from os.path import isdir
|
|
from datetime import datetime
|
|
|
|
|
|
class SnapshotProvider:
|
|
def __init__(self, directory, git_dir):
|
|
author = f'{getuser()} via TFW {self.__class__.__name__}'
|
|
self.gitenv = {
|
|
'GIT_DIR': git_dir,
|
|
'GIT_WORK_TREE': directory,
|
|
'GIT_AUTHOR_NAME': author,
|
|
'GIT_AUTHOR_EMAIL': '',
|
|
'GIT_COMMITTER_NAME': author,
|
|
'GIT_COMMITTER_EMAIL': '',
|
|
'GIT_PAGER': 'cat'
|
|
}
|
|
|
|
self._check_environment()
|
|
|
|
self._head_detached = False
|
|
self._branch = 'master'
|
|
self._branches = [self._branch]
|
|
|
|
self._init_repo_if_needed()
|
|
|
|
def _check_environment(self):
|
|
if not isdir(self.gitenv['GIT_DIR']) or not isdir(self.gitenv['GIT_WORK_TREE']):
|
|
raise EnvironmentError('Directories "directory" and "git_dir" must exist!')
|
|
|
|
def _init_repo_if_needed(self):
|
|
if not self._repo_is_initialized():
|
|
self._run(('git', 'init'))
|
|
|
|
def _repo_is_initialized(self):
|
|
return self._run(('git', 'status')).returncode == 0
|
|
|
|
def take_snapshot(self):
|
|
if self._head_detached:
|
|
self._checkout_branch_from_head()
|
|
self._run(('git', 'add', '-A'))
|
|
self._run(('git', 'commit', '-m', 'Snapshot'))
|
|
|
|
def _checkout_branch_from_head(self):
|
|
head_hash = self._get_head_hash()
|
|
self._run((
|
|
'git', 'checkout',
|
|
'-b', head_hash, head_hash
|
|
))
|
|
self._branches.append(head_hash)
|
|
self._branch = head_hash
|
|
self._head_detached = False
|
|
|
|
def _get_head_hash(self):
|
|
return self._get_stdout(('git', 'rev-parse', 'HEAD'))
|
|
|
|
def restore_snapshot(self, date):
|
|
commit = self._get_commit_from_timestamp(date)
|
|
self._checkout(commit)
|
|
self._head_detached = True
|
|
|
|
def _get_commit_from_timestamp(self, date):
|
|
return self._get_stdout((
|
|
'git', 'rev-list',
|
|
'--date=iso',
|
|
'-n', '1',
|
|
f'--before="{date.isoformat()}"',
|
|
self._branch
|
|
))
|
|
|
|
def _checkout(self, what):
|
|
self._run((
|
|
'git', 'checkout',
|
|
what
|
|
))
|
|
|
|
def _get_stdout(self, *args, **kwargs):
|
|
kwargs['capture_output'] = True
|
|
stdout_bytes = self._run(*args, **kwargs).stdout
|
|
return stdout_bytes.decode().rstrip('\n')
|
|
|
|
def _run(self, *args, **kwargs):
|
|
if 'env' not in kwargs:
|
|
kwargs['env'] = self.gitenv
|
|
return run(*args, **kwargs)
|
|
|
|
@property
|
|
def all_timelines(self):
|
|
return self._branches
|
|
|
|
@property
|
|
def timeline(self):
|
|
return self._branch
|
|
|
|
@timeline.setter
|
|
def timeline(self, value):
|
|
self._checkout(value)
|
|
|
|
@property
|
|
def snapshots(self):
|
|
return self._pretty_log_branch()
|
|
|
|
def _pretty_log_branch(self):
|
|
git_log_output = self._get_stdout((
|
|
'git', 'log',
|
|
'--pretty=%H@%aI'
|
|
))
|
|
|
|
commits = []
|
|
for line in git_log_output.splitlines():
|
|
commit_hash, timestamp = line.split('@')
|
|
commits.append({
|
|
'hash': commit_hash,
|
|
'timestamp': datetime.fromisoformat(timestamp)
|
|
})
|
|
|
|
return commits
|