[#350] Move file-related functions to file_helper

Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
This commit is contained in:
Vladimir Domnich 2022-10-11 15:15:50 +00:00 committed by Vladimir
parent ce41104d3a
commit 5eeb8b4058
6 changed files with 153 additions and 93 deletions

View file

@ -1,64 +1,33 @@
import os
import time
import uuid
import allure
from common import ASSETS_DIR, SIMPLE_OBJ_SIZE, STORAGE_GC_TIME
def create_file_with_content(file_path: str = None, content: str = None) -> str:
mode = "w+"
if not content:
content = os.urandom(SIMPLE_OBJ_SIZE)
mode = "wb"
if not file_path:
file_path = f"{os.getcwd()}/{ASSETS_DIR}/{str(uuid.uuid4())}"
else:
if not os.path.exists(os.path.dirname(file_path)):
os.makedirs(os.path.dirname(file_path))
with open(file_path, mode) as out_file:
out_file.write(content)
return file_path
def get_file_content(file_path: str, content_len: int = None, mode="r") -> str:
with open(file_path, mode) as out_file:
if content_len:
content = out_file.read(content_len)
else:
content = out_file.read()
return content
def split_file(file_path: str, parts: int) -> list[str]:
files = []
with open(file_path, "rb") as in_file:
data = in_file.read()
content_size = len(data)
chunk_size = int((content_size + parts) / parts)
part_id = 1
for start_position in range(0, content_size + 1, chunk_size):
part_file_name = f"{file_path}_part_{part_id}"
files.append(part_file_name)
with open(part_file_name, "wb") as out_file:
out_file.write(data[start_position : start_position + chunk_size])
part_id += 1
return files
from common import STORAGE_GC_TIME
def parse_time(value: str) -> int:
if value.endswith("s"):
return int(value[:-1])
"""Converts time interval in text form into time interval as number of seconds.
if value.endswith("m"):
return int(value[:-1]) * 60
Args:
value: time interval as text.
Returns:
Number of seconds in the parsed time interval.
"""
value = value.lower()
for suffix in ["s", "sec"]:
if value.endswith(suffix):
return int(value[: -len(suffix)])
for suffix in ["m", "min"]:
if value.endswith(suffix):
return int(value[: -len(suffix)]) * 60
for suffix in ["h", "hr", "hour"]:
if value.endswith(suffix):
return int(value[: -len(suffix)]) * 60 * 60
raise ValueError(f"Unknown units in time value '{value}'")
def placement_policy_from_container(container_info: str) -> str:
@ -88,7 +57,6 @@ def placement_policy_from_container(container_info: str) -> str:
def wait_for_gc_pass_on_storage_nodes() -> None:
# We add 15 seconds to allow some time for GC process itself
wait_time = parse_time(STORAGE_GC_TIME)
with allure.step(f"Wait {wait_time}s until GC completes on storage nodes"):
time.sleep(wait_time)