2024-01-04 15:35:09 +00:00
|
|
|
import os
|
2024-07-11 11:37:26 +00:00
|
|
|
import subprocess
|
2024-05-16 16:23:35 +00:00
|
|
|
from contextlib import contextmanager
|
2024-01-04 15:35:09 +00:00
|
|
|
from pathlib import Path
|
2024-05-16 16:23:35 +00:00
|
|
|
from typing import Any, Iterator, List, Union
|
2024-01-04 15:35:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WithIter(type):
|
|
|
|
def __iter__(cls):
|
|
|
|
return (v for k, v in cls.__dict__.items() if not k.startswith("_"))
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def cd(path: Union[Path, str]) -> Iterator[None]:
|
|
|
|
oldpwd = os.getcwd()
|
|
|
|
os.chdir(path)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(oldpwd)
|
2024-02-02 17:10:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_hex(s):
|
|
|
|
try:
|
|
|
|
int(s, 16)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2024-02-04 19:12:37 +00:00
|
|
|
def normalize_string(string: str) -> str:
|
2024-06-02 16:25:14 +00:00
|
|
|
res = string.lower()
|
|
|
|
for r in ((" ", "_"), ("(", "_"), (")", "_"), (",", "_"), ("/", "_"), ("-", "_")):
|
|
|
|
res = res.replace(*r)
|
|
|
|
return res
|
2024-02-04 19:12:37 +00:00
|
|
|
|
|
|
|
|
2024-02-02 17:10:47 +00:00
|
|
|
class GHActions:
|
|
|
|
@staticmethod
|
2024-02-04 19:12:37 +00:00
|
|
|
def print_in_group(group_name: str, lines: Union[Any, List[Any]]) -> None:
|
2024-02-02 17:10:47 +00:00
|
|
|
lines = list(lines)
|
|
|
|
print(f"::group::{group_name}")
|
|
|
|
for line in lines:
|
|
|
|
print(line)
|
|
|
|
print("::endgroup::")
|
2024-07-11 11:37:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Shell:
|
|
|
|
@classmethod
|
|
|
|
def run_strict(cls, command):
|
|
|
|
subprocess.run(
|
|
|
|
command + " 2>&1",
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
text=True,
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, command):
|
|
|
|
res = ""
|
|
|
|
result = subprocess.run(
|
|
|
|
command,
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
text=True,
|
|
|
|
check=False,
|
|
|
|
)
|
|
|
|
if result.returncode == 0:
|
|
|
|
res = result.stdout
|
|
|
|
return res.strip()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def check(cls, command):
|
|
|
|
result = subprocess.run(
|
|
|
|
command + " 2>&1",
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
text=True,
|
|
|
|
check=False,
|
|
|
|
)
|
|
|
|
return result.returncode == 0
|