class GitFile: def __init__(self, path): self.__path = path self.__new = True self.__git = Git(dirname(path)) def read(self): if exists(self.__path): with open(self.__path, 'r') as input: for line in input: line = line.strip() if line and not line.startswith('#'): epoch, sentence = line.split(' ', 1) yield int(epoch), sentence self.__new = False def __header(self): with open(self.__path, 'w') as output: output.write(''' # each non-comment line contains a unix epoch, followed by a sentence, # encoded in ascii, and terminated by a newline character (unix style). # the epoch is the time at which the sentence was created. obviously there # may be some delay before it is visible. sentences 'end' when the next one # is created. the gap is typically of order %d seconds, but is not # guaranteed (there is a random - stochastic - component as well as the # possibility of service downtimes, etc). # blank lines and those starting with a # are comments. ''' % PERIOD) def __push(self): try: self.__git.add(self.__path) self.__git.commit(self.__path, m='new sentence') self.__git.push() except GitException as e: eprint(e) def write(self, append, epoch, sentence): if self.__new: self.__header() self.__push() self.__new = False if append: with open(self.__path, 'a') as output: output.write("%d %s\n" % (epoch, sentence)) self.__push()
class GitCommandList: _repo = None _local_git_path = None _repo_url = None # to initialize the repository variable def init_git(self, local_git_path, repo_url): try: self._local_git_path = local_git_path self._repo_url = repo_url self._repo = Git(self._local_git_path, self._repo_url) self._repo.pull() return True except: return False # to make a commit, without doing a push on the remote repository on git def commit(self, message='Auto-commit: ' + str(datetime.now())): try: for file in local_tree(self._local_git_path): self._repo.add(file) self._repo.commit(message) return True except: return False # To push che files in the local repository. # A commit is generated automatically def push(self): try: self.commit() self._repo.pretty_log() r = self._repo.push() return '[*] Pushing at: ' + str(datetime.now()) + '\n' + r except: return False # to pull in the local from the remote repository def pull(self): try: self._repo.pretty_log() r = self._repo.pull() return '[*] Pulling at: ' + str(datetime.now()) + '\n' + r except Exception as e: print(e) return False