1"""Misc. utility functions"""
9from pathlib
import Path
10from os.path
import join
as join_path
16 """Encodes the last generated log map into a z-lib compressed C binary array
19 path (str): Path to store the C file containing the array
21 print(
"Encoding LogMap 📃\n")
29 f
"{path}\\log_lookup.cpp",
37 """Checks for missing or uninitalized submodules within project
40 file_types (str): File types to expect in submodules
44 response = subprocess.check_output(
"git config -f .gitmodules -l", stderr=subprocess.STDOUT, shell=
True)
45 submodules = tuple(line.split(
"=")[1]
for line
in response.decode(
"utf-8").splitlines()
if ".url=" not in line)
46 for module
in submodules:
48 not os.path.exists(module)
49 or not os.path.isdir(module)
50 or not list(1
for ext
in file_types
if len(glob.glob(f
"**{os.path.sep}*{ext}", root_dir=module, recursive=
True)))
52 print(Text.warning(
"Submodule does not exist, or contains no source files : " + module))
54 except subprocess.CalledProcessError:
55 print(Text.error(
"Failed to check for git submodules"))
57 print(Text.important(
"\nConsider running " + Text.red(
"git pull --recurse-submodules")))
58 print(Text.important(
"or " + Text.red(
"git submodule update --init")) + Text.important(
" if repo has just been cloned\n"))
61LIB_BLACKLIST = join_path(
"libraries",
".blacklist")
65 """Get the library folder blacklist based on core model
68 dict[str, list]: folder blacklist as a dict
70 blacklist: dict[str, list] = {}
71 with open(LIB_BLACKLIST,
"r", encoding=
"utf-8")
as file:
73 for line
in file.readlines():
75 currentModel = line.split(
" ")[0][1:]
76 if currentModel
not in blacklist:
77 blacklist[currentModel] = []
79 for token
in line.split(
" "):
80 token = token.strip(
" \n")
81 if not token
or token[0] ==
"#":
83 elif os.path.exists(join_path(os.path.dirname(LIB_BLACKLIST), token)):
84 blacklist[currentModel].append(join_path(os.path.dirname(LIB_BLACKLIST), token))
91def sync_file(filePath: str, offset: str, rawpath: str, workingFilePath: str =
None, suppress: bool =
False) -> bool:
92 """Syncs a file between directories
95 filePath (str): Path to the original file
96 offset (str): Path offset to prepend to the rawpath to get the filepath
97 rawpath (str): Path to the directory the original file is in
98 workingFilePath (str, optional): Path to the file to sync to. Defaults to offset + filepath.
99 suppress (bool, optional): Suppress log messages. Defaults to False.
102 bool: Whether the file was synced
104 workingFilePath = workingFilePath
or f
"{offset}{filePath}"
111 if not os.path.exists(workingFilePath)
or new != old:
114 touch(f
"{offset}{rawpath}")
115 shutil.copyfile(filePath, workingFilePath)
117 print(f
"Sync File: {os.path.basename(workingFilePath)}")
123 """Creates a directory tree
126 rawpath (str): Path to generate
129 Path(rawpath).mkdir(parents=
True, exist_ok=
True)
130 except OSError
as exc:
131 if exc.errno != errno.EEXIST:
139 """Get The amount of RAM available in GBs
146 out = subprocess.check_output(
"wmic OS get FreePhysicalMemory /Value", stderr=subprocess.STDOUT, shell=
True)
148 int(str(out).strip(
"b").strip(
"'").replace(
"\\r",
"").replace(
"\\n",
"").replace(
"FreePhysicalMemory=",
"")) / 1048576, 2
161 filePath (str): Path to the file to hash
164 str: HEX string of the file's hash
166 if os.path.exists(filePath):
168 sha256 = hashlib.sha256()
169 with open(filePath,
"rb")
as f:
171 data = f.read(BUF_SIZE)
175 return sha256.digest()
177 with open(filePath,
"rb")
as f:
178 return hashlib.sha256(f.read()).hexdigest()
str hashFile(str filePath)
Hashes a file.
None check_git_submodules(str file_types)
Checks for missing or uninitalized submodules within project.
None touch(str rawpath)
Creates a directory tree.
bool available_ram()
Get The amount of RAM available in GBs.
dict[str, list] get_library_blacklist()
Get the library folder blacklist based on core model.
None encode_log_map(str path)
Encodes the last generated log map into a z-lib compressed C binary array.
bool sync_file(str filePath, str offset, str rawpath, str workingFilePath=None, bool suppress=False)
Syncs a file between directories.