Commit 01c014c6 authored by Dominik Woiwode (pter)'s avatar Dominik Woiwode (pter)
Browse files

Use subprocess instead of os system for gitwrapper

parent 62291eb1
......@@ -55,7 +55,6 @@ class Config:
raise RuntimeError("Git is not configured correctly")
self._git = pterClasses.GitHelper(self.GIT_REPO, self.GIT_REPO_PATH)
self._git.force_pull()
self._git.config(self.GIT_EMAIL, self.GIT_NAME)
self._git.update()
self._git.config(self.GIT_EMAIL, self.GIT_NAME)
return self._git
import hashlib
import logging
import os
import smtplib
import subprocess
from dataclasses import dataclass
from email import encoders
from email.mime.base import MIMEBase
......@@ -8,7 +10,7 @@ from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from enum import Enum
from pathlib import Path
from typing import List
from typing import List, Union
@dataclass
......@@ -85,47 +87,57 @@ def send_fr_mail(text: str, subject: str, sender: MailParticipant, receivers: Li
class GitHelper:
def __init__(self, repository: str, repository_path: str):
def __init__(self, repository: str, repository_path: Union[str, Path]):
self.repository_url = repository
self.repository_path = repository_path
self.repository_path = Path(repository_path).absolute()
self.logger = logging.getLogger("GitHelper")
def _run_cmd(self, cmd):
self.logger.debug(f"Executing '{cmd}'")
proc = subprocess.run(cmd, cwd=str(self.repository_path))
if proc.returncode != 0:
print(proc.stderr)
raise RuntimeError(proc)
return 0
def config(self, email, name):
cmd = f'git -C {self.repository_path} config user.name {name}'
ret = os.system(cmd)
if ret != 0:
return ret
cmd = f'git -C {self.repository_path} config user.email {email}'
return os.system(cmd)
cmd = ["git", "config", "user.name", name]
self._run_cmd(cmd)
cmd = ["git", "config", "user.email", email]
self._run_cmd(cmd)
def clone(self):
cmd = f'git clone {self.repository_url} {self.repository_path}'
return os.system(cmd)
cmd = ["git", "clone", self.repository_url, self.repository_path]
proc = subprocess.run(cmd, cwd=str(self.repository_path.parent))
if proc.returncode != 0:
raise RuntimeError(proc)
return 0
def force_pull(self):
cmd = f'git -C {self.repository_path} fetch origin master'
ret = os.system(cmd)
if ret != 0:
return ret
cmd = f'git -C {self.repository_path} reset --hard origin/master'
return os.system(cmd)
cmd = ["git", "fetch", "origin", "master"]
self._run_cmd(cmd)
cmd = ["git", "reset", "--hard", "origin/master"]
self._run_cmd(cmd)
def update(self):
if os.path.exists(os.path.join(self.repository_path, "/.git")):
if (self.repository_path / ".git").exists():
return self.force_pull()
else:
return self.clone()
def add(self, file: str):
cmd = f'git -C {self.repository_path} add {file}'
return os.system(cmd)
cmd = ["git", "add", file]
self._run_cmd(cmd)
def commit(self, msg: str):
cmd = f'git -C {self.repository_path} commit -m \"{msg}\"'
return os.system(cmd)
cmd = ["git", "commit", "-m", msg]
self._run_cmd(cmd)
def push(self):
cmd = f'git -C {self.repository_path} push origin master'
return os.system(cmd)
cmd = ["git", "push", "origin", "master"]
self._run_cmd(cmd)
class SemesterPart(Enum):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment