clean up usage of python_util

This commit is contained in:
Michael Jerger 2023-05-18 14:23:08 +02:00
parent d16a022728
commit 9e91491542
3 changed files with 28 additions and 17 deletions

View file

@ -3,7 +3,6 @@ from .infrastructure import (
ImageApi,
ResourceApi,
ExecutionApi,
ProjectRepository,
EnvironmentApi,
CredentialsApi,
)

View file

@ -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):

View file

@ -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]