blob: 3fce19cb89cf0ee835d5eec4f44661194562fe22 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Module to host all file system related functions."""
from __future__ import annotations
import re
import shutil
from pathlib import Path
from typing import Literal
from mlia.utils.filesystem import get_mlia_resources
ResourceType = Literal["applications", "systems"]
def get_backend_resources() -> Path:
"""Get backend resources folder path."""
return get_mlia_resources() / "backends"
def get_backends_path(name: ResourceType) -> Path:
"""Return the absolute path of the specified resource.
It uses importlib to return resources packaged with MANIFEST.in.
"""
if not name:
raise ResourceWarning("Resource name is not provided")
resource_path = get_backend_resources() / name
if resource_path.is_dir():
return resource_path
raise ResourceWarning(f"Resource '{name}' not found.")
def copy_directory_content(source: Path, destination: Path) -> None:
"""Copy content of the source directory into destination directory."""
for item in source.iterdir():
src = source / item.name
dest = destination / item.name
if src.is_dir():
shutil.copytree(src, dest)
else:
shutil.copy2(src, dest)
def remove_resource(resource_directory: str, resource_type: ResourceType) -> None:
"""Remove resource data."""
resources = get_backends_path(resource_type)
resource_location = resources / resource_directory
if not resource_location.exists():
raise Exception(f"Resource {resource_directory} does not exist")
if not resource_location.is_dir():
raise Exception(f"Wrong resource {resource_directory}")
shutil.rmtree(resource_location)
def remove_directory(directory_path: Path | None) -> None:
"""Remove directory."""
if not directory_path or not directory_path.is_dir():
raise Exception("No directory path provided")
shutil.rmtree(directory_path)
def recreate_directory(directory_path: Path | None) -> None:
"""Recreate directory."""
if not directory_path:
raise Exception("No directory path provided")
if directory_path.exists() and not directory_path.is_dir():
raise Exception(
f"Path {str(directory_path)} does exist and it is not a directory."
)
if directory_path.is_dir():
remove_directory(directory_path)
directory_path.mkdir()
def valid_for_filename(value: str, replacement: str = "") -> str:
"""Replace non alpha numeric characters."""
return re.sub(r"[^\w.]", replacement, value, flags=re.ASCII)
|