From 9e91491542ea619d17678a7d2a3b09f44eebb6d6 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Thu, 18 May 2023 14:23:08 +0200 Subject: [PATCH] clean up usage of python_util --- .../ddadevops/infrastructure/__init__.py | 1 - .../infrastructure/infrastructure.py | 31 +++++++++++-------- src/main/python/ddadevops/python_util.py | 13 ++++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/python/ddadevops/infrastructure/__init__.py b/src/main/python/ddadevops/infrastructure/__init__.py index 037a5df..ab663ee 100644 --- a/src/main/python/ddadevops/infrastructure/__init__.py +++ b/src/main/python/ddadevops/infrastructure/__init__.py @@ -3,7 +3,6 @@ from .infrastructure import ( ImageApi, ResourceApi, ExecutionApi, - ProjectRepository, EnvironmentApi, CredentialsApi, ) diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index 7f69e56..888afc4 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -1,17 +1,11 @@ from pathlib import Path from sys import stdout -from os import chmod -from subprocess import run +from os import chmod, environ from pkg_resources import resource_string import yaml -from os import environ import deprecation +from subprocess import check_output, Popen, PIPE, run from ..domain import Devops, Image, C4k, Release, BuildFile -from ..python_util import execute - - -class ProjectRepository: - pass class ResourceApi: @@ -20,15 +14,18 @@ class ResourceApi: class FileApi: + def __init__(self): + self.execution_api = ExecutionApi() + def clean_dir(self, directory: str): - execute("rm -rf " + directory, shell=True) - execute("mkdir -p " + directory, shell=True) + self.execution_api.execute("rm -rf " + directory) + self.execution_api.execute("mkdir -p " + directory) def cp_force(self, src: str, target_dir: str): - execute("cp -f " + src + "* " + target_dir, shell=True) + self.execution_api.execute("cp -f " + src + "* " + target_dir) def cp_recursive(self, src: str, target_dir: str): - execute("cp -r " + src + " " + target_dir, shell=True) + self.execution_api.execute("cp -r " + src + " " + target_dir) def write_data_to_file(self, path: Path, data: bytes): with open(path, "w", encoding="utf-8") as output_file: @@ -99,10 +96,18 @@ class ExecutionApi: if dry_run: print(command) else: - output = execute(command, True) + output = check_output(command, encoding="UTF-8", shell=True) + output = output.rstrip() print(output) return output + def execute_live(command): + process = Popen(command, stdout=PIPE) + for line in iter(process.stdout.readline, b""): + print(line.decode("utf-8"), end="") + process.stdout.close() + process.wait() + class EnvironmentApi: def get(self, key): diff --git a/src/main/python/ddadevops/python_util.py b/src/main/python/ddadevops/python_util.py index 204ee4d..e872ad6 100644 --- a/src/main/python/ddadevops/python_util.py +++ b/src/main/python/ddadevops/python_util.py @@ -1,15 +1,22 @@ from subprocess import check_output, Popen, PIPE +import deprecation + +@deprecation.deprecated(deprecated_in="3.2", details="use ExecutionApi instead") def execute(cmd, shell=False): - output = check_output(cmd, encoding='UTF-8', shell=shell) + output = check_output(cmd, encoding="UTF-8", shell=shell) return output.rstrip() + +@deprecation.deprecated(deprecated_in="3.2", details="use ExecutionApi instead") def execute_live(cmd): process = Popen(cmd, stdout=PIPE) - for line in iter(process.stdout.readline, b''): - print(line.decode('utf-8'), end='') + for line in iter(process.stdout.readline, b""): + print(line.decode("utf-8"), end="") process.stdout.close() process.wait() + +@deprecation.deprecated(deprecated_in="3.2", details="use domain.filter_none instead") def filter_none(list_to_filter): return [x for x in list_to_filter if x is not None]