2023-02-24 09:46:43 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from pathlib import Path
|
2023-02-09 11:38:04 +00:00
|
|
|
import json
|
|
|
|
import re
|
2023-02-24 09:31:04 +00:00
|
|
|
import subprocess as sub
|
2023-02-09 11:38:04 +00:00
|
|
|
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 11:38:04 +00:00
|
|
|
class FileHandler(ABC):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_file_path(cls, file_path):
|
2023-02-09 12:33:56 +00:00
|
|
|
config_file_type = file_path.suffix
|
2023-02-09 11:38:04 +00:00
|
|
|
match config_file_type:
|
2023-02-09 12:33:56 +00:00
|
|
|
case '.json':
|
2023-02-09 11:38:04 +00:00
|
|
|
file_handler = JsonFileHandler()
|
2023-02-09 12:33:56 +00:00
|
|
|
case '.gradle':
|
2023-02-09 11:38:04 +00:00
|
|
|
file_handler = GradleFileHandler()
|
2023-02-09 12:56:45 +00:00
|
|
|
case '.clj':
|
2023-02-09 13:32:55 +00:00
|
|
|
file_handler = ClojureFileHandler()
|
|
|
|
case '.py':
|
|
|
|
file_handler = PythonFileHandler()
|
2023-02-09 11:38:04 +00:00
|
|
|
case _:
|
2023-02-24 09:46:43 +00:00
|
|
|
raise Exception(
|
|
|
|
f'The file type "{config_file_type}" is not implemented')
|
2023-02-09 11:38:04 +00:00
|
|
|
|
|
|
|
file_handler.config_file_path = file_path
|
|
|
|
file_handler.config_file_type = config_file_type
|
|
|
|
return file_handler
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def parse(self) -> tuple[list[int], bool]:
|
|
|
|
pass
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 11:38:04 +00:00
|
|
|
@abstractmethod
|
2023-02-09 12:20:42 +00:00
|
|
|
def write(self, version_string):
|
2023-02-09 11:38:04 +00:00
|
|
|
pass
|
|
|
|
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 11:38:04 +00:00
|
|
|
class JsonFileHandler(FileHandler):
|
|
|
|
|
|
|
|
def parse(self) -> tuple[list[int], bool]:
|
|
|
|
with open(self.config_file_path, 'r') as json_file:
|
|
|
|
json_version = json.load(json_file)['version']
|
|
|
|
is_snapshot = False
|
|
|
|
if '-SNAPSHOT' in json_version:
|
|
|
|
is_snapshot = True
|
|
|
|
json_version = json_version.replace('-SNAPSHOT', '')
|
|
|
|
version = [int(x) for x in json_version.split('.')]
|
|
|
|
return version, is_snapshot
|
|
|
|
|
2023-02-09 12:20:42 +00:00
|
|
|
def write(self, version_string):
|
2023-02-09 11:38:04 +00:00
|
|
|
with open(self.config_file_path, 'r+') as json_file:
|
|
|
|
json_data = json.load(json_file)
|
2023-02-09 12:20:42 +00:00
|
|
|
json_data['version'] = version_string
|
2023-02-24 09:46:43 +00:00
|
|
|
json_file.seek(0)
|
2023-02-09 11:38:04 +00:00
|
|
|
json.dump(json_data, json_file, indent=4)
|
|
|
|
json_file.truncate()
|
|
|
|
|
|
|
|
|
|
|
|
class GradleFileHandler(FileHandler):
|
|
|
|
|
|
|
|
def parse(self) -> tuple[list[int], bool]:
|
|
|
|
with open(self.config_file_path, 'r') as gradle_file:
|
|
|
|
contents = gradle_file.read()
|
2023-02-09 13:32:55 +00:00
|
|
|
version_line = re.search("\nversion = .*", contents)
|
2023-02-24 09:46:43 +00:00
|
|
|
exception = Exception("Version not found in gradle file")
|
2023-02-09 11:38:04 +00:00
|
|
|
if version_line is None:
|
2023-02-09 12:56:45 +00:00
|
|
|
raise exception
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 11:38:04 +00:00
|
|
|
version_line = version_line.group()
|
2023-02-24 09:46:43 +00:00
|
|
|
version_string = re.search(
|
|
|
|
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line)
|
2023-02-09 11:38:04 +00:00
|
|
|
if version_string is None:
|
2023-02-09 12:56:45 +00:00
|
|
|
raise exception
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 11:38:04 +00:00
|
|
|
version_string = version_string.group()
|
2023-02-09 11:52:58 +00:00
|
|
|
is_snapshot = False
|
2023-02-09 11:38:04 +00:00
|
|
|
if '-SNAPSHOT' in version_string:
|
2023-02-09 11:52:58 +00:00
|
|
|
is_snapshot = True
|
2023-02-09 11:38:04 +00:00
|
|
|
version_string = version_string.replace('-SNAPSHOT', '')
|
|
|
|
|
2023-02-09 11:52:58 +00:00
|
|
|
version = [int(x) for x in version_string.split('.')]
|
|
|
|
|
|
|
|
return version, is_snapshot
|
2023-02-09 11:38:04 +00:00
|
|
|
|
2023-02-09 12:20:42 +00:00
|
|
|
def write(self, version_string):
|
2023-02-09 11:38:04 +00:00
|
|
|
with open(self.config_file_path, 'r+') as gradle_file:
|
2023-02-09 13:32:55 +00:00
|
|
|
contents = gradle_file.read()
|
2023-02-24 09:46:43 +00:00
|
|
|
version_substitute = re.sub(
|
|
|
|
'\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', f'\nversion = "{version_string}"', contents)
|
2023-02-09 11:38:04 +00:00
|
|
|
gradle_file.seek(0)
|
|
|
|
gradle_file.write(version_substitute)
|
|
|
|
gradle_file.truncate()
|
2023-02-09 12:56:45 +00:00
|
|
|
|
2023-02-09 13:32:55 +00:00
|
|
|
|
|
|
|
class PythonFileHandler(FileHandler):
|
|
|
|
|
|
|
|
def parse(self) -> tuple[list[int], bool]:
|
|
|
|
with open(self.config_file_path, 'r') as python_file:
|
|
|
|
contents = python_file.read()
|
|
|
|
version_line = re.search("\nversion = .*\n", contents)
|
2023-02-24 09:46:43 +00:00
|
|
|
exception = Exception("Version not found in gradle file")
|
2023-02-09 13:32:55 +00:00
|
|
|
if version_line is None:
|
|
|
|
raise exception
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 13:32:55 +00:00
|
|
|
version_line = version_line.group()
|
2023-02-24 09:46:43 +00:00
|
|
|
version_string = re.search(
|
|
|
|
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line)
|
2023-02-09 13:32:55 +00:00
|
|
|
if version_string is None:
|
|
|
|
raise exception
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 13:32:55 +00:00
|
|
|
version_string = version_string.group()
|
|
|
|
is_snapshot = False
|
|
|
|
if '-SNAPSHOT' in version_string:
|
|
|
|
is_snapshot = True
|
|
|
|
version_string = version_string.replace('-SNAPSHOT', '')
|
|
|
|
|
|
|
|
version = [int(x) for x in version_string.split('.')]
|
|
|
|
|
|
|
|
return version, is_snapshot
|
|
|
|
|
|
|
|
def write(self, version_string):
|
|
|
|
with open(self.config_file_path, 'r+') as python_file:
|
|
|
|
contents = python_file.read()
|
2023-02-24 09:46:43 +00:00
|
|
|
version_substitute = re.sub(
|
|
|
|
'\nversion = "[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?"', f'\nversion = "{version_string}"', contents)
|
2023-02-09 13:32:55 +00:00
|
|
|
python_file.seek(0)
|
|
|
|
python_file.write(version_substitute)
|
|
|
|
python_file.truncate()
|
|
|
|
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 13:32:55 +00:00
|
|
|
class ClojureFileHandler(FileHandler):
|
2023-02-09 12:56:45 +00:00
|
|
|
|
|
|
|
def parse(self) -> tuple[list[int], bool]:
|
|
|
|
with open(self.config_file_path, 'r') as clj_file:
|
|
|
|
contents = clj_file.read()
|
|
|
|
version_line = re.search("^\\(defproject .*\n", contents)
|
2023-02-24 09:46:43 +00:00
|
|
|
exception = Exception("Version not found in clj file")
|
2023-02-09 12:56:45 +00:00
|
|
|
if version_line is None:
|
|
|
|
raise exception
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 12:56:45 +00:00
|
|
|
version_line = version_line.group()
|
2023-02-24 09:46:43 +00:00
|
|
|
version_string = re.search(
|
|
|
|
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line)
|
2023-02-09 12:56:45 +00:00
|
|
|
if version_string is None:
|
|
|
|
raise exception
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-09 12:56:45 +00:00
|
|
|
version_string = version_string.group()
|
|
|
|
is_snapshot = False
|
|
|
|
if '-SNAPSHOT' in version_string:
|
|
|
|
is_snapshot = True
|
|
|
|
version_string = version_string.replace('-SNAPSHOT', '')
|
|
|
|
|
|
|
|
version = [int(x) for x in version_string.split('.')]
|
|
|
|
|
|
|
|
return version, is_snapshot
|
|
|
|
|
|
|
|
def write(self, version_string):
|
|
|
|
with open(self.config_file_path, 'r+') as clj_file:
|
|
|
|
clj_first = clj_file.readline()
|
|
|
|
clj_rest = clj_file.read()
|
2023-02-24 09:46:43 +00:00
|
|
|
version_substitute = re.sub(
|
|
|
|
'[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', f'"{version_string}"\n', clj_first)
|
2023-02-09 12:56:45 +00:00
|
|
|
clj_file.seek(0)
|
|
|
|
clj_file.write(version_substitute)
|
|
|
|
clj_file.write(clj_rest)
|
2023-02-24 09:31:04 +00:00
|
|
|
clj_file.truncate()
|
|
|
|
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-02-24 09:31:04 +00:00
|
|
|
class SystemAPI():
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.stdout = [""]
|
|
|
|
self.stderr = [""]
|
|
|
|
|
|
|
|
def run(self, args):
|
|
|
|
stream = sub.Popen(args,
|
|
|
|
stdout=sub.PIPE,
|
|
|
|
stderr=sub.PIPE,
|
|
|
|
text=True,
|
|
|
|
encoding="UTF-8")
|
|
|
|
self.stdout = stream.stdout.readlines()
|
|
|
|
self.stderr = stream.stderr.readlines()
|
|
|
|
|
|
|
|
def run_checked(self, *args):
|
|
|
|
self.run(args)
|
|
|
|
|
|
|
|
if len(self.stderr) > 0:
|
|
|
|
raise Exception(f"Command failed with: {self.stderr}")
|
2023-02-24 09:46:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GitApi():
|
|
|
|
|
|
|
|
def __init__(self):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api = SystemAPI()
|
2023-02-24 09:46:43 +00:00
|
|
|
|
|
|
|
def get_latest_n_commits(self, n: int):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked(
|
2023-02-24 09:46:43 +00:00
|
|
|
'git', 'log', '--oneline', '--format="%s %b"', f'-n {n}')
|
2023-03-02 15:55:49 +00:00
|
|
|
return self.system_api.stdout
|
2023-02-24 09:46:43 +00:00
|
|
|
|
|
|
|
def get_latest_commit(self):
|
|
|
|
output = self.get_latest_n_commits(1)
|
|
|
|
return " ".join(output)
|
|
|
|
|
|
|
|
def tag_annotated(self, annotation: str, message: str, count: int):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked(
|
2023-02-24 09:46:43 +00:00
|
|
|
'git', 'tag', '-a', annotation, '-m', message, f'HEAD~{count}')
|
2023-03-02 15:55:49 +00:00
|
|
|
return self.system_api.stdout
|
|
|
|
|
|
|
|
def get_latest_tag(self):
|
|
|
|
self.system_api.run_checked('git', 'describe', '--tags', '--abbrev=0')
|
|
|
|
return self.system_api.stdout
|
2023-02-24 09:46:43 +00:00
|
|
|
|
|
|
|
def get_current_branch(self):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked('git', 'branch', '--show-current')
|
|
|
|
return ''.join(self.system_api.stdout).rstrip()
|
2023-02-24 09:46:43 +00:00
|
|
|
|
2023-03-02 13:31:15 +00:00
|
|
|
def init(self):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked('git', 'init')
|
2023-03-02 13:31:15 +00:00
|
|
|
|
2023-02-24 09:46:43 +00:00
|
|
|
def add_file(self, file_path: Path):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked('git', 'add', file_path)
|
|
|
|
return self.system_api.stdout
|
2023-02-24 09:46:43 +00:00
|
|
|
|
|
|
|
def commit(self, commit_message: str):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked(
|
2023-02-24 09:46:43 +00:00
|
|
|
'git', 'commit', '-m', commit_message)
|
2023-03-02 15:55:49 +00:00
|
|
|
return self.system_api.stdout
|
2023-02-24 09:46:43 +00:00
|
|
|
|
|
|
|
def push(self):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked('git', 'push')
|
|
|
|
return self.system_api.stdout
|
2023-02-24 09:46:43 +00:00
|
|
|
|
|
|
|
def checkout(self, branch: str):
|
2023-03-02 15:55:49 +00:00
|
|
|
self.system_api.run_checked('git', 'checkout', branch)
|
|
|
|
return self.system_api.stdout
|