From 2a214ff0baf373f15958bd8d4c9e45172c1b0809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 22 Jun 2018 14:27:25 +0200 Subject: [PATCH] tmp --- benchmark-new/benchmark.py | 22 ++++++++ benchmark-new/benchmark_repository.py | 81 +++++++++++++++++++++++++++ benchmark-new/config.yml | 21 +++++++ 3 files changed, 124 insertions(+) create mode 100755 benchmark-new/benchmark.py create mode 100755 benchmark-new/benchmark_repository.py create mode 100644 benchmark-new/config.yml diff --git a/benchmark-new/benchmark.py b/benchmark-new/benchmark.py new file mode 100755 index 000000000..750334f0f --- /dev/null +++ b/benchmark-new/benchmark.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +import subprocess +import yaml + +from benchmark_repository import BenchmarkRepository + +def executeCommand(command, stdin = None, cwd = None): + with subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = (subprocess.PIPE if stdin != None else None), cwd = cwd) as process: + stdout, stderr = process.communicate(input = (stdin.encode("utf-8") if stdin != None else None)) + exitCode = process.returncode + + return stdout.decode("utf-8"), stderr.decode("utf-8"), exitCode + +def main(): + with open("config.yml", "r") as stream: + config = yaml.load(stream, Loader = yaml.CLoader) + + benchmarkRepository = BenchmarkRepository(config["repository"]) + +if __name__ == "__main__": + main() diff --git a/benchmark-new/benchmark_repository.py b/benchmark-new/benchmark_repository.py new file mode 100755 index 000000000..b18b463b5 --- /dev/null +++ b/benchmark-new/benchmark_repository.py @@ -0,0 +1,81 @@ +#!/usr/bin/python3 + +import math +import os +import pygit2 +import shutil +import sys + +def printProgressBar(title, value, total, overwrite = False): + terminalSize = shutil.get_terminal_size((80, 20)) + progress = value / total if total > 0 else 1 + progressText = "{: >3d} %".format(math.floor(progress * 100)) + remainingWidth = terminalSize.columns - len(title) - len(progressText) - 4 + progressBarWidth = min(remainingWidth, 94) + blankWidth = remainingWidth - progressBarWidth + filledWidth = math.floor(progressBarWidth * progress) + progressBar = "#" * filledWidth + "-" * (progressBarWidth - filledWidth) + + if overwrite: + print("\x1b[1A\x1b[2K", end = "") + + print("{} {}[{}] {}".format(title, " " * blankWidth, progressBar, progressText)) + +def printCloneProgress(transferProgress): + printProgressBar("Cloning the repository", transferProgress.received_objects, transferProgress.total_objects, overwrite = True) + +def printFetchProgress(transferProgress): + printProgressBar("Updating the repository", transferProgress.received_objects, transferProgress.total_objects, overwrite = True) + +class BenchmarkRepository: + def __init__(self, config): + self.basePath = config["basePath"] + self.remote = config["remote"] + self.remoteUser = config["remoteUser"] + self.userName = config["userName"] + self.userEMail = config["userEMail"] + self.userSigningKey = config["userSigningKey"] if "userSigningKey" in config else None + self.branches = config["branches"] if "branches" in config else {"master": "master", "results": "results", "config": "config", "status": "status"} + self.statusLogSize = config["statusLogSize"] + + remoteUser = pygit2.credentials.KeypairFromAgent(self.remoteUser) + callbacks = pygit2.RemoteCallbacks(credentials = remoteUser) + + # check for an existing repository + if os.path.isdir(self.branchDir("master")): + repositoryPath = pygit2.discover_repository(self.branchDir("master")) + + if repositoryPath is None: + raise Exception("{} exists but is not a Git directory".format(self.branchDir("master"))) + + callbacks.transfer_progress = printFetchProgress + + self.repository = pygit2.Repository(repositoryPath) + + for remote in self.repository.remotes: + printProgressBar("Updating the repository", 0, 1) + remote.fetch(callbacks = callbacks) + printProgressBar("Updating the repository", 1, 1, overwrite = True) + + try: + pass + #self.repository.lookup_worktree("results") + #self.repository.lookup_worktree("config") + #self.repository.lookup_worktree("status") + except pygit2.GitError: + raise Exception("Worktrees are not properly set up") + else: + callbacks.transfer_progress = printCloneProgress + + printProgressBar("Cloning the repository", 0, 1) + self.repository = pygit2.clone_repository(self.remote, self.branchDir("master"), callbacks = callbacks, checkout_branch = self.branches["master"]) + printProgressBar("Cloning the repository", 1, 1, overwrite = True) + + for branchName in ["results", "config", "status"]: + worktree = self.repository.add_worktree(branchName, self.branchDir(branchName)) + worktree = pygit2.Repository(worktree.path) + branch = worktree.branches.local.create(branchName) + #worktree.checkout(self.branches["results"]) + + def branchDir(self, branchName): + return os.path.join(self.basePath, branchName) diff --git a/benchmark-new/config.yml b/benchmark-new/config.yml new file mode 100644 index 000000000..333c499ae --- /dev/null +++ b/benchmark-new/config.yml @@ -0,0 +1,21 @@ +repository: + # local directory where remote is cloned to + basePath: storage + # repote Git repository URL + remote: git@git.luehne.de:patrick/tplp-planning-benchmark.git + # user name of the SSH key pair with access to the remote + remoteUser: git + # user name for commits + userName: potassco-bot + # user e-mail for commits + userEMail: bot@potassco.org + # user GPG key for signing commits (optional) + # userSigningKey: 12345678 + # data branches + branches: + master: master + results: test-results + config: test-config + status: test-status + # the maximum size of the status log in lines + statusLogSize: 100