From 233c11efec570839c5942966be08fae0f3d45bc4 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Tue, 16 Jan 2024 20:45:06 +0300 Subject: [PATCH] wip --- .env.example | 1 + autorepacked/base_provider.py | 68 +++++++++++++++-------------------- autorepacked/main.py | 67 ++++++++++++++++++++++++++++++++++ autorepacked/utils.py | 9 +++++ 4 files changed, 106 insertions(+), 39 deletions(-) create mode 100644 autorepacked/main.py create mode 100644 autorepacked/utils.py diff --git a/.env.example b/.env.example index e69de29..6de76cd 100644 --- a/.env.example +++ b/.env.example @@ -0,0 +1 @@ +REPO_PATH= \ No newline at end of file diff --git a/autorepacked/base_provider.py b/autorepacked/base_provider.py index c0a09a4..ffddd56 100644 --- a/autorepacked/base_provider.py +++ b/autorepacked/base_provider.py @@ -1,15 +1,17 @@ import os import shutil -import subprocess import tempfile import re +from autorepacked import utils + def _get_file(directory): files_and_dirs = os.listdir(directory) files = [f for f in files_and_dirs if os.path.isfile(os.path.join(directory, f))] return os.path.join(directory, files[0]) if files else None + class BaseProvider: def __init__(self, config): self.config = config @@ -21,14 +23,14 @@ class BaseProvider: return self._name def get_version(self): - url = subprocess.run([ + url = utils.run([ 'epm', '--silent', 'tool', 'eget', '--get-real-url', self.get_download_url(), - ], stdout=subprocess.PIPE).stdout.decode('utf-8') + ]) pattern = r'\d+\.\d+\.\d+' match = re.search(pattern, url) @@ -44,48 +46,36 @@ class BaseProvider: download_directory = tempfile.mkdtemp() repacked_directory = tempfile.mkdtemp() - subprocess.run( - args=[ - 'epm', - '--silent', - 'tool', - 'eget', - '-q', - self.get_download_url(), - ], - stdout=subprocess.PIPE, - cwd=download_directory, - ).stdout.decode('utf-8') + utils.run([ + 'epm', + '--silent', + 'tool', + 'eget', + '-q', + self.get_download_url(), + ], cwd=download_directory) file = _get_file(download_directory) - subprocess.run( - args=[ - 'epm', - '--silent', - '-y', - 'repack', - file, - ], - stdout=subprocess.PIPE, - cwd=repacked_directory, - ).stdout.decode('utf-8') + utils.run([ + 'epm', + '--silent', + '-y', + 'repack', + file, + ], cwd=repacked_directory) shutil.rmtree(download_directory) file = _get_file(repacked_directory) - subprocess.run( - args=[ - 'epm', - '--silent', - 'repo', - 'pkgadd', - self.config.get('repo_path'), - file, - ], - stdout=subprocess.PIPE, - cwd=repacked_directory, - ).stdout.decode('utf-8') + utils.run([ + 'epm', + '--silent', + 'repo', + 'pkgadd', + self.config.get('repo_path'), + file, + ], cwd=repacked_directory) - shutil.rmtree(repacked_directory) \ No newline at end of file + shutil.rmtree(repacked_directory) diff --git a/autorepacked/main.py b/autorepacked/main.py new file mode 100644 index 0000000..64a7079 --- /dev/null +++ b/autorepacked/main.py @@ -0,0 +1,67 @@ +import json +import os +import importlib + +from autorepacked import utils +from autorepacked.base_provider import BaseProvider +from autorepacked.config import Config + + +def create_repo(config): + utils.run([ + 'epm', + 'repo', + 'create', + config.get('repo_path') + ]) + + +def main(): + config = Config() + + create_repo(config) + + providers_path = os.path.join(os.getcwd(), 'autorepacked/providers') + modules = os.listdir(providers_path) + + json_file_path = os.path.join(config.get('repo_path'), 'data.json') + if not os.path.exists(json_file_path): + with open(json_file_path, 'w') as data_file: + json.dump({'versions': {}}, data_file, indent=4) + + with open(json_file_path, 'r') as data_file: + repo_data = json.load(data_file) + + versions = repo_data['versions'] + need_update_index = False + for module_name in modules: + module = importlib.import_module(f'providers.{module_name}') + if hasattr(module, 'get_provider'): + provider = module.get_provider(config) # type: BaseProvider + name = provider.get_name() + print(f"request version of {name}") + version = provider.get_version() + if name not in versions or versions[name] != version: + print(f'{name} has new version {version}') + versions[name] = version + provider.download() + need_update_index = True + + repo_data['versions'] = versions + + with open(json_file_path, 'w') as data_file: + json.dump(repo_data, data_file, indent=4) + + if need_update_index: + utils.run( + args=[ + 'epm', + 'repo', + 'index', + config.get('repo_path'), + ] + ) + + +if __name__ == "__main__": + main() diff --git a/autorepacked/utils.py b/autorepacked/utils.py new file mode 100644 index 0000000..34dca5f --- /dev/null +++ b/autorepacked/utils.py @@ -0,0 +1,9 @@ +import subprocess + + +def run(args, cwd=None): + return subprocess.run( + args=args, + cwd=cwd, + stdout=subprocess.PIPE, + ).stdout.decode('utf-8')