ScriptCollection 4.3.64__py3-none-any.whl → 4.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. ScriptCollection/AnionBuildPlatform.py +205 -205
  2. ScriptCollection/CertificateUpdater.py +157 -157
  3. ScriptCollection/Executables.py +4 -2
  4. ScriptCollection/GeneralUtilities.py +1696 -1696
  5. ScriptCollection/OCIImages/OCIImageManager.py +278 -278
  6. ScriptCollection/ProcessesRunner.py +43 -43
  7. ScriptCollection/ProgramRunnerSudo.py +110 -110
  8. ScriptCollection/Resources/CultureChooser/CultureChooser.js +0 -0
  9. ScriptCollection/Resources/CultureChooser/index.html +0 -0
  10. ScriptCollection/Resources/Dependencies/JRE/Version.txt +0 -0
  11. ScriptCollection/Resources/Dependencies/OpenAPIGenerator/Version.txt +0 -0
  12. ScriptCollection/Resources/Dependencies/PlantUML/Version.txt +0 -0
  13. ScriptCollection/Resources/Fonts/DejaVuSans-Bold.ttf +0 -0
  14. ScriptCollection/Resources/Fonts/DejaVuSans-BoldOblique.ttf +0 -0
  15. ScriptCollection/Resources/Fonts/DejaVuSans-Oblique.ttf +0 -0
  16. ScriptCollection/Resources/Fonts/DejaVuSans.ttf +0 -0
  17. ScriptCollection/Resources/Fonts/LICENSE.txt +0 -0
  18. ScriptCollection/Resources/MaintenanceSite/MaintenanceSite.html +0 -0
  19. ScriptCollection/SCLog.py +115 -115
  20. ScriptCollection/ScriptCollectionCore.py +4372 -4351
  21. ScriptCollection/TFCPS/DotNet/TFCPS_CodeUnitSpecific_DotNet.py +716 -709
  22. ScriptCollection/TFCPS/NodeJS/TFCPS_CodeUnitSpecific_NodeJS.py +235 -235
  23. ScriptCollection/TFCPS/Python/TFCPS_CodeUnitSpecific_Python.py +258 -258
  24. ScriptCollection/TFCPS/TFCPS_CodeUnitSpecific_Base.py +524 -524
  25. ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnit.py +157 -155
  26. ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnits.py +691 -681
  27. ScriptCollection/TFCPS/TFCPS_CreateRelease.py +98 -98
  28. ScriptCollection/TFCPS/TFCPS_Generic.py +44 -44
  29. ScriptCollection/TFCPS/TFCPS_MergeToMain.py +132 -132
  30. ScriptCollection/TFCPS/TFCPS_MergeToStable.py +358 -358
  31. ScriptCollection/TFCPS/TFCPS_PreBuildCodeunitsScript.py +48 -48
  32. ScriptCollection/TFCPS/TFCPS_Tools_General.py +30 -4
  33. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/METADATA +1 -1
  34. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/RECORD +26 -26
  35. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/WHEEL +0 -0
  36. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/entry_points.txt +0 -0
  37. {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.0.dist-info}/top_level.txt +0 -0
@@ -1,1696 +1,1696 @@
1
- import re
2
- import os
3
- from os import listdir
4
- from os.path import isfile, join, isdir
5
- import platform
6
- import tempfile
7
- import json
8
- import inspect
9
- import ctypes
10
- import hashlib
11
- import subprocess
12
- import shutil
13
- import time
14
- import urllib
15
- import stat
16
- import fnmatch
17
- import secrets
18
- import string as strin
19
- import sys
20
- from importlib import resources
21
- from enum import Enum
22
- import traceback
23
- import warnings
24
- import functools
25
- from pathlib import Path
26
- from datetime import datetime, timedelta, date, timezone
27
- import typing
28
- from packaging.version import Version
29
- import psutil
30
- from defusedxml.minidom import parse
31
- from OpenSSL import crypto
32
-
33
- class VersionEcholon(Enum):
34
- LatestPatch = 0
35
- LatestPatchOrLatestMinor = 1
36
- LatestPatchOrLatestMinorOrNextMajor = 2
37
- LatestVersion = 3
38
- CustomAlgorithm = 4
39
- NoUpdate = 5
40
-
41
- class Platform(Enum):
42
- Windows_AMD64 = 0
43
- Linux_AMD64 = 1
44
- Linux_ARM64 = 2
45
- MacOS_ARM64 = 3
46
-
47
-
48
- class Dependency:
49
- dependencyname:str = None
50
- current_version:str = None
51
-
52
- def __init__(self,dependencyname:str,current_version:str):
53
- self.dependencyname=dependencyname
54
- self.current_version=current_version
55
-
56
- class GeneralUtilities:
57
-
58
- empty_string: str = ""
59
-
60
- @staticmethod
61
- def get_modest_dark_url() -> str:
62
- return "https://aniondev.github.io/CDN/ScriptCollectionDesigns/ModestDark/Style.css"
63
-
64
- @staticmethod
65
- def is_generic(t: typing.Type):
66
- return hasattr(t, "__origin__")
67
-
68
- @staticmethod
69
- def is_debugger_attached():
70
- return sys.gettrace() is not None
71
-
72
- @staticmethod
73
- def check_arguments(function):
74
- def __check_function(*args, **named_args):
75
- parameters: list = inspect.getfullargspec(function)[0].copy()
76
- arguments: list = list(tuple(args)).copy()
77
- if "self" in parameters:
78
- parameters.remove("self")
79
- arguments.pop(0)
80
- for index, argument in enumerate(arguments):
81
- if argument is not None: # Check type of None is not possible. None is always a valid argument-value
82
- if parameters[index] in function.__annotations__: # Check if a type-hint for parameter exist. If not, no parameter-type available for argument-type-check
83
- # Check type of arguments if the type is a generic type seems to be impossible.
84
- if not GeneralUtilities.is_generic(function.__annotations__[parameters[index]]):
85
- if not isinstance(argument, function.__annotations__[parameters[index]]):
86
- raise TypeError(f"Argument with index {index} for function {function.__name__} ('{str(argument)}') is not of type {function.__annotations__[parameters[index]]} but has type "+str(type(argument)))
87
- for index, named_argument in enumerate(named_args):
88
- if named_args[named_argument] is not None:
89
- if named_argument in function.__annotations__:
90
- if not GeneralUtilities.is_generic(function.__annotations__.get(named_argument)):
91
- if not isinstance(named_args[named_argument], function.__annotations__.get(named_argument)):
92
- raise TypeError(f"Argument with name {named_argument} for function {function.__name__} ('{str(named_args[named_argument])}') is not of type {function.__annotations__.get(named_argument)}")
93
- return function(*args, **named_args)
94
- __check_function.__doc__ = function.__doc__
95
- return __check_function
96
-
97
- @staticmethod
98
- def deprecated(reason: str=None):
99
- def decorator(target):
100
- if isinstance(target, type):
101
- # target is a class: wrap its __init__ so the warning is emitted on instantiation while target stays a class.
102
- original_init = target.__init__
103
-
104
- @functools.wraps(original_init)
105
- def init_wrapper(self, *args, **kwargs):
106
- msg = f"Class {target.__name__} is deprecated."
107
- if GeneralUtilities.string_has_content(reason):
108
- msg += f" {reason}"
109
- warnings.warn(
110
- msg,
111
- category=DeprecationWarning,
112
- stacklevel=2
113
- )
114
- return original_init(self, *args, **kwargs)
115
- target.__init__ = init_wrapper
116
- return target
117
-
118
- @functools.wraps(target)
119
- def wrapper(*args, **kwargs):
120
- msg = f"Function {target.__name__}() is deprecated."
121
- if GeneralUtilities.string_has_content(reason):
122
- msg += f" {reason}"
123
- warnings.warn(
124
- msg,
125
- category=DeprecationWarning,
126
- stacklevel=2
127
- )
128
- return target(*args, **kwargs)
129
- return wrapper
130
- return decorator
131
-
132
- @staticmethod
133
- @check_arguments
134
- def args_array_surround_with_quotes_if_required(arguments: list[str]) -> list[str]:
135
- result = []
136
- for argument in arguments:
137
- if " " in argument and not (argument.startswith('"') and argument.endswith('"')):
138
- result.append(f'"{argument}"')
139
- else:
140
- result.append(argument)
141
- return result
142
-
143
- @staticmethod
144
- @check_arguments
145
- def string_to_lines(string: str, add_empty_lines: bool = True, adapt_lines: bool = True) -> list[str]:
146
- result : list[str] = list[str]()
147
- lines : list[str] = list[str]()
148
- if (string is not None):
149
- if ("\n" in string):
150
- lines = string.split("\n")
151
- else:
152
- lines.append(string)
153
- for rawline in lines:
154
- if adapt_lines:
155
- line = rawline.replace("\r", "").strip()
156
- else:
157
- line = rawline
158
- if GeneralUtilities.string_is_none_or_whitespace(line):
159
- if add_empty_lines:
160
- result.append(line)
161
- else:
162
- result.append(line)
163
- return result
164
-
165
- @staticmethod
166
- @check_arguments
167
- def string_to_datetime(value: str) -> datetime:
168
- """expects a value in the format 2022-10-06T19:26:01"""
169
- if "." in value:
170
- value = value.split(".")[0]
171
- return datetime.strptime(value,"%Y-%m-%dT%H:%M:%S")
172
-
173
- @staticmethod
174
- @check_arguments
175
- def datetime_to_string(value: datetime) -> str:
176
- """returns a value in the format 2022-10-06T19:26:01"""
177
- return value.strftime("%Y-%m-%dT%H:%M:%S")
178
-
179
- @staticmethod
180
- @check_arguments
181
- def string_to_date(value: str) -> date:
182
- """expects a value in the format 2022-10-06"""
183
- splitted = value.split("-")
184
- return date(int(splitted[0]), int(splitted[1]), int(splitted[2]))
185
-
186
- @staticmethod
187
- @check_arguments
188
- def date_to_string(value: date) -> str:
189
- """returns a value in the format 2022-10-06"""
190
- return value.strftime("%Y-%m-%d")
191
-
192
- @staticmethod
193
- @check_arguments
194
- def copy_content_of_folder(source_directory: str, target_directory: str, overwrite_existing_files=False, ignored_glob_patterms: list[str] = None) -> None:
195
- GeneralUtilities.__copy_or_move_content_of_folder(source_directory, target_directory, overwrite_existing_files, False, ignored_glob_patterms)
196
-
197
- @staticmethod
198
- @check_arguments
199
- def move_content_of_folder(source_directory: str, target_directory: str, overwrite_existing_files=False, ignored_glob_patterms: list[str] = None) -> None:
200
- GeneralUtilities.__copy_or_move_content_of_folder(source_directory, target_directory, overwrite_existing_files, True, ignored_glob_patterms)
201
-
202
- @staticmethod
203
- @check_arguments
204
- def merge_dependency_lists(versions:list[list[Dependency]]) -> dict[str,set[str]]:
205
- result:dict[str,set[str]]=dict[str,set[str]]()
206
- for dlist in versions:
207
- for ditem in dlist:
208
- if not ditem.dependencyname in result:
209
- result[ditem.dependencyname]=set[str]()
210
- result[ditem.dependencyname].add(ditem.current_version)
211
- return result
212
-
213
- @staticmethod
214
- @check_arguments
215
- def choose_version(available_versions:list[str],current_version:str,echolon:VersionEcholon) -> str:
216
- match echolon:
217
- case VersionEcholon.LatestPatch:
218
- return GeneralUtilities.get_latest_version(GeneralUtilities.filter_versions_by_prefix(available_versions,f"{GeneralUtilities.get_major_part_of_version(current_version)}.{GeneralUtilities.get_minor_part_of_version(current_version)}."))
219
- case VersionEcholon.LatestPatchOrLatestMinor:
220
- return GeneralUtilities.get_latest_version(GeneralUtilities.filter_versions_by_prefix(available_versions,f"{GeneralUtilities.get_major_part_of_version(current_version)}."))
221
- case VersionEcholon.LatestPatchOrLatestMinorOrNextMajor:
222
- raise ValueError("not implemented")#TODO
223
- case VersionEcholon.LatestVersion:
224
- return GeneralUtilities.get_latest_version(available_versions)
225
- case VersionEcholon.NoUpdate:
226
- return current_version
227
- case _:
228
- raise ValueError("Unknown echolon-value: "+str(echolon))
229
-
230
- @staticmethod
231
- @check_arguments
232
- def get_latest_version(versions:list[str]) -> str:
233
- GeneralUtilities.assert_condition(0<len(versions),"Version-list can not be empty.")
234
- latest = max(versions, key=Version)
235
- return latest
236
-
237
- @staticmethod
238
- @check_arguments
239
- def filter_versions_by_prefix(versions:list[str],prefix:str) -> list[str]:
240
- return [v for v in versions if v.startswith(prefix)]
241
-
242
- @staticmethod
243
- @check_arguments
244
- def get_major_part_of_version(version:str) -> int:
245
- return GeneralUtilities.get_version_parts(version)[0]
246
-
247
- @staticmethod
248
- @check_arguments
249
- def get_minor_part_of_version(version:str) -> int:
250
- return GeneralUtilities.get_version_parts(version)[1]
251
-
252
- @staticmethod
253
- @check_arguments
254
- def get_patch_part_of_version(version:str) -> int:
255
- return GeneralUtilities.get_version_parts(version)[2]
256
-
257
- @staticmethod
258
- @check_arguments
259
- def get_version_parts(version:str) -> tuple[int,int,int]:
260
- match = re.match(r"^(\d+).(\d+).(\d+)$", version)
261
- GeneralUtilities.assert_condition(match is not None,f"string \"{version}\" is not a valid version.")
262
- return (int(match.group(1)),int(match.group(2)),int(match.group(3)))
263
-
264
- @staticmethod
265
- @check_arguments
266
- def is_ignored_by_glob_pattern(source_directory:str,path:str, ignored_glob_patterms: list[str]) -> bool:
267
- source_directory=GeneralUtilities.normalize_path(source_directory)
268
- path=GeneralUtilities.normalize_path(path)
269
- GeneralUtilities.assert_condition(path.startswith(source_directory), f"Path '{path}' is not located in source directory '{source_directory}'.")
270
- if ignored_glob_patterms is None:
271
- return False
272
- relative_path = os.path.relpath(path, source_directory)
273
- for pattern in ignored_glob_patterms:
274
- if fnmatch.filter([relative_path], pattern):
275
- return True
276
- return False
277
-
278
- @staticmethod
279
- @check_arguments
280
- def safe_copy(src:str, dst:str):
281
- shutil.copy2(src, dst)
282
- src_size = os.path.getsize(src)
283
- dst_size = os.path.getsize(dst)
284
- if src_size != dst_size:
285
- raise IOError(f"Copy failed: size mismatch {src} ({src_size}) != {dst} ({dst_size})")
286
-
287
- @staticmethod
288
- @check_arguments
289
- def __copy_or_move_content_of_folder(source_directory: str, target_directory: str, overwrite_existing_files:bool, remove_source: bool,ignored_glob_patterms: list[str] = None) -> None:
290
- srcDirFull = GeneralUtilities.resolve_relative_path_from_current_working_directory(source_directory)
291
- dstDirFull = GeneralUtilities.resolve_relative_path_from_current_working_directory(target_directory)
292
- if (os.path.isdir(source_directory)):
293
- GeneralUtilities.ensure_directory_exists(target_directory)
294
- for file in GeneralUtilities.get_direct_files_of_folder(srcDirFull):
295
- filename = os.path.basename(file)
296
- if not GeneralUtilities.is_ignored_by_glob_pattern(source_directory,file, ignored_glob_patterms):
297
- targetfile = os.path.join(dstDirFull, filename)
298
- if (os.path.isfile(targetfile)):
299
- if overwrite_existing_files:
300
- GeneralUtilities.ensure_file_does_not_exist(targetfile)
301
- else:
302
- raise ValueError(f"Targetfile '{targetfile}' does already exist.")
303
- GeneralUtilities.safe_copy(file, targetfile)
304
- if remove_source:
305
- GeneralUtilities.ensure_file_does_not_exist(file)#remove file to save storage
306
- for sub_folder in GeneralUtilities.get_direct_folders_of_folder(srcDirFull):
307
- if not GeneralUtilities.is_ignored_by_glob_pattern(source_directory,sub_folder, ignored_glob_patterms):
308
- foldername = os.path.basename(sub_folder)
309
- sub_target = os.path.join(dstDirFull, foldername)
310
- GeneralUtilities.__copy_or_move_content_of_folder(sub_folder, sub_target, overwrite_existing_files, remove_source,ignored_glob_patterms)
311
- if remove_source:
312
- GeneralUtilities.ensure_directory_does_not_exist(sub_folder)
313
- else:
314
- raise ValueError(f"Folder '{source_directory}' does not exist")
315
-
316
- @staticmethod
317
- @check_arguments
318
- def replace_regex_each_line_of_file(file: str, replace_from_regex: str, replace_to_regex: str, encoding="utf-8", verbose: bool = False) -> None:
319
- if verbose:
320
- GeneralUtilities.write_message_to_stdout(f"Replace '{replace_from_regex}' to '{replace_to_regex}' in '{file}'")
321
- lines=GeneralUtilities.read_lines_from_file(file,encoding)
322
- replaced_lines = []
323
- for line in lines:
324
- replaced_line = re.sub(replace_from_regex, replace_to_regex, line)
325
- replaced_lines.append(replaced_line)
326
- GeneralUtilities.write_lines_to_file(file,replaced_lines,encoding)
327
-
328
- @staticmethod
329
- @check_arguments
330
- def replace_regex_in_file(file: str, replace_from_regex: str, replace_to_regex: str, encoding="utf-8") -> None:
331
- with open(file, encoding=encoding, mode="r") as f:
332
- content = f.read()
333
- content = re.sub(replace_from_regex, replace_to_regex, content)
334
- with open(file, encoding=encoding, mode="w") as f:
335
- f.write(content)
336
-
337
- @staticmethod
338
- @check_arguments
339
- def replace_xmltag_in_file(file: str, tag: str, new_value: str, encoding="utf-8") -> None:
340
- GeneralUtilities.assert_condition(tag.isalnum(), f"Invalid tag: \"{tag}\"")
341
- GeneralUtilities.replace_regex_in_file(file, f"<{tag}>.*</{tag}>", f"<{tag}>{new_value}</{tag}>", encoding)
342
-
343
- @staticmethod
344
- @check_arguments
345
- def update_version_in_csproj_file(file: str, target_version: str) -> None:
346
- GeneralUtilities.replace_xmltag_in_file(file, "Version", target_version)
347
- GeneralUtilities.replace_xmltag_in_file(file, "AssemblyVersion", target_version + ".0")
348
- GeneralUtilities.replace_xmltag_in_file(file, "FileVersion", target_version + ".0")
349
-
350
- @staticmethod
351
- @check_arguments
352
- def replace_underscores_in_text(text: str, replacements: dict) -> str:
353
- changed = True
354
- while changed:
355
- changed = False
356
- for key, value in replacements.items():
357
- previousValue = text
358
- text = text.replace(f"__{key}__", value)
359
- if (not text == previousValue):
360
- changed = True
361
- return text
362
-
363
- @staticmethod
364
- @check_arguments
365
- def replace_underscores_in_file(file: str, replacements: dict, encoding: str = "utf-8"):
366
- text = GeneralUtilities.read_text_from_file(file, encoding)
367
- text = GeneralUtilities.replace_underscores_in_text(text, replacements)
368
- GeneralUtilities.write_text_to_file(file, text, encoding)
369
-
370
- @staticmethod
371
- @check_arguments
372
- def remove_invisible_characters(text: str) -> str:
373
- return "".join(character for character in text if character == "\n" or character.isprintable())
374
-
375
- @staticmethod
376
- @check_arguments
377
- def remove_invisible_characters_from_file(file: str, encoding: str = "utf-8") -> None:
378
- text = GeneralUtilities.read_text_from_file(file, encoding)
379
- text = GeneralUtilities.remove_invisible_characters(text)
380
- GeneralUtilities.write_text_to_file(file, text, encoding)
381
-
382
- @staticmethod
383
- @check_arguments
384
- def print_text(text: str, print_to_stdout: bool = True):
385
- stream: object = sys.stdout if print_to_stdout else sys.stderr
386
- GeneralUtilities.__print_text_to_console(text, stream)
387
-
388
- @staticmethod
389
- @check_arguments
390
- def print_text_in_green(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
391
- GeneralUtilities.print_text_in_color(text, 32, print_to_stdout, print_as_color)
392
-
393
- @staticmethod
394
- @check_arguments
395
- def print_text_in_yellow(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
396
- GeneralUtilities.print_text_in_color(text, 33, print_to_stdout, print_as_color)
397
-
398
- @staticmethod
399
- @check_arguments
400
- def print_text_in_red(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
401
- GeneralUtilities.print_text_in_color(text, 31, print_to_stdout, print_as_color)
402
-
403
- @staticmethod
404
- @check_arguments
405
- def print_text_in_cyan(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
406
- GeneralUtilities.print_text_in_color(text, 36, print_to_stdout, print_as_color)
407
-
408
- @staticmethod
409
- @check_arguments
410
- def print_text_in_color(text: str, colorcode: int, print_to_stdout: bool = True, print_as_color: bool = True):
411
- stream: object = sys.stdout if print_to_stdout else sys.stderr
412
- if print_as_color:
413
- text = f"\033[{colorcode}m{text}\033[0m"
414
- GeneralUtilities.__print_text_to_console(text, stream)
415
-
416
- @staticmethod
417
- @check_arguments
418
- def __print_text_to_console(text: str, stream: object):
419
- stream.write(text)
420
- stream.flush()
421
-
422
- @staticmethod
423
- @check_arguments
424
- def reconfigure_standard_input_and_outputs():
425
- sys.stdin.reconfigure(encoding='utf-8')
426
- sys.stderr.reconfigure(encoding='utf-8')
427
- sys.stdout.reconfigure(encoding='utf-8')
428
-
429
- @staticmethod
430
- @check_arguments
431
- def write_message_to_stdout_advanced(message: str, add_empty_lines: bool = True, adapt_lines: bool = True, append_linebreak: bool = True):
432
- new_line_character: str = "\n" if append_linebreak else GeneralUtilities.empty_string
433
- for line in GeneralUtilities.string_to_lines(message, add_empty_lines, adapt_lines):
434
- sys.stdout.write(GeneralUtilities.str_none_safe(line)+new_line_character)
435
- sys.stdout.flush()
436
-
437
- @staticmethod
438
- @check_arguments
439
- def write_message_to_stdout(message: str):
440
- GeneralUtilities.write_message_to_stdout_advanced(message, True, True, True)
441
-
442
- @staticmethod
443
- @check_arguments
444
- def write_message_to_stderr_advanced(message: str, add_empty_lines: bool = True, adapt_lines: bool = True, append_linebreak: bool = True):
445
- new_line_character: str = "\n" if append_linebreak else GeneralUtilities.empty_string
446
- for line in GeneralUtilities.string_to_lines(message, add_empty_lines, adapt_lines):
447
- sys.stderr.write(GeneralUtilities.str_none_safe(line)+new_line_character)
448
- sys.stderr.flush()
449
-
450
- @staticmethod
451
- @check_arguments
452
- def write_message_to_stderr(message: str):
453
- GeneralUtilities.write_message_to_stderr_advanced(message, True, True, True)
454
-
455
- @staticmethod
456
- @check_arguments
457
- def get_advanced_errormessage_for_os_error(os_error: OSError) -> str:
458
- if GeneralUtilities.string_has_content(os_error.filename2):
459
- secondpath = f" {os_error.filename2}"
460
- else:
461
- secondpath = GeneralUtilities.empty_string
462
- return f"Related path(s): {os_error.filename}{secondpath}"
463
-
464
- @staticmethod
465
- @check_arguments
466
- def write_exception_to_stderr(exception: Exception, extra_message: str = None):
467
- GeneralUtilities.write_exception_to_stderr_with_traceback(exception, None, extra_message)
468
-
469
- @staticmethod
470
- @check_arguments
471
- def write_exception_to_stderr_with_traceback(exception: Exception, current_traceback=None, extra_message: str = None):
472
- GeneralUtilities.write_message_to_stderr(GeneralUtilities.exception_to_str(exception,current_traceback,extra_message))
473
-
474
- @staticmethod
475
- @check_arguments
476
- def exception_to_str(exception: Exception, current_traceback=None, extra_message: str = None)->str:
477
- result=""
478
- result=result+"Exception("
479
- result=result+"\n Type: " + str(type(exception))
480
- result=result+"\n Message: " + str(exception)
481
- if extra_message is not None:
482
- result=result+"\n Extra-message: " + str(extra_message)
483
- if isinstance(exception, OSError):
484
- result=result+"\n "+GeneralUtilities.get_advanced_errormessage_for_os_error(exception)
485
- if current_traceback is not None:
486
- result=result+"\n Traceback:\n" +str(current_traceback.format_exc())
487
- result=result+"\n)"
488
- return result
489
-
490
- @staticmethod
491
- @check_arguments
492
- def string_has_content(string: str) -> bool:
493
- if string is None:
494
- return False
495
- else:
496
- return len(string.strip()) > 0
497
-
498
- @staticmethod
499
- @check_arguments
500
- def datetime_to_string_for_logfile_name(datetime_object: datetime, add_milliseconds: bool ) -> str:
501
- """returns a string like '2025-08-21T15-30-00+02-00' or '2025-08-21T15-30-00.123+02-00' depending on the value of add_milliseconds"""
502
- return GeneralUtilities.datetime_to_string_base(datetime_object, add_milliseconds, "T", "-", False)
503
-
504
- @staticmethod
505
- @check_arguments
506
- def datetime_to_string_for_logfile_entry(datetime_object: datetime, add_milliseconds: bool ) -> str:
507
- """returns a string like '2025-08-21T15:30:00+02:00' or '2025-08-21T15:30:00.123+02:00' depending on the value of add_milliseconds"""
508
- return GeneralUtilities.datetime_to_string_base(datetime_object, add_milliseconds, "T", ":", False)
509
-
510
- @staticmethod
511
- @check_arguments
512
- def datetime_to_string_for_readable_entry(datetime_object: datetime, add_milliseconds: bool ) -> str:
513
- """returns a string like '2025-08-21 15-30-00 +02:00' or '2025-08-21 15:30:00.123 +02:00' depending on the value of add_milliseconds"""
514
- return GeneralUtilities.datetime_to_string_base(datetime_object, add_milliseconds, " ", ":", True)
515
-
516
- @staticmethod
517
- @check_arguments
518
- def datetime_to_string_base(datetime_object: datetime, add_milliseconds: bool ,mid_separator:str,time_separator:str,add_whitespace_before_timezone:bool) -> str:
519
- dt:datetime=None
520
- if datetime_object.tzinfo is None:
521
- dt = datetime_object.replace(tzinfo=timezone.utc) # assume UTC when no timezone is given
522
- else:
523
- dt = datetime_object
524
- pattern = "%Y-%m-%d"+mid_separator+"%H"+time_separator+"%M"+time_separator+"%S"
525
- if add_milliseconds:
526
- pattern = pattern+".%f"
527
- if add_whitespace_before_timezone:
528
- pattern = pattern+" "
529
- pattern = pattern+"%z"
530
-
531
- result = dt.strftime(pattern)
532
- result = result[:-2] + time_separator + result[-2:]
533
- return result
534
-
535
- @staticmethod
536
- @check_arguments
537
- def string_has_nonwhitespace_content(string: str) -> bool:
538
- if string is None:
539
- return False
540
- else:
541
- return len(string.strip()) > 0
542
-
543
- @staticmethod
544
- @check_arguments
545
- def string_is_none_or_empty(argument: str) -> bool:
546
- if argument is None:
547
- return True
548
- type_of_argument = type(argument)
549
- if type_of_argument == str:
550
- return argument == GeneralUtilities.empty_string
551
- else:
552
- raise ValueError(f"expected string-variable in argument of string_is_none_or_empty but the type was '{str(type_of_argument)}'")
553
-
554
- @staticmethod
555
- @check_arguments
556
- def string_is_none_or_whitespace(string: str) -> bool:
557
- if GeneralUtilities.string_is_none_or_empty(string):
558
- return True
559
- else:
560
- return string.strip() == GeneralUtilities.empty_string
561
-
562
- @staticmethod
563
- @check_arguments
564
- def strip_new_line_character(value: str) -> str:
565
- while not GeneralUtilities.__strip_new_line_character_helper_value_is_ok(value):
566
- value = GeneralUtilities.__strip_new_line_character_helper_normalize_value(value)
567
- return value
568
-
569
- @staticmethod
570
- @check_arguments
571
- def __strip_new_line_character_helper_value_is_ok(value: str) -> bool:
572
- if value.startswith("\r") or value.endswith("\r"):
573
- return False
574
- if value.startswith("\n") or value.endswith("\n"):
575
- return False
576
- return True
577
-
578
- @staticmethod
579
- @check_arguments
580
- def __strip_new_line_character_helper_normalize_value(value: str) -> str:
581
- return value.strip('\n').strip('\r')
582
-
583
- @staticmethod
584
- @check_arguments
585
- def file_ends_with_newline(file: str) -> bool:
586
- file=GeneralUtilities.normalize_path(file)
587
- with open(file, "rb") as file_object:
588
- return GeneralUtilities.ends_with_newline_character(file_object.read())
589
-
590
- @staticmethod
591
- @check_arguments
592
- def ends_with_newline_character(content: bytes) -> bool:
593
- result = content.endswith(GeneralUtilities.string_to_bytes("\n"))
594
- return result
595
-
596
- @staticmethod
597
- @check_arguments
598
- def file_ends_with_content(file: str) -> bool:
599
- content = GeneralUtilities.read_binary_from_file(file)
600
- if len(content) == 0:
601
- return False
602
- else:
603
- if GeneralUtilities.ends_with_newline_character(content):
604
- return False
605
- else:
606
- return True
607
-
608
- @staticmethod
609
- @check_arguments
610
- def get_new_line_character_for_textfile_if_required(file: str) -> bool:
611
- if GeneralUtilities.file_ends_with_content(file):
612
- return "\n"
613
- else:
614
- return GeneralUtilities.empty_string
615
-
616
- @staticmethod
617
- @check_arguments
618
- def append_line_to_file(file: str, line: str, encoding: str = "utf-8") -> None:
619
- file=GeneralUtilities.normalize_path(file)
620
- GeneralUtilities.append_lines_to_file(file, [line], encoding)
621
-
622
- @staticmethod
623
- @check_arguments
624
- def append_lines_to_file(file: str, lines: list[str], encoding: str = "utf-8") -> None:
625
- file=GeneralUtilities.normalize_path(file)
626
- if len(lines) == 0:
627
- return
628
- is_first_line = True
629
- for line in lines:
630
- insert_linebreak: bool = None
631
- if is_first_line:
632
- insert_linebreak = GeneralUtilities.file_ends_with_content(file)
633
- else:
634
- insert_linebreak = True
635
- line_to_write: str = None
636
- if insert_linebreak:
637
- line_to_write = "\n"+line
638
- else:
639
- line_to_write = line
640
- with open(file, "r+b") as fileObject:
641
- fileObject.seek(0, os.SEEK_END)
642
- fileObject.write(GeneralUtilities.string_to_bytes(line_to_write, encoding))
643
- is_first_line = False
644
-
645
- @staticmethod
646
- @check_arguments
647
- def append_to_file(file: str, content: str, encoding: str = "utf-8") -> None:
648
- file=GeneralUtilities.normalize_path(file)
649
- GeneralUtilities.assert_condition(not "\n" in content, "Appending multiple lines is not allowed. Use append_lines_to_file instead.")
650
- with open(file, "a", encoding=encoding) as fileObject:
651
- fileObject.write(content)
652
-
653
- @staticmethod
654
- @check_arguments
655
- def ensure_directory_exists(path: str) -> None:
656
- path=GeneralUtilities.normalize_path(path)
657
- if not os.path.isdir(path):
658
- os.makedirs(path)
659
-
660
- @staticmethod
661
- @check_arguments
662
- def ensure_file_exists(path: str) -> None:
663
- path=GeneralUtilities.normalize_path(path)
664
- if (not os.path.isfile(path)):
665
- with open(path, "a+", encoding="utf-8"):
666
- pass
667
-
668
- @staticmethod
669
- @check_arguments
670
- def __remove_readonly(func, path, _):
671
- os.chmod(path, stat.S_IWRITE)
672
- func(path)
673
-
674
- @staticmethod
675
- @check_arguments
676
- def __rmtree(directory: str) -> None:
677
- shutil.rmtree(directory, onerror=GeneralUtilities.__remove_readonly) # pylint: disable=deprecated-argument
678
-
679
- @staticmethod
680
- @check_arguments
681
- def ensure_directory_does_not_exist(path: str) -> None:
682
- if (os.path.isdir(path)):
683
- for root, dirs, files in os.walk(path, topdown=False):
684
- for name in files:
685
- filename = os.path.join(root, name)
686
- os.chmod(filename, stat.S_IWUSR)
687
- os.remove(filename)
688
- for name in dirs:
689
- GeneralUtilities.__rmtree(os.path.join(root, name))
690
- GeneralUtilities.__rmtree(path)
691
-
692
- @staticmethod
693
- @check_arguments
694
- def ensure_folder_exists_and_is_empty(path: str) -> None:
695
- GeneralUtilities.ensure_directory_exists(path)
696
- for filename in os.listdir(path):
697
- file_path = os.path.join(path, filename)
698
- if os.path.isfile(file_path):
699
- os.remove(file_path)
700
- if os.path.islink(file_path):
701
- os.unlink(file_path)
702
- elif os.path.isdir(file_path):
703
- shutil.rmtree(file_path)
704
-
705
- @staticmethod
706
- @check_arguments
707
- def ensure_file_does_not_exist(path: str) -> None:
708
- if (os.path.isfile(path)):
709
- os.remove(path)
710
-
711
- @staticmethod
712
- @check_arguments
713
- def ensure_path_does_not_exist(path: str) -> None:
714
- if (os.path.isfile(path)):
715
- GeneralUtilities.ensure_file_does_not_exist(path)
716
- if (os.path.isdir(path)):
717
- GeneralUtilities.ensure_directory_does_not_exist(path)
718
-
719
- @staticmethod
720
- @check_arguments
721
- def format_xml_file(filepath: str) -> None:
722
- GeneralUtilities.format_xml_file_with_encoding(filepath, "utf-8")
723
-
724
- @staticmethod
725
- @check_arguments
726
- def format_xml_file_with_encoding(filepath: str, encoding: str) -> None:
727
- with open(filepath, 'r', encoding=encoding) as file:
728
- text = file.read()
729
- text = parse(text).toprettyxml()
730
- with open(filepath, 'w', encoding=encoding) as file:
731
- file.write(text)
732
-
733
- @staticmethod
734
- @check_arguments
735
- def get_clusters_and_sectors_of_disk(diskpath: str) -> None:
736
- GeneralUtilities.assert_condition(GeneralUtilities.current_system_is_windows(), "get_clusters_and_sectors_of_disk(diskpath) is only available on windows.")
737
- sectorsPerCluster = ctypes.c_ulonglong(0)
738
- bytesPerSector = ctypes.c_ulonglong(0)
739
- rootPathName = ctypes.c_wchar_p(diskpath)
740
- ctypes.windll.kernel32.GetDiskFreeSpaceW(rootPathName, ctypes.pointer(sectorsPerCluster), ctypes.pointer(bytesPerSector), None, None)
741
- return (sectorsPerCluster.value, bytesPerSector.value)
742
-
743
- @staticmethod
744
- @check_arguments
745
- def ensure_path_is_not_quoted(path: str) -> str:
746
- if (path.startswith("\"") and path.endswith("\"")) or (path.startswith("'") and path.endswith("'")):
747
- path = path[1:]
748
- path = path[:-1]
749
- return path
750
- else:
751
- return path
752
-
753
- @staticmethod
754
- @check_arguments
755
- def get_missing_files(folderA: str, folderB: str) -> list:
756
- folderA_length = len(folderA)
757
- result = []
758
- for fileA in GeneralUtilities.absolute_file_paths(folderA):
759
- file = fileA[folderA_length:]
760
- fileB = folderB + file
761
- if not os.path.isfile(fileB):
762
- result.append(fileB)
763
- return result
764
-
765
- @staticmethod
766
- @check_arguments
767
- def to_pascal_case(s: str) -> str:
768
- return ''.join(current.lower() if prev.isalnum() else current.upper() for prev, current in zip(' ' + s, s) if current.isalnum())
769
-
770
- @staticmethod
771
- @check_arguments
772
- def to_snake_case(s: str) -> str:
773
- return '_'.join(t.lower() for t in re.split(r'[^a-zA-Z0-9]+', s) if t)
774
-
775
- @staticmethod
776
- @check_arguments
777
- def to_camel_case(s: str) -> str:
778
- words = [t.lower() for t in re.split(r'[^a-zA-Z0-9]+', s) if t]
779
- return words[0] + ''.join(w.capitalize() for w in words[1:]) if words else ''
780
-
781
- @staticmethod
782
- @check_arguments
783
- def to_kebab_case(s: str) -> str:
784
- return '-'.join(t.lower() for t in re.split(r'[^a-zA-Z0-9]+', s) if t)
785
-
786
- @staticmethod
787
- @check_arguments
788
- def find_between(s: str, start: str, end: str) -> str:
789
- return s.split(start)[1].split(end)[0]
790
-
791
- @staticmethod
792
- @check_arguments
793
- def write_lines_to_file(file: str, lines: list, encoding="utf-8") -> None:
794
- lines = [GeneralUtilities.strip_new_line_character(line) for line in lines]
795
- content = "\n".join(lines)
796
- GeneralUtilities.write_text_to_file(file, content, encoding)
797
-
798
- @staticmethod
799
- @check_arguments
800
- def write_text_to_file(file: str, content: str, encoding="utf-8") -> None:
801
- GeneralUtilities.write_binary_to_file(file, bytes(bytearray(content, encoding)))
802
-
803
- @staticmethod
804
- @check_arguments
805
- def write_binary_to_file(file: str, content: bytes) -> None:
806
- with open(file, "wb") as file_object:
807
- file_object.write(content)
808
-
809
- @staticmethod
810
- def is_binary_file(path: str):
811
- content = GeneralUtilities.read_binary_from_file(path)
812
- binary_content_indicators = [b'\x00', b'\x01', b'\x02', b'\x03', b'\x04', b'\x05', b'\x06', b'\x07', b'\x08', b'\x0E', b'\x1F']
813
- for binary_content_indicator in binary_content_indicators:
814
- if binary_content_indicator in content:
815
- return True
816
- return False
817
-
818
- @staticmethod
819
- @check_arguments
820
- def read_lines_from_file(file: str, encoding="utf-8") -> list[str]:
821
- content = GeneralUtilities.read_text_from_file(file, encoding)
822
- if len(content) == 0:
823
- return []
824
- else:
825
- return [GeneralUtilities.strip_new_line_character(line) for line in content.split('\n')]
826
-
827
- @staticmethod
828
- @check_arguments
829
- def read_nonempty_lines_from_file(file: str, encoding="utf-8") -> list[str]:
830
- return [line for line in GeneralUtilities.read_lines_from_file(file, encoding) if GeneralUtilities.string_has_content(line)]
831
-
832
- @staticmethod
833
- @check_arguments
834
- def read_text_from_file(file: str, encoding="utf-8") -> str:
835
- GeneralUtilities.assert_file_exists(file)
836
- return GeneralUtilities.bytes_to_string(GeneralUtilities.read_binary_from_file(file), encoding)
837
-
838
- @staticmethod
839
- @check_arguments
840
- def read_text_from_file_without_linebreak(file: str, encoding="utf-8") -> str:
841
- return GeneralUtilities.read_text_from_file(file,encoding).replace("\n","").replace("\r","")
842
-
843
- @staticmethod
844
- @check_arguments
845
- def read_binary_from_file(file: str) -> bytes:
846
- try:
847
- file=GeneralUtilities.normalize_path(file)
848
- with open(file, "rb") as file_object:
849
- return file_object.read()
850
- except Exception as e:
851
- raise ValueError(f"Could not read file '{file}'.") from e
852
-
853
- @staticmethod
854
- @check_arguments
855
- def timedelta_to_simple_string(delta: timedelta) -> str:
856
- return (datetime(1970, 1, 1, 0, 0, 0) + delta).strftime('%H:%M:%S')
857
-
858
- @staticmethod
859
- @check_arguments
860
- def resolve_relative_path_from_current_working_directory(path: str) -> str:
861
- return GeneralUtilities.resolve_relative_path(path, os.getcwd())
862
-
863
- @staticmethod
864
- @check_arguments
865
- def resolve_relative_path(path: str, base_path: str):
866
- if (os.path.isabs(path)):
867
- return path
868
- else:
869
- return str(Path(os.path.join(base_path, path)).resolve())
870
-
871
- @staticmethod
872
- @check_arguments
873
- def get_metadata_for_file_for_clone_folder_structure(file: str) -> str:
874
- size = os.path.getsize(file)
875
- last_modified_timestamp = os.path.getmtime(file)
876
- hash_value = GeneralUtilities.get_sha256_of_file(file)
877
- last_access_timestamp = os.path.getatime(file)
878
- return f'{{"size":"{size}","sha256":"{hash_value}","mtime":"{last_modified_timestamp}","atime":"{last_access_timestamp}"}}'
879
-
880
- @staticmethod
881
- @check_arguments
882
- def clone_folder_structure(source: str, target: str, copy_only_metadata: bool):
883
- source = GeneralUtilities.resolve_relative_path(source, os.getcwd())
884
- target = GeneralUtilities.resolve_relative_path(target, os.getcwd())
885
- length_of_source = len(source)
886
- for source_file in GeneralUtilities.absolute_file_paths(source):
887
- target_file = target+source_file[length_of_source:]
888
- GeneralUtilities.ensure_directory_exists(os.path.dirname(target_file))
889
- if copy_only_metadata:
890
- with open(target_file, 'w', encoding='utf8') as f:
891
- f.write(GeneralUtilities.get_metadata_for_file_for_clone_folder_structure(source_file))
892
- else:
893
- shutil.copyfile(source_file, target_file)
894
-
895
- @staticmethod
896
- @check_arguments
897
- def current_user_has_elevated_privileges() -> bool:
898
- try:
899
- return os.getuid() == 0
900
- except AttributeError:
901
- return ctypes.windll.shell32.IsUserAnAdmin() == 1
902
-
903
- @staticmethod
904
- @check_arguments
905
- def ensure_elevated_privileges() -> None:
906
- if (not GeneralUtilities.current_user_has_elevated_privileges()):
907
- raise ValueError("Not enough privileges.")
908
-
909
- @staticmethod
910
- @check_arguments
911
- def rename_names_of_all_files_and_folders(folder: str, replace_from: str, replace_to: str, replace_only_full_match=False):
912
- for file in GeneralUtilities.get_direct_files_of_folder(folder):
913
- GeneralUtilities.replace_in_filename(file, replace_from, replace_to, replace_only_full_match)
914
- for sub_folder in GeneralUtilities.get_direct_folders_of_folder(folder):
915
- GeneralUtilities.rename_names_of_all_files_and_folders(sub_folder, replace_from, replace_to, replace_only_full_match)
916
- GeneralUtilities.replace_in_foldername(folder, replace_from, replace_to, replace_only_full_match)
917
-
918
- @staticmethod
919
- @check_arguments
920
- def get_direct_files_of_folder(folder: str) -> list[str]:
921
- result = [os.path.join(folder, f) for f in listdir(folder) if isfile(join(folder, f))]
922
- result = sorted(result, key=str.casefold)
923
- return result
924
-
925
- @staticmethod
926
- @check_arguments
927
- def get_direct_folders_of_folder(folder: str) -> list[str]:
928
- result = [os.path.join(folder, f) for f in listdir(folder) if isdir(join(folder, f))]
929
- result = sorted(result, key=str.casefold)
930
- return result
931
-
932
- @staticmethod
933
- @check_arguments
934
- def get_all_files_of_folder(folder: str) -> list[str]:
935
- result = list()
936
- result.extend(GeneralUtilities.get_direct_files_of_folder(folder))
937
- for subfolder in GeneralUtilities.get_direct_folders_of_folder(folder):
938
- result.extend(GeneralUtilities.get_all_files_of_folder(subfolder))
939
- result = sorted(result, key=str.casefold)
940
- return result
941
-
942
- @staticmethod
943
- @check_arguments
944
- def get_all_folders_of_folder(folder: str) -> list[str]:
945
- result = list()
946
- subfolders = GeneralUtilities.get_direct_folders_of_folder(folder)
947
- result.extend(subfolders)
948
- for subfolder in subfolders:
949
- result.extend(GeneralUtilities.get_all_folders_of_folder(subfolder))
950
- result = sorted(result, key=str.casefold)
951
- return result
952
-
953
- @staticmethod
954
- @check_arguments
955
- def get_all_objects_of_folder(folder: str) -> list[str]:
956
- return sorted(GeneralUtilities.get_all_files_of_folder(folder) + GeneralUtilities.get_all_folders_of_folder(folder), key=str.casefold)
957
-
958
- @staticmethod
959
- @check_arguments
960
- def replace_in_filename(file: str, replace_from: str, replace_to: str, replace_only_full_match=False):
961
- filename = Path(file).name
962
- if (GeneralUtilities.__should_get_replaced(filename, replace_from, replace_only_full_match)):
963
- folder_of_file = os.path.dirname(file)
964
- os.rename(file, os.path.join(folder_of_file, filename.replace(replace_from, replace_to)))
965
-
966
- @staticmethod
967
- @check_arguments
968
- def replace_in_foldername(folder: str, replace_from: str, replace_to: str, replace_only_full_match=False):
969
- foldername = Path(folder).name
970
- if (GeneralUtilities.__should_get_replaced(foldername, replace_from, replace_only_full_match)):
971
- folder_of_folder = os.path.dirname(folder)
972
- os.rename(folder, os.path.join(folder_of_folder, foldername.replace(replace_from, replace_to)))
973
-
974
- @staticmethod
975
- @check_arguments
976
- def __should_get_replaced(input_text, search_text, replace_only_full_match) -> bool:
977
- if replace_only_full_match:
978
- return input_text == search_text
979
- else:
980
- return search_text in input_text
981
-
982
- @staticmethod
983
- @check_arguments
984
- def str_none_safe(variable) -> str:
985
- if variable is None:
986
- return ''
987
- else:
988
- return str(variable)
989
-
990
- @staticmethod
991
- @check_arguments
992
- def arguments_to_array(arguments_as_string: str) -> list[str]:
993
- if arguments_as_string is None:
994
- return []
995
- if GeneralUtilities.string_has_content(arguments_as_string):
996
- return arguments_as_string.split(" ") # TODO this function should get improved to allow whitespaces in quote-substrings
997
- else:
998
- return []
999
-
1000
- @staticmethod
1001
- @check_arguments
1002
- def arguments_to_array_for_log(arguments_as_string: str) -> list[str]:
1003
- if arguments_as_string is None:
1004
- return None
1005
- return GeneralUtilities.arguments_to_array(arguments_as_string)
1006
-
1007
- @staticmethod
1008
- @check_arguments
1009
- def get_sha256_of_file(file: str) -> str:
1010
- sha256 = hashlib.sha256()
1011
- with open(file, "rb") as fileObject:
1012
- for chunk in iter(lambda: fileObject.read(4096), b""):
1013
- sha256.update(chunk)
1014
- return sha256.hexdigest()
1015
-
1016
- @staticmethod
1017
- @check_arguments
1018
- def remove_duplicates(input_list) -> list:
1019
- result = []
1020
- for item in input_list:
1021
- if not item in result:
1022
- result.append(item)
1023
- return result
1024
-
1025
- @staticmethod
1026
- @check_arguments
1027
- def print_stacktrace() -> None:
1028
- for line in traceback.format_stack():
1029
- GeneralUtilities.write_message_to_stderr(line.strip())
1030
-
1031
- @staticmethod
1032
- @check_arguments
1033
- def string_to_boolean(value: str) -> bool:
1034
- value = value.strip().lower()
1035
- if value in ('yes', 'y', 'true', 't', '1'):
1036
- return True
1037
- elif value in ('no', 'n', 'false', 'f', '0'):
1038
- return False
1039
- else:
1040
- raise ValueError(f"Can not convert '{value}' to a boolean value")
1041
-
1042
- @staticmethod
1043
- @check_arguments
1044
- def file_is_empty(file: str) -> bool:
1045
- return os.stat(file).st_size == 0
1046
-
1047
- @staticmethod
1048
- @check_arguments
1049
- def folder_is_empty(folder: str) -> bool:
1050
- return len(GeneralUtilities.get_direct_files_of_folder(folder)) == 0 and len(GeneralUtilities.get_direct_folders_of_folder(folder)) == 0
1051
-
1052
- @staticmethod
1053
- @check_arguments
1054
- def get_time_based_logfile_by_folder(folder: str, name: str = "Log") -> str:
1055
- return os.path.join(GeneralUtilities.resolve_relative_path_from_current_working_directory(folder), f"{GeneralUtilities.get_time_based_logfilename(name)}.log")
1056
-
1057
- @staticmethod
1058
- @check_arguments
1059
- def get_now() -> datetime:
1060
- return datetime.now().astimezone().replace(microsecond=0)
1061
-
1062
- @staticmethod
1063
- @check_arguments
1064
- def get_time_based_logfilename(name: str = "Log") -> str:
1065
- d = GeneralUtilities.get_now()
1066
- return f"{name}_{GeneralUtilities.datetime_to_string_for_logfile_name(d,False)}"
1067
-
1068
- @staticmethod
1069
- @check_arguments
1070
- def bytes_to_string(payload: bytes, encoding: str = 'utf-8') -> str:
1071
- return payload.decode(encoding, errors="ignore")
1072
-
1073
- @staticmethod
1074
- @check_arguments
1075
- def string_to_bytes(payload: str, encoding: str = 'utf-8') -> bytes:
1076
- return payload.encode(encoding, errors="ignore")
1077
-
1078
- @staticmethod
1079
- @check_arguments
1080
- def contains_line(lines, regex: str) -> bool:
1081
- for line in lines:
1082
- if (re.match(regex, line)):
1083
- return True
1084
- return False
1085
-
1086
- @staticmethod
1087
- @check_arguments
1088
- def read_csv_file(file: str, ignore_first_line: bool = False, treat_number_sign_at_begin_of_line_as_comment: bool = True, trim_values: bool = True, encoding="utf-8", ignore_empty_lines: bool = True, separator_character: str = ";", values_are_surrounded_by_quotes: bool = False) -> list[list[str]]:
1089
- lines = GeneralUtilities.read_lines_from_file(file, encoding)
1090
-
1091
- if ignore_first_line:
1092
- lines = lines[1:]
1093
- result = list()
1094
- line: str = None
1095
- for line_loopvariable in lines:
1096
- use_line = True
1097
- line = line_loopvariable
1098
-
1099
- if trim_values:
1100
- line = line.strip()
1101
- if ignore_empty_lines:
1102
- if not GeneralUtilities.string_has_content(line):
1103
- use_line = False
1104
-
1105
- if treat_number_sign_at_begin_of_line_as_comment:
1106
- if line.startswith("#"):
1107
- use_line = False
1108
-
1109
- if use_line:
1110
- if separator_character in line:
1111
- raw_values_of_line = GeneralUtilities.to_list(line, separator_character)
1112
- else:
1113
- raw_values_of_line = [line]
1114
- if trim_values:
1115
- raw_values_of_line = [value.strip() for value in raw_values_of_line]
1116
- values_of_line = []
1117
- for raw_value_of_line in raw_values_of_line:
1118
- value_of_line = raw_value_of_line
1119
- if values_are_surrounded_by_quotes:
1120
- value_of_line = value_of_line[1:]
1121
- value_of_line = value_of_line[:-1]
1122
- value_of_line = value_of_line.replace('""', '"')
1123
- values_of_line.append(value_of_line)
1124
- result.extend([values_of_line])
1125
- return result
1126
-
1127
- @staticmethod
1128
- @check_arguments
1129
- def epew_is_available() -> bool:
1130
- return GeneralUtilities.tool_is_available("epew")
1131
-
1132
- @staticmethod
1133
- @check_arguments
1134
- def tool_is_available(toolname: str) -> bool:
1135
- try:
1136
- return shutil.which(toolname) is not None
1137
- except:
1138
- return False
1139
-
1140
- @staticmethod
1141
- @check_arguments
1142
- @deprecated
1143
- def absolute_file_paths(directory: str) -> list[str]:
1144
- return GeneralUtilities.get_all_files_of_folder(directory)
1145
-
1146
- @staticmethod
1147
- @check_arguments
1148
- def to_list(list_as_string: str, separator: str = ",") -> list[str]:
1149
- result = list()
1150
- if list_as_string is not None:
1151
- list_as_string = list_as_string.strip()
1152
- if list_as_string == GeneralUtilities.empty_string:
1153
- pass
1154
- elif separator in list_as_string:
1155
- for item in list_as_string.split(separator):
1156
- result.append(item.strip())
1157
- else:
1158
- result.append(list_as_string)
1159
- return result
1160
-
1161
- @staticmethod
1162
- @check_arguments
1163
- def get_next_square_number(number: int) -> int:
1164
- GeneralUtilities.assert_condition(number >= 0, "get_next_square_number is only applicable for nonnegative numbers")
1165
- if number == 0:
1166
- return 1
1167
- root = 0
1168
- square = 0
1169
- while square < number:
1170
- root = root+1
1171
- square = root*root
1172
- return root*root
1173
-
1174
- @staticmethod
1175
- @check_arguments
1176
- def generate_password(length: int = 16, alphabet: str = None) -> None:
1177
- if alphabet is None:
1178
- alphabet = strin.ascii_letters + strin.digits+"_"
1179
- return GeneralUtilities.empty_string.join(secrets.choice(alphabet) for i in range(length))
1180
-
1181
- @staticmethod
1182
- @check_arguments
1183
- def assert_condition(condition: bool, information: str = None) -> None:
1184
- """Throws an exception if the condition is false."""
1185
- if (not condition):
1186
- if information is None:
1187
- information = "Internal assertion error."
1188
- raise ValueError("Condition failed. "+information)
1189
-
1190
- @staticmethod
1191
- def current_system_is_windows():
1192
- return platform.system() == 'Windows'
1193
-
1194
- @staticmethod
1195
- def current_system_is_linux():
1196
- return platform.system() == 'Linux'
1197
-
1198
- @staticmethod
1199
- def current_system_is_macos():
1200
- return platform.system() == 'Darwin'
1201
-
1202
- @staticmethod
1203
- def get_temp_folder() -> str:
1204
- """Returns an absolute path to the system-temp-folder. Use this instead of tempfile.gettempdir() directly:
1205
- in some Linux-build-containers TMPDIR/TEMP/TMP is set to a relative value, which makes tempfile.gettempdir()
1206
- return a relative path. A relative temp-path would create temp-content relative to the current working-directory
1207
- (e.g. inside a codeunit-folder) and would not be cleaned up reliably. In that case the platform-default absolute
1208
- temp-folder is used instead."""
1209
- temp_folder = tempfile.gettempdir()
1210
- if not os.path.isabs(temp_folder):
1211
- if GeneralUtilities.current_system_is_windows():
1212
- temp_folder = os.path.join(os.environ.get("SystemRoot", "C:\\Windows"), "Temp")
1213
- else:
1214
- temp_folder = "/tmp"
1215
- return temp_folder
1216
-
1217
- @staticmethod
1218
- def _internal_with_absolute_temp_folder_environment_variables(environment: dict) -> dict:
1219
- """Returns a copy of the given environment-dictionary in which the temp-folder-environment-variables (TMPDIR/TMP/TEMP)
1220
- are replaced by the absolute system-temp-folder if they are set to a relative value. This is required because spawned
1221
- child-processes resolve their temp-folder via these variables (e.g. .NET-tools like the coverlet-collector or CycloneDX
1222
- via Path.GetTempPath()); a relative value (as set in some Linux-build-containers) would otherwise make them create
1223
- temp-content relative to their current working-directory (e.g. a leftover 'tmp/<guid>'-folder inside a codeunit-folder)
1224
- instead of in the system-temp-folder."""
1225
- result = dict(environment)
1226
- absolute_temp_folder = GeneralUtilities.get_temp_folder()
1227
- for temp_environment_variable_name in ("TMPDIR", "TMP", "TEMP"):
1228
- value = result.get(temp_environment_variable_name)
1229
- if value is not None and not os.path.isabs(value):
1230
- result[temp_environment_variable_name] = absolute_temp_folder
1231
- return result
1232
-
1233
- @staticmethod
1234
- @check_arguments
1235
- def get_line():
1236
- return "--------------------------"
1237
-
1238
- @staticmethod
1239
- def get_longline():
1240
- return GeneralUtilities.get_line() + GeneralUtilities.get_line()
1241
-
1242
- @staticmethod
1243
- @check_arguments
1244
- def get_icon_check_empty(positive: bool) -> str:
1245
- if positive:
1246
- return "✅"
1247
- else:
1248
- return GeneralUtilities.empty_string
1249
-
1250
- @staticmethod
1251
- @check_arguments
1252
- def get_icon_check_cross(positive: bool) -> str:
1253
- if positive:
1254
- return "✅"
1255
- else:
1256
- return "❌"
1257
-
1258
- @staticmethod
1259
- @check_arguments
1260
- def get_certificate_expiry_date(certificate_file: str) -> datetime:
1261
- with open(certificate_file, encoding="utf-8") as certificate_file_content:
1262
- cert = crypto.load_certificate(crypto.FILETYPE_PEM, certificate_file_content.read())
1263
- date_as_bytes = cert.get_notAfter()
1264
- date_as_string = date_as_bytes.decode("utf-8")
1265
- result = datetime.strptime(date_as_string, '%Y%m%d%H%M%SZ')
1266
- return result
1267
-
1268
- @staticmethod
1269
- @check_arguments
1270
- def certificate_is_expired(certificate_file: str) -> bool:
1271
- return GeneralUtilities.get_certificate_expiry_date(certificate_file) < GeneralUtilities.get_now()
1272
-
1273
- @staticmethod
1274
- @check_arguments
1275
- def internet_connection_is_available() -> bool:
1276
- """Returns True as soon as any of a small set of well-known public hosts can be reached.
1277
- Returns False only when none of the hosts answered (= no internet connectivity)."""
1278
- hosts: list[str] = [
1279
- "https://www.google.com",
1280
- "https://www.wikipedia.org",
1281
- "https://www.github.com",
1282
- "https://www.cloudflare.com",
1283
- ]
1284
- for host in hosts:
1285
- try:
1286
- with urllib.request.urlopen(host, timeout=10):
1287
- return True
1288
- except Exception:
1289
- continue
1290
- return False
1291
-
1292
- @staticmethod
1293
- @check_arguments
1294
- def replace_variable_in_string(input_string: str, variable_name: str, variable_value: str) -> str:
1295
- GeneralUtilities.assert_condition(not "__" in variable_name, f"'{variable_name}' is an invalid variable name because it contains '__' which is treated as control-sequence.")
1296
- return input_string.replace(f"__[{variable_name}]__", variable_value)
1297
-
1298
- @staticmethod
1299
- @check_arguments
1300
- def replace_variable(prefix:str, variable_name:str, suffix:str, value:str, content: str) -> str:
1301
- GeneralUtilities.assert_condition(not "__" in variable_name, f"'{variable_name}' is an invalid variable name because it contains '__' which is treated as control-sequence.")
1302
- pattern = re.escape(GeneralUtilities.str_none_safe( prefix)) + r"\s*" +"__"+ re.escape(variable_name) + "__"+r"\s*" + re.escape(GeneralUtilities.str_none_safe( suffix))
1303
- result:str= re.sub(pattern, value, content)
1304
- GeneralUtilities.assert_condition(not f"__{variable_name}__" in result, f"Variable '{variable_name}' was not replaced in the content. This is likely caused by an error in the content or the variable name.")
1305
- return result
1306
-
1307
- @staticmethod
1308
- @check_arguments
1309
- def input(prompt: str, print_result: bool) -> str: # This function is a workaround for usescases like python scripts which calls input(...) using epew because then the prompt is not printed by the built-in-input-function.
1310
- GeneralUtilities.write_message_to_stdout(prompt)
1311
- result: str = input()
1312
- if print_result:
1313
- GeneralUtilities.write_message_to_stdout(f"Result: {result}")
1314
- return result
1315
-
1316
- @staticmethod
1317
- @check_arguments
1318
- def run_program_simple(program: str, arguments: list[str], cwd: str = None) -> tuple[int, str, str]:
1319
- if cwd is None:
1320
- cwd = os.getcwd()
1321
- cmd = [program]+arguments
1322
- with subprocess.Popen(cmd, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE) as process:
1323
- stdout, stderr = process.communicate()
1324
- exit_code = process.wait()
1325
- return (exit_code, stdout, stderr)
1326
-
1327
- @staticmethod
1328
- @check_arguments
1329
- def assert_file_exists(file: str,message=None) -> None:
1330
- if message is None:
1331
- message=f"File '{file}' does not exist."
1332
- GeneralUtilities.assert_condition(os.path.isfile(file), message)
1333
-
1334
- @staticmethod
1335
- @check_arguments
1336
- def assert_file_does_not_exist(file: str,message=None) -> None:
1337
- if message is None:
1338
- message=f"File '{file}' exists."
1339
- GeneralUtilities.assert_condition(not os.path.isfile(file), message)
1340
-
1341
- @staticmethod
1342
- @check_arguments
1343
- def assert_folder_exists(folder: str,message=None) -> None:
1344
- if message is None:
1345
- message=f"Folder '{folder}' does not exist."
1346
- GeneralUtilities.assert_condition(os.path.isdir(folder),message )
1347
-
1348
- @staticmethod
1349
- @check_arguments
1350
- def assert_folder_does_not_exist(folder: str,message=None) -> None:
1351
- if message is None:
1352
- message= f"Folder '{folder}' exists."
1353
- GeneralUtilities.assert_condition(not os.path.isdir(folder), f"Folder '{folder}' exists.")
1354
-
1355
- @staticmethod
1356
- @check_arguments
1357
- def assert_not_null(obj,message:str=None) -> str:
1358
- if message is None:
1359
- message="Variable is not set"
1360
- GeneralUtilities.assert_condition(obj is not None, message)
1361
-
1362
- @staticmethod
1363
- @check_arguments
1364
- def retry_action(action, amount_of_attempts: int, action_name: str = None,delay_in_seconds:int=2) -> None:
1365
- amount_of_fails = 0
1366
- last_exception:Exception=None
1367
- GeneralUtilities.assert_condition(0<amount_of_attempts,"amount_of_attempts must be greater than 0.")
1368
- while amount_of_fails<amount_of_attempts:
1369
- try:
1370
- result = action()
1371
- return result
1372
- except Exception as e:
1373
- time.sleep(delay_in_seconds)
1374
- amount_of_fails = amount_of_fails+1
1375
- last_exception=e
1376
- GeneralUtilities.assert_not_null(last_exception)
1377
- message = "Action"
1378
- if action_name is not None:
1379
- message = f"{message} \"{action_name}\""
1380
- message = f"{message} failed {amount_of_attempts} time(s)."
1381
- GeneralUtilities.write_message_to_stderr(message)
1382
- raise last_exception
1383
-
1384
-
1385
- @staticmethod
1386
- @check_arguments
1387
- def normalize_path(path)->str:
1388
- path=str(path)
1389
- path=os.path.expandvars(path)
1390
- if GeneralUtilities.current_system_is_windows():
1391
- path=path.replace("/","\\")
1392
- else:
1393
- path=path.replace("\\","/")
1394
- return path
1395
-
1396
- @staticmethod
1397
- @check_arguments
1398
- def int_to_string(number: int, leading_zeroplaces: int, trailing_zeroplaces: int) -> str:
1399
- return GeneralUtilities.float_to_string(float(number), leading_zeroplaces, trailing_zeroplaces)
1400
-
1401
- @staticmethod
1402
- @check_arguments
1403
- def float_to_string(number: float, leading_zeroplaces: int, trailing_zeroplaces: int) -> str:
1404
- plain_str = str(number)
1405
- GeneralUtilities.assert_condition("." in plain_str)
1406
- splitted: list[str] = plain_str.split(".")
1407
- return splitted[0].zfill(leading_zeroplaces)+"."+splitted[1].ljust(trailing_zeroplaces, '0')
1408
-
1409
- @staticmethod
1410
- @check_arguments
1411
- def process_is_running_by_name(process_name: str) -> bool:
1412
- processes: list[psutil.Process] = list(psutil.process_iter())
1413
- for p in processes:
1414
- if p.name() == process_name:
1415
- return True
1416
- return False
1417
-
1418
- @staticmethod
1419
- @check_arguments
1420
- def process_is_running_by_id(process_id: int) -> bool:
1421
- processes: list[psutil.Process] = list(psutil.process_iter())
1422
- for p in processes:
1423
- if p.pid == process_id:
1424
- return True
1425
- return False
1426
-
1427
- @staticmethod
1428
- @check_arguments
1429
- def kill_process(process_id: int, include_child_processes: bool) -> bool:
1430
- if GeneralUtilities. process_is_running_by_id(process_id):
1431
- GeneralUtilities.write_message_to_stdout(f"Process with id {process_id} is running. Terminating it...")
1432
- process = psutil.Process(process_id)
1433
- if include_child_processes:
1434
- for child in process.children(recursive=True):
1435
- if GeneralUtilities.process_is_running_by_id(child.pid):
1436
- child.kill()
1437
- if GeneralUtilities.process_is_running_by_id(process_id):
1438
- process.kill()
1439
- else:
1440
- GeneralUtilities.write_message_to_stdout(f"Process with id {process_id} is not running anymore.")
1441
-
1442
- @staticmethod
1443
- @check_arguments
1444
- def get_only_item_from_list(list_with_one_element:list):
1445
- GeneralUtilities.assert_condition(len(list_with_one_element)==1,f"List does not contain exactly one item. It contains {len(list_with_one_element)} items.")
1446
- return list_with_one_element[0]
1447
-
1448
- @staticmethod
1449
- @check_arguments
1450
- def trim_newlines(s: str) -> str:
1451
- return s.strip("\n")
1452
-
1453
- @staticmethod
1454
- @check_arguments
1455
- def log_merger(folder:str):
1456
- """For each log file in this folder this function looks for log-rotation-files and merges them together again into one file."""
1457
- all_files=GeneralUtilities.get_direct_files_of_folder(folder)
1458
- for log_file in all_files:
1459
- if "AutoRename" in log_file:
1460
- continue
1461
- filename=os.path.basename(log_file)
1462
- filename_without_extension=Path(log_file).stem
1463
- if not ".archive." in filename:
1464
-
1465
- #merge with rotated logs
1466
- if filename.endswith(".log") and not ".archive." in filename:
1467
- rotated_log_files=sorted([f for f in all_files if os.path.basename(f).startswith(filename_without_extension+".archive.")], key=GeneralUtilities._internal_extract_log_file_number)
1468
- result=GeneralUtilities.empty_string
1469
- if 0<len(rotated_log_files):
1470
- for rotated_log_file in rotated_log_files:
1471
- result+="\n"+GeneralUtilities.read_text_from_file(rotated_log_file)
1472
- result+="\n"+GeneralUtilities.read_text_from_file(log_file)
1473
- GeneralUtilities.write_text_to_file(log_file, result)
1474
- for rotated_log_file in rotated_log_files:
1475
- GeneralUtilities.ensure_file_does_not_exist(rotated_log_file)
1476
-
1477
- #normalize
1478
- logs=GeneralUtilities.read_text_from_file(log_file)
1479
- logs=logs.replace("\r\n", "\n")
1480
- logs=logs.replace("\r", GeneralUtilities.empty_string)
1481
- logs=re.sub(r"\n+", "\n", logs)
1482
- logs=GeneralUtilities.trim_newlines(logs)
1483
- GeneralUtilities.write_text_to_file(log_file, logs)
1484
-
1485
- @staticmethod
1486
- @check_arguments
1487
- def _internal_extract_log_file_number(filename: str) -> int:
1488
- m = re.search(r"\.archive\.(\d+)\.log$", filename)
1489
- if m:
1490
- return int(m.group(1))
1491
- else:
1492
- raise ValueError(f"Filename '{filename}' does not match the expected pattern for rotated log files.")
1493
-
1494
- @staticmethod
1495
- @check_arguments
1496
- def _internal_load_resource(relative_path: str) -> bytes:
1497
- res = resources.files("ScriptCollection.Resources")
1498
- for part in relative_path.split("/"):
1499
- res = res.joinpath(part)
1500
- result= res.read_bytes()
1501
- return result
1502
-
1503
- @staticmethod
1504
- @check_arguments
1505
- def escape_json_string_value(str_value: str) -> str:
1506
- """For input 'a"b\\c' this function returns 'a\\"b\\\\c'. This function does not add surrounding quotes. It only escapes the characters inside the string."""
1507
- json_escaped = json.dumps(str_value)
1508
- return json_escaped[1:-1]
1509
-
1510
- @staticmethod
1511
- @check_arguments
1512
- def escape_json_property_value(str_value: str) -> str:
1513
- # remove all characters not in [a-zA-Z0-9_]
1514
- sanitized = re.sub(r'[^a-zA-Z0-9_]', '', str_value)
1515
-
1516
- # check if result starts with [a-zA-Z_]
1517
- if not sanitized or len(sanitized) == 0 or not re.match(r'[a-zA-Z_]', sanitized[0]):
1518
- raise ValueError(f"Invalid JSON property name after sanitizing: '{sanitized}'. Value must start with [a-zA-Z_].")
1519
-
1520
- return sanitized
1521
-
1522
- @staticmethod
1523
- @check_arguments
1524
- def escape_yaml_string_value(str_value: str) -> str:
1525
- """For input 'a"b\\c' this function returns 'a\\"b\\\\c'. This function does not add surrounding quotes. It only escapes the characters inside the string."""
1526
- result= GeneralUtilities.escape_json_string_value(str_value)
1527
- #do other adaptions here if desired/required
1528
- return result
1529
-
1530
- @staticmethod
1531
- @check_arguments
1532
- def escape_yaml_property_value(str_value: str) -> str:
1533
- result= GeneralUtilities.escape_json_property_value(str_value)
1534
- #do other adaptions here if desired/required
1535
- return result
1536
-
1537
-
1538
- @staticmethod
1539
- @check_arguments
1540
- def get_current_platform() -> Platform:
1541
- system = platform.system().lower()
1542
- arch = platform.machine().lower()
1543
-
1544
- if system == "windows" and GeneralUtilities. system_is_x64(arch):
1545
- return Platform.Windows_AMD64
1546
- elif system == "linux" and GeneralUtilities.system_is_x64(arch):
1547
- return Platform.Linux_AMD64
1548
- elif system == "linux" and GeneralUtilities.system_is_arm64(arch):
1549
- return Platform.Linux_ARM64
1550
- elif system == "darwin" and GeneralUtilities.system_is_arm64(arch):
1551
- return Platform.MacOS_ARM64
1552
- else:
1553
- raise ValueError(f"Unsupported platform: {system}/{arch}")
1554
-
1555
- @staticmethod
1556
- @check_arguments
1557
- def current_system_is_x64():
1558
- return GeneralUtilities.system_is_x64(platform.machine().lower())
1559
-
1560
- @staticmethod
1561
- @check_arguments
1562
- def system_is_x64(arch:str):
1563
- return arch in ("x86_64", "amd64")
1564
-
1565
- @staticmethod
1566
- @check_arguments
1567
- def current_system_is_arm64():
1568
- return GeneralUtilities.system_is_arm64(platform.machine().lower())
1569
-
1570
- @staticmethod
1571
- @check_arguments
1572
- def system_is_arm64(arch:str):
1573
- return arch in ("arm", "arm64", "aarch64")
1574
-
1575
- @staticmethod
1576
- @check_arguments
1577
- def platform_to_short_str(platform_value: Platform) -> str:
1578
- mapping = {
1579
- Platform.Windows_AMD64: "win-x64",
1580
- Platform.Linux_AMD64: "linux-x64",
1581
- Platform.Linux_ARM64: "linux-arm64",
1582
- Platform.MacOS_ARM64: "osx-arm64",
1583
- }
1584
- return mapping[platform_value]
1585
-
1586
- @staticmethod
1587
- @check_arguments
1588
- def platform_from_short_str(platform_str: str) -> Platform:
1589
- mapping = {
1590
- "win-x64": Platform.Windows_AMD64,
1591
- "linux-x64": Platform.Linux_AMD64,
1592
- "linux-arm64": Platform.Linux_ARM64,
1593
- "osx-arm64": Platform.MacOS_ARM64,
1594
- }
1595
- if platform_str not in mapping:
1596
- raise ValueError(f"Unsupported platform string: {platform_str}")
1597
- return mapping[platform_str]
1598
-
1599
- @staticmethod
1600
- @check_arguments
1601
- def platform_to_dash_str(platform_value: Platform) -> str:
1602
- mapping = {
1603
- Platform.Windows_AMD64: "Windows-x64",
1604
- Platform.Linux_AMD64: "Linux-x64",
1605
- Platform.Linux_ARM64: "Linux-arm64",
1606
- Platform.MacOS_ARM64: "MacOS-arm64",
1607
- }
1608
- return mapping[platform_value]
1609
-
1610
- @staticmethod
1611
- @check_arguments
1612
- def platform_from_dash_str(platform_str: str) -> Platform:
1613
- mapping = {
1614
- "Windows-x64": Platform.Windows_AMD64,
1615
- "Linux-x64": Platform.Linux_AMD64,
1616
- "Linux-arm64": Platform.Linux_ARM64,
1617
- "MacOS-arm64": Platform.MacOS_ARM64,
1618
- }
1619
- if platform_str not in mapping:
1620
- raise ValueError(f"Unsupported platform string: {platform_str}")
1621
- return mapping[platform_str]
1622
-
1623
- @staticmethod
1624
- @check_arguments
1625
- def platform_to_docker_platform_str(platform_value: Platform) -> str:
1626
- mapping = {
1627
- Platform.Windows_AMD64: "windows/amd64",
1628
- Platform.Linux_AMD64: "linux/amd64",
1629
- Platform.Linux_ARM64: "linux/arm64",
1630
- Platform.MacOS_ARM64: "linux/arm64", # macOS → linux container
1631
- }
1632
- return mapping[platform_value]
1633
-
1634
- @staticmethod
1635
- @check_arguments
1636
- def platform_to_dotnet_runtime_identifier(platform_value: Platform) -> str:
1637
- mapping = {
1638
- Platform.Windows_AMD64: "win-x64",
1639
- Platform.Linux_AMD64: "linux-x64",
1640
- Platform.Linux_ARM64: "linux-arm64",
1641
- Platform.MacOS_ARM64: "osx-arm64",
1642
- }
1643
- return mapping[platform_value]
1644
-
1645
- @staticmethod
1646
- @check_arguments
1647
- def platform_to_go_runtime_identifier(platform_value: Platform) -> str:
1648
- mapping = {
1649
- Platform.Linux_AMD64: "amd64",
1650
- Platform.Linux_ARM64: "arm64",
1651
- }
1652
- return mapping[platform_value]
1653
-
1654
- @staticmethod
1655
- @check_arguments
1656
- def get_all_platforms() -> list[Platform]:
1657
- return [
1658
- Platform.Windows_AMD64,
1659
- Platform.Linux_AMD64,
1660
- Platform.Linux_ARM64,
1661
- Platform.MacOS_ARM64,
1662
- ]
1663
-
1664
- @staticmethod
1665
- @check_arguments
1666
- def get_scriptcollection_configuration_folder() -> str:
1667
- result = os.path.join(str(Path.home()), ".ScriptCollection")
1668
- result = GeneralUtilities.normalize_path(result)
1669
- GeneralUtilities.ensure_directory_exists(result)
1670
- return result
1671
-
1672
- @staticmethod
1673
- @check_arguments
1674
- def get_python_executable() -> str:
1675
- configured_file = os.path.join(GeneralUtilities.get_scriptcollection_configuration_folder(), "PythonExecutable.txt")
1676
- if os.path.isfile(configured_file):
1677
- result = GeneralUtilities.read_text_from_file(configured_file).strip()
1678
- GeneralUtilities.assert_condition(os.path.isfile(result), f"Python executable does not exist: '{result}'")
1679
- elif GeneralUtilities.string_has_content(sys.executable):
1680
- result = sys.executable
1681
- else:
1682
- result = "python"
1683
- return result
1684
-
1685
- @staticmethod
1686
- @check_arguments
1687
- def get_docker_executable() -> str:
1688
- configured_file = os.path.join(GeneralUtilities.get_scriptcollection_configuration_folder(), "DockerExecutable.txt")
1689
- if os.path.isfile(configured_file):
1690
- result = GeneralUtilities.read_text_from_file(configured_file).strip()
1691
- GeneralUtilities.assert_condition(os.path.isfile(result), f"Docker executable does not exist: '{result}'")
1692
- elif GeneralUtilities.string_has_content(sys.executable):
1693
- result = sys.executable
1694
- else:
1695
- result = "docker"
1696
- return result
1
+ import re
2
+ import os
3
+ from os import listdir
4
+ from os.path import isfile, join, isdir
5
+ import platform
6
+ import tempfile
7
+ import json
8
+ import inspect
9
+ import ctypes
10
+ import hashlib
11
+ import subprocess
12
+ import shutil
13
+ import time
14
+ import urllib
15
+ import stat
16
+ import fnmatch
17
+ import secrets
18
+ import string as strin
19
+ import sys
20
+ from importlib import resources
21
+ from enum import Enum
22
+ import traceback
23
+ import warnings
24
+ import functools
25
+ from pathlib import Path
26
+ from datetime import datetime, timedelta, date, timezone
27
+ import typing
28
+ from packaging.version import Version
29
+ import psutil
30
+ from defusedxml.minidom import parse
31
+ from OpenSSL import crypto
32
+
33
+ class VersionEcholon(Enum):
34
+ LatestPatch = 0
35
+ LatestPatchOrLatestMinor = 1
36
+ LatestPatchOrLatestMinorOrNextMajor = 2
37
+ LatestVersion = 3
38
+ CustomAlgorithm = 4
39
+ NoUpdate = 5
40
+
41
+ class Platform(Enum):
42
+ Windows_AMD64 = 0
43
+ Linux_AMD64 = 1
44
+ Linux_ARM64 = 2
45
+ MacOS_ARM64 = 3
46
+
47
+
48
+ class Dependency:
49
+ dependencyname:str = None
50
+ current_version:str = None
51
+
52
+ def __init__(self,dependencyname:str,current_version:str):
53
+ self.dependencyname=dependencyname
54
+ self.current_version=current_version
55
+
56
+ class GeneralUtilities:
57
+
58
+ empty_string: str = ""
59
+
60
+ @staticmethod
61
+ def get_modest_dark_url() -> str:
62
+ return "https://aniondev.github.io/CDN/ScriptCollectionDesigns/ModestDark/Style.css"
63
+
64
+ @staticmethod
65
+ def is_generic(t: typing.Type):
66
+ return hasattr(t, "__origin__")
67
+
68
+ @staticmethod
69
+ def is_debugger_attached():
70
+ return sys.gettrace() is not None
71
+
72
+ @staticmethod
73
+ def check_arguments(function):
74
+ def __check_function(*args, **named_args):
75
+ parameters: list = inspect.getfullargspec(function)[0].copy()
76
+ arguments: list = list(tuple(args)).copy()
77
+ if "self" in parameters:
78
+ parameters.remove("self")
79
+ arguments.pop(0)
80
+ for index, argument in enumerate(arguments):
81
+ if argument is not None: # Check type of None is not possible. None is always a valid argument-value
82
+ if parameters[index] in function.__annotations__: # Check if a type-hint for parameter exist. If not, no parameter-type available for argument-type-check
83
+ # Check type of arguments if the type is a generic type seems to be impossible.
84
+ if not GeneralUtilities.is_generic(function.__annotations__[parameters[index]]):
85
+ if not isinstance(argument, function.__annotations__[parameters[index]]):
86
+ raise TypeError(f"Argument with index {index} for function {function.__name__} ('{str(argument)}') is not of type {function.__annotations__[parameters[index]]} but has type "+str(type(argument)))
87
+ for index, named_argument in enumerate(named_args):
88
+ if named_args[named_argument] is not None:
89
+ if named_argument in function.__annotations__:
90
+ if not GeneralUtilities.is_generic(function.__annotations__.get(named_argument)):
91
+ if not isinstance(named_args[named_argument], function.__annotations__.get(named_argument)):
92
+ raise TypeError(f"Argument with name {named_argument} for function {function.__name__} ('{str(named_args[named_argument])}') is not of type {function.__annotations__.get(named_argument)}")
93
+ return function(*args, **named_args)
94
+ __check_function.__doc__ = function.__doc__
95
+ return __check_function
96
+
97
+ @staticmethod
98
+ def deprecated(reason: str=None):
99
+ def decorator(target):
100
+ if isinstance(target, type):
101
+ # target is a class: wrap its __init__ so the warning is emitted on instantiation while target stays a class.
102
+ original_init = target.__init__
103
+
104
+ @functools.wraps(original_init)
105
+ def init_wrapper(self, *args, **kwargs):
106
+ msg = f"Class {target.__name__} is deprecated."
107
+ if GeneralUtilities.string_has_content(reason):
108
+ msg += f" {reason}"
109
+ warnings.warn(
110
+ msg,
111
+ category=DeprecationWarning,
112
+ stacklevel=2
113
+ )
114
+ return original_init(self, *args, **kwargs)
115
+ target.__init__ = init_wrapper
116
+ return target
117
+
118
+ @functools.wraps(target)
119
+ def wrapper(*args, **kwargs):
120
+ msg = f"Function {target.__name__}() is deprecated."
121
+ if GeneralUtilities.string_has_content(reason):
122
+ msg += f" {reason}"
123
+ warnings.warn(
124
+ msg,
125
+ category=DeprecationWarning,
126
+ stacklevel=2
127
+ )
128
+ return target(*args, **kwargs)
129
+ return wrapper
130
+ return decorator
131
+
132
+ @staticmethod
133
+ @check_arguments
134
+ def args_array_surround_with_quotes_if_required(arguments: list[str]) -> list[str]:
135
+ result = []
136
+ for argument in arguments:
137
+ if " " in argument and not (argument.startswith('"') and argument.endswith('"')):
138
+ result.append(f'"{argument}"')
139
+ else:
140
+ result.append(argument)
141
+ return result
142
+
143
+ @staticmethod
144
+ @check_arguments
145
+ def string_to_lines(string: str, add_empty_lines: bool = True, adapt_lines: bool = True) -> list[str]:
146
+ result : list[str] = list[str]()
147
+ lines : list[str] = list[str]()
148
+ if (string is not None):
149
+ if ("\n" in string):
150
+ lines = string.split("\n")
151
+ else:
152
+ lines.append(string)
153
+ for rawline in lines:
154
+ if adapt_lines:
155
+ line = rawline.replace("\r", "").strip()
156
+ else:
157
+ line = rawline
158
+ if GeneralUtilities.string_is_none_or_whitespace(line):
159
+ if add_empty_lines:
160
+ result.append(line)
161
+ else:
162
+ result.append(line)
163
+ return result
164
+
165
+ @staticmethod
166
+ @check_arguments
167
+ def string_to_datetime(value: str) -> datetime:
168
+ """expects a value in the format 2022-10-06T19:26:01"""
169
+ if "." in value:
170
+ value = value.split(".")[0]
171
+ return datetime.strptime(value,"%Y-%m-%dT%H:%M:%S")
172
+
173
+ @staticmethod
174
+ @check_arguments
175
+ def datetime_to_string(value: datetime) -> str:
176
+ """returns a value in the format 2022-10-06T19:26:01"""
177
+ return value.strftime("%Y-%m-%dT%H:%M:%S")
178
+
179
+ @staticmethod
180
+ @check_arguments
181
+ def string_to_date(value: str) -> date:
182
+ """expects a value in the format 2022-10-06"""
183
+ splitted = value.split("-")
184
+ return date(int(splitted[0]), int(splitted[1]), int(splitted[2]))
185
+
186
+ @staticmethod
187
+ @check_arguments
188
+ def date_to_string(value: date) -> str:
189
+ """returns a value in the format 2022-10-06"""
190
+ return value.strftime("%Y-%m-%d")
191
+
192
+ @staticmethod
193
+ @check_arguments
194
+ def copy_content_of_folder(source_directory: str, target_directory: str, overwrite_existing_files=False, ignored_glob_patterms: list[str] = None) -> None:
195
+ GeneralUtilities.__copy_or_move_content_of_folder(source_directory, target_directory, overwrite_existing_files, False, ignored_glob_patterms)
196
+
197
+ @staticmethod
198
+ @check_arguments
199
+ def move_content_of_folder(source_directory: str, target_directory: str, overwrite_existing_files=False, ignored_glob_patterms: list[str] = None) -> None:
200
+ GeneralUtilities.__copy_or_move_content_of_folder(source_directory, target_directory, overwrite_existing_files, True, ignored_glob_patterms)
201
+
202
+ @staticmethod
203
+ @check_arguments
204
+ def merge_dependency_lists(versions:list[list[Dependency]]) -> dict[str,set[str]]:
205
+ result:dict[str,set[str]]=dict[str,set[str]]()
206
+ for dlist in versions:
207
+ for ditem in dlist:
208
+ if not ditem.dependencyname in result:
209
+ result[ditem.dependencyname]=set[str]()
210
+ result[ditem.dependencyname].add(ditem.current_version)
211
+ return result
212
+
213
+ @staticmethod
214
+ @check_arguments
215
+ def choose_version(available_versions:list[str],current_version:str,echolon:VersionEcholon) -> str:
216
+ match echolon:
217
+ case VersionEcholon.LatestPatch:
218
+ return GeneralUtilities.get_latest_version(GeneralUtilities.filter_versions_by_prefix(available_versions,f"{GeneralUtilities.get_major_part_of_version(current_version)}.{GeneralUtilities.get_minor_part_of_version(current_version)}."))
219
+ case VersionEcholon.LatestPatchOrLatestMinor:
220
+ return GeneralUtilities.get_latest_version(GeneralUtilities.filter_versions_by_prefix(available_versions,f"{GeneralUtilities.get_major_part_of_version(current_version)}."))
221
+ case VersionEcholon.LatestPatchOrLatestMinorOrNextMajor:
222
+ raise ValueError("not implemented")#TODO
223
+ case VersionEcholon.LatestVersion:
224
+ return GeneralUtilities.get_latest_version(available_versions)
225
+ case VersionEcholon.NoUpdate:
226
+ return current_version
227
+ case _:
228
+ raise ValueError("Unknown echolon-value: "+str(echolon))
229
+
230
+ @staticmethod
231
+ @check_arguments
232
+ def get_latest_version(versions:list[str]) -> str:
233
+ GeneralUtilities.assert_condition(0<len(versions),"Version-list can not be empty.")
234
+ latest = max(versions, key=Version)
235
+ return latest
236
+
237
+ @staticmethod
238
+ @check_arguments
239
+ def filter_versions_by_prefix(versions:list[str],prefix:str) -> list[str]:
240
+ return [v for v in versions if v.startswith(prefix)]
241
+
242
+ @staticmethod
243
+ @check_arguments
244
+ def get_major_part_of_version(version:str) -> int:
245
+ return GeneralUtilities.get_version_parts(version)[0]
246
+
247
+ @staticmethod
248
+ @check_arguments
249
+ def get_minor_part_of_version(version:str) -> int:
250
+ return GeneralUtilities.get_version_parts(version)[1]
251
+
252
+ @staticmethod
253
+ @check_arguments
254
+ def get_patch_part_of_version(version:str) -> int:
255
+ return GeneralUtilities.get_version_parts(version)[2]
256
+
257
+ @staticmethod
258
+ @check_arguments
259
+ def get_version_parts(version:str) -> tuple[int,int,int]:
260
+ match = re.match(r"^(\d+).(\d+).(\d+)$", version)
261
+ GeneralUtilities.assert_condition(match is not None,f"string \"{version}\" is not a valid version.")
262
+ return (int(match.group(1)),int(match.group(2)),int(match.group(3)))
263
+
264
+ @staticmethod
265
+ @check_arguments
266
+ def is_ignored_by_glob_pattern(source_directory:str,path:str, ignored_glob_patterms: list[str]) -> bool:
267
+ source_directory=GeneralUtilities.normalize_path(source_directory)
268
+ path=GeneralUtilities.normalize_path(path)
269
+ GeneralUtilities.assert_condition(path.startswith(source_directory), f"Path '{path}' is not located in source directory '{source_directory}'.")
270
+ if ignored_glob_patterms is None:
271
+ return False
272
+ relative_path = os.path.relpath(path, source_directory)
273
+ for pattern in ignored_glob_patterms:
274
+ if fnmatch.filter([relative_path], pattern):
275
+ return True
276
+ return False
277
+
278
+ @staticmethod
279
+ @check_arguments
280
+ def safe_copy(src:str, dst:str):
281
+ shutil.copy2(src, dst)
282
+ src_size = os.path.getsize(src)
283
+ dst_size = os.path.getsize(dst)
284
+ if src_size != dst_size:
285
+ raise IOError(f"Copy failed: size mismatch {src} ({src_size}) != {dst} ({dst_size})")
286
+
287
+ @staticmethod
288
+ @check_arguments
289
+ def __copy_or_move_content_of_folder(source_directory: str, target_directory: str, overwrite_existing_files:bool, remove_source: bool,ignored_glob_patterms: list[str] = None) -> None:
290
+ srcDirFull = GeneralUtilities.resolve_relative_path_from_current_working_directory(source_directory)
291
+ dstDirFull = GeneralUtilities.resolve_relative_path_from_current_working_directory(target_directory)
292
+ if (os.path.isdir(source_directory)):
293
+ GeneralUtilities.ensure_directory_exists(target_directory)
294
+ for file in GeneralUtilities.get_direct_files_of_folder(srcDirFull):
295
+ filename = os.path.basename(file)
296
+ if not GeneralUtilities.is_ignored_by_glob_pattern(source_directory,file, ignored_glob_patterms):
297
+ targetfile = os.path.join(dstDirFull, filename)
298
+ if (os.path.isfile(targetfile)):
299
+ if overwrite_existing_files:
300
+ GeneralUtilities.ensure_file_does_not_exist(targetfile)
301
+ else:
302
+ raise ValueError(f"Targetfile '{targetfile}' does already exist.")
303
+ GeneralUtilities.safe_copy(file, targetfile)
304
+ if remove_source:
305
+ GeneralUtilities.ensure_file_does_not_exist(file)#remove file to save storage
306
+ for sub_folder in GeneralUtilities.get_direct_folders_of_folder(srcDirFull):
307
+ if not GeneralUtilities.is_ignored_by_glob_pattern(source_directory,sub_folder, ignored_glob_patterms):
308
+ foldername = os.path.basename(sub_folder)
309
+ sub_target = os.path.join(dstDirFull, foldername)
310
+ GeneralUtilities.__copy_or_move_content_of_folder(sub_folder, sub_target, overwrite_existing_files, remove_source,ignored_glob_patterms)
311
+ if remove_source:
312
+ GeneralUtilities.ensure_directory_does_not_exist(sub_folder)
313
+ else:
314
+ raise ValueError(f"Folder '{source_directory}' does not exist")
315
+
316
+ @staticmethod
317
+ @check_arguments
318
+ def replace_regex_each_line_of_file(file: str, replace_from_regex: str, replace_to_regex: str, encoding="utf-8", verbose: bool = False) -> None:
319
+ if verbose:
320
+ GeneralUtilities.write_message_to_stdout(f"Replace '{replace_from_regex}' to '{replace_to_regex}' in '{file}'")
321
+ lines=GeneralUtilities.read_lines_from_file(file,encoding)
322
+ replaced_lines = []
323
+ for line in lines:
324
+ replaced_line = re.sub(replace_from_regex, replace_to_regex, line)
325
+ replaced_lines.append(replaced_line)
326
+ GeneralUtilities.write_lines_to_file(file,replaced_lines,encoding)
327
+
328
+ @staticmethod
329
+ @check_arguments
330
+ def replace_regex_in_file(file: str, replace_from_regex: str, replace_to_regex: str, encoding="utf-8") -> None:
331
+ with open(file, encoding=encoding, mode="r") as f:
332
+ content = f.read()
333
+ content = re.sub(replace_from_regex, replace_to_regex, content)
334
+ with open(file, encoding=encoding, mode="w") as f:
335
+ f.write(content)
336
+
337
+ @staticmethod
338
+ @check_arguments
339
+ def replace_xmltag_in_file(file: str, tag: str, new_value: str, encoding="utf-8") -> None:
340
+ GeneralUtilities.assert_condition(tag.isalnum(), f"Invalid tag: \"{tag}\"")
341
+ GeneralUtilities.replace_regex_in_file(file, f"<{tag}>.*</{tag}>", f"<{tag}>{new_value}</{tag}>", encoding)
342
+
343
+ @staticmethod
344
+ @check_arguments
345
+ def update_version_in_csproj_file(file: str, target_version: str) -> None:
346
+ GeneralUtilities.replace_xmltag_in_file(file, "Version", target_version)
347
+ GeneralUtilities.replace_xmltag_in_file(file, "AssemblyVersion", target_version + ".0")
348
+ GeneralUtilities.replace_xmltag_in_file(file, "FileVersion", target_version + ".0")
349
+
350
+ @staticmethod
351
+ @check_arguments
352
+ def replace_underscores_in_text(text: str, replacements: dict) -> str:
353
+ changed = True
354
+ while changed:
355
+ changed = False
356
+ for key, value in replacements.items():
357
+ previousValue = text
358
+ text = text.replace(f"__{key}__", value)
359
+ if (not text == previousValue):
360
+ changed = True
361
+ return text
362
+
363
+ @staticmethod
364
+ @check_arguments
365
+ def replace_underscores_in_file(file: str, replacements: dict, encoding: str = "utf-8"):
366
+ text = GeneralUtilities.read_text_from_file(file, encoding)
367
+ text = GeneralUtilities.replace_underscores_in_text(text, replacements)
368
+ GeneralUtilities.write_text_to_file(file, text, encoding)
369
+
370
+ @staticmethod
371
+ @check_arguments
372
+ def remove_invisible_characters(text: str) -> str:
373
+ return "".join(character for character in text if character == "\n" or character.isprintable())
374
+
375
+ @staticmethod
376
+ @check_arguments
377
+ def remove_invisible_characters_from_file(file: str, encoding: str = "utf-8") -> None:
378
+ text = GeneralUtilities.read_text_from_file(file, encoding)
379
+ text = GeneralUtilities.remove_invisible_characters(text)
380
+ GeneralUtilities.write_text_to_file(file, text, encoding)
381
+
382
+ @staticmethod
383
+ @check_arguments
384
+ def print_text(text: str, print_to_stdout: bool = True):
385
+ stream: object = sys.stdout if print_to_stdout else sys.stderr
386
+ GeneralUtilities.__print_text_to_console(text, stream)
387
+
388
+ @staticmethod
389
+ @check_arguments
390
+ def print_text_in_green(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
391
+ GeneralUtilities.print_text_in_color(text, 32, print_to_stdout, print_as_color)
392
+
393
+ @staticmethod
394
+ @check_arguments
395
+ def print_text_in_yellow(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
396
+ GeneralUtilities.print_text_in_color(text, 33, print_to_stdout, print_as_color)
397
+
398
+ @staticmethod
399
+ @check_arguments
400
+ def print_text_in_red(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
401
+ GeneralUtilities.print_text_in_color(text, 31, print_to_stdout, print_as_color)
402
+
403
+ @staticmethod
404
+ @check_arguments
405
+ def print_text_in_cyan(text: str, print_to_stdout: bool = True, print_as_color: bool = True):
406
+ GeneralUtilities.print_text_in_color(text, 36, print_to_stdout, print_as_color)
407
+
408
+ @staticmethod
409
+ @check_arguments
410
+ def print_text_in_color(text: str, colorcode: int, print_to_stdout: bool = True, print_as_color: bool = True):
411
+ stream: object = sys.stdout if print_to_stdout else sys.stderr
412
+ if print_as_color:
413
+ text = f"\033[{colorcode}m{text}\033[0m"
414
+ GeneralUtilities.__print_text_to_console(text, stream)
415
+
416
+ @staticmethod
417
+ @check_arguments
418
+ def __print_text_to_console(text: str, stream: object):
419
+ stream.write(text)
420
+ stream.flush()
421
+
422
+ @staticmethod
423
+ @check_arguments
424
+ def reconfigure_standard_input_and_outputs():
425
+ sys.stdin.reconfigure(encoding='utf-8')
426
+ sys.stderr.reconfigure(encoding='utf-8')
427
+ sys.stdout.reconfigure(encoding='utf-8')
428
+
429
+ @staticmethod
430
+ @check_arguments
431
+ def write_message_to_stdout_advanced(message: str, add_empty_lines: bool = True, adapt_lines: bool = True, append_linebreak: bool = True):
432
+ new_line_character: str = "\n" if append_linebreak else GeneralUtilities.empty_string
433
+ for line in GeneralUtilities.string_to_lines(message, add_empty_lines, adapt_lines):
434
+ sys.stdout.write(GeneralUtilities.str_none_safe(line)+new_line_character)
435
+ sys.stdout.flush()
436
+
437
+ @staticmethod
438
+ @check_arguments
439
+ def write_message_to_stdout(message: str):
440
+ GeneralUtilities.write_message_to_stdout_advanced(message, True, True, True)
441
+
442
+ @staticmethod
443
+ @check_arguments
444
+ def write_message_to_stderr_advanced(message: str, add_empty_lines: bool = True, adapt_lines: bool = True, append_linebreak: bool = True):
445
+ new_line_character: str = "\n" if append_linebreak else GeneralUtilities.empty_string
446
+ for line in GeneralUtilities.string_to_lines(message, add_empty_lines, adapt_lines):
447
+ sys.stderr.write(GeneralUtilities.str_none_safe(line)+new_line_character)
448
+ sys.stderr.flush()
449
+
450
+ @staticmethod
451
+ @check_arguments
452
+ def write_message_to_stderr(message: str):
453
+ GeneralUtilities.write_message_to_stderr_advanced(message, True, True, True)
454
+
455
+ @staticmethod
456
+ @check_arguments
457
+ def get_advanced_errormessage_for_os_error(os_error: OSError) -> str:
458
+ if GeneralUtilities.string_has_content(os_error.filename2):
459
+ secondpath = f" {os_error.filename2}"
460
+ else:
461
+ secondpath = GeneralUtilities.empty_string
462
+ return f"Related path(s): {os_error.filename}{secondpath}"
463
+
464
+ @staticmethod
465
+ @check_arguments
466
+ def write_exception_to_stderr(exception: Exception, extra_message: str = None):
467
+ GeneralUtilities.write_exception_to_stderr_with_traceback(exception, None, extra_message)
468
+
469
+ @staticmethod
470
+ @check_arguments
471
+ def write_exception_to_stderr_with_traceback(exception: Exception, current_traceback=None, extra_message: str = None):
472
+ GeneralUtilities.write_message_to_stderr(GeneralUtilities.exception_to_str(exception,current_traceback,extra_message))
473
+
474
+ @staticmethod
475
+ @check_arguments
476
+ def exception_to_str(exception: Exception, current_traceback=None, extra_message: str = None)->str:
477
+ result=""
478
+ result=result+"Exception("
479
+ result=result+"\n Type: " + str(type(exception))
480
+ result=result+"\n Message: " + str(exception)
481
+ if extra_message is not None:
482
+ result=result+"\n Extra-message: " + str(extra_message)
483
+ if isinstance(exception, OSError):
484
+ result=result+"\n "+GeneralUtilities.get_advanced_errormessage_for_os_error(exception)
485
+ if current_traceback is not None:
486
+ result=result+"\n Traceback:\n" +str(current_traceback.format_exc())
487
+ result=result+"\n)"
488
+ return result
489
+
490
+ @staticmethod
491
+ @check_arguments
492
+ def string_has_content(string: str) -> bool:
493
+ if string is None:
494
+ return False
495
+ else:
496
+ return len(string.strip()) > 0
497
+
498
+ @staticmethod
499
+ @check_arguments
500
+ def datetime_to_string_for_logfile_name(datetime_object: datetime, add_milliseconds: bool ) -> str:
501
+ """returns a string like '2025-08-21T15-30-00+02-00' or '2025-08-21T15-30-00.123+02-00' depending on the value of add_milliseconds"""
502
+ return GeneralUtilities.datetime_to_string_base(datetime_object, add_milliseconds, "T", "-", False)
503
+
504
+ @staticmethod
505
+ @check_arguments
506
+ def datetime_to_string_for_logfile_entry(datetime_object: datetime, add_milliseconds: bool ) -> str:
507
+ """returns a string like '2025-08-21T15:30:00+02:00' or '2025-08-21T15:30:00.123+02:00' depending on the value of add_milliseconds"""
508
+ return GeneralUtilities.datetime_to_string_base(datetime_object, add_milliseconds, "T", ":", False)
509
+
510
+ @staticmethod
511
+ @check_arguments
512
+ def datetime_to_string_for_readable_entry(datetime_object: datetime, add_milliseconds: bool ) -> str:
513
+ """returns a string like '2025-08-21 15-30-00 +02:00' or '2025-08-21 15:30:00.123 +02:00' depending on the value of add_milliseconds"""
514
+ return GeneralUtilities.datetime_to_string_base(datetime_object, add_milliseconds, " ", ":", True)
515
+
516
+ @staticmethod
517
+ @check_arguments
518
+ def datetime_to_string_base(datetime_object: datetime, add_milliseconds: bool ,mid_separator:str,time_separator:str,add_whitespace_before_timezone:bool) -> str:
519
+ dt:datetime=None
520
+ if datetime_object.tzinfo is None:
521
+ dt = datetime_object.replace(tzinfo=timezone.utc) # assume UTC when no timezone is given
522
+ else:
523
+ dt = datetime_object
524
+ pattern = "%Y-%m-%d"+mid_separator+"%H"+time_separator+"%M"+time_separator+"%S"
525
+ if add_milliseconds:
526
+ pattern = pattern+".%f"
527
+ if add_whitespace_before_timezone:
528
+ pattern = pattern+" "
529
+ pattern = pattern+"%z"
530
+
531
+ result = dt.strftime(pattern)
532
+ result = result[:-2] + time_separator + result[-2:]
533
+ return result
534
+
535
+ @staticmethod
536
+ @check_arguments
537
+ def string_has_nonwhitespace_content(string: str) -> bool:
538
+ if string is None:
539
+ return False
540
+ else:
541
+ return len(string.strip()) > 0
542
+
543
+ @staticmethod
544
+ @check_arguments
545
+ def string_is_none_or_empty(argument: str) -> bool:
546
+ if argument is None:
547
+ return True
548
+ type_of_argument = type(argument)
549
+ if type_of_argument == str:
550
+ return argument == GeneralUtilities.empty_string
551
+ else:
552
+ raise ValueError(f"expected string-variable in argument of string_is_none_or_empty but the type was '{str(type_of_argument)}'")
553
+
554
+ @staticmethod
555
+ @check_arguments
556
+ def string_is_none_or_whitespace(string: str) -> bool:
557
+ if GeneralUtilities.string_is_none_or_empty(string):
558
+ return True
559
+ else:
560
+ return string.strip() == GeneralUtilities.empty_string
561
+
562
+ @staticmethod
563
+ @check_arguments
564
+ def strip_new_line_character(value: str) -> str:
565
+ while not GeneralUtilities.__strip_new_line_character_helper_value_is_ok(value):
566
+ value = GeneralUtilities.__strip_new_line_character_helper_normalize_value(value)
567
+ return value
568
+
569
+ @staticmethod
570
+ @check_arguments
571
+ def __strip_new_line_character_helper_value_is_ok(value: str) -> bool:
572
+ if value.startswith("\r") or value.endswith("\r"):
573
+ return False
574
+ if value.startswith("\n") or value.endswith("\n"):
575
+ return False
576
+ return True
577
+
578
+ @staticmethod
579
+ @check_arguments
580
+ def __strip_new_line_character_helper_normalize_value(value: str) -> str:
581
+ return value.strip('\n').strip('\r')
582
+
583
+ @staticmethod
584
+ @check_arguments
585
+ def file_ends_with_newline(file: str) -> bool:
586
+ file=GeneralUtilities.normalize_path(file)
587
+ with open(file, "rb") as file_object:
588
+ return GeneralUtilities.ends_with_newline_character(file_object.read())
589
+
590
+ @staticmethod
591
+ @check_arguments
592
+ def ends_with_newline_character(content: bytes) -> bool:
593
+ result = content.endswith(GeneralUtilities.string_to_bytes("\n"))
594
+ return result
595
+
596
+ @staticmethod
597
+ @check_arguments
598
+ def file_ends_with_content(file: str) -> bool:
599
+ content = GeneralUtilities.read_binary_from_file(file)
600
+ if len(content) == 0:
601
+ return False
602
+ else:
603
+ if GeneralUtilities.ends_with_newline_character(content):
604
+ return False
605
+ else:
606
+ return True
607
+
608
+ @staticmethod
609
+ @check_arguments
610
+ def get_new_line_character_for_textfile_if_required(file: str) -> bool:
611
+ if GeneralUtilities.file_ends_with_content(file):
612
+ return "\n"
613
+ else:
614
+ return GeneralUtilities.empty_string
615
+
616
+ @staticmethod
617
+ @check_arguments
618
+ def append_line_to_file(file: str, line: str, encoding: str = "utf-8") -> None:
619
+ file=GeneralUtilities.normalize_path(file)
620
+ GeneralUtilities.append_lines_to_file(file, [line], encoding)
621
+
622
+ @staticmethod
623
+ @check_arguments
624
+ def append_lines_to_file(file: str, lines: list[str], encoding: str = "utf-8") -> None:
625
+ file=GeneralUtilities.normalize_path(file)
626
+ if len(lines) == 0:
627
+ return
628
+ is_first_line = True
629
+ for line in lines:
630
+ insert_linebreak: bool = None
631
+ if is_first_line:
632
+ insert_linebreak = GeneralUtilities.file_ends_with_content(file)
633
+ else:
634
+ insert_linebreak = True
635
+ line_to_write: str = None
636
+ if insert_linebreak:
637
+ line_to_write = "\n"+line
638
+ else:
639
+ line_to_write = line
640
+ with open(file, "r+b") as fileObject:
641
+ fileObject.seek(0, os.SEEK_END)
642
+ fileObject.write(GeneralUtilities.string_to_bytes(line_to_write, encoding))
643
+ is_first_line = False
644
+
645
+ @staticmethod
646
+ @check_arguments
647
+ def append_to_file(file: str, content: str, encoding: str = "utf-8") -> None:
648
+ file=GeneralUtilities.normalize_path(file)
649
+ GeneralUtilities.assert_condition(not "\n" in content, "Appending multiple lines is not allowed. Use append_lines_to_file instead.")
650
+ with open(file, "a", encoding=encoding) as fileObject:
651
+ fileObject.write(content)
652
+
653
+ @staticmethod
654
+ @check_arguments
655
+ def ensure_directory_exists(path: str) -> None:
656
+ path=GeneralUtilities.normalize_path(path)
657
+ if not os.path.isdir(path):
658
+ os.makedirs(path)
659
+
660
+ @staticmethod
661
+ @check_arguments
662
+ def ensure_file_exists(path: str) -> None:
663
+ path=GeneralUtilities.normalize_path(path)
664
+ if (not os.path.isfile(path)):
665
+ with open(path, "a+", encoding="utf-8"):
666
+ pass
667
+
668
+ @staticmethod
669
+ @check_arguments
670
+ def __remove_readonly(func, path, _):
671
+ os.chmod(path, stat.S_IWRITE)
672
+ func(path)
673
+
674
+ @staticmethod
675
+ @check_arguments
676
+ def __rmtree(directory: str) -> None:
677
+ shutil.rmtree(directory, onerror=GeneralUtilities.__remove_readonly) # pylint: disable=deprecated-argument
678
+
679
+ @staticmethod
680
+ @check_arguments
681
+ def ensure_directory_does_not_exist(path: str) -> None:
682
+ if (os.path.isdir(path)):
683
+ for root, dirs, files in os.walk(path, topdown=False):
684
+ for name in files:
685
+ filename = os.path.join(root, name)
686
+ os.chmod(filename, stat.S_IWUSR)
687
+ os.remove(filename)
688
+ for name in dirs:
689
+ GeneralUtilities.__rmtree(os.path.join(root, name))
690
+ GeneralUtilities.__rmtree(path)
691
+
692
+ @staticmethod
693
+ @check_arguments
694
+ def ensure_folder_exists_and_is_empty(path: str) -> None:
695
+ GeneralUtilities.ensure_directory_exists(path)
696
+ for filename in os.listdir(path):
697
+ file_path = os.path.join(path, filename)
698
+ if os.path.isfile(file_path):
699
+ os.remove(file_path)
700
+ if os.path.islink(file_path):
701
+ os.unlink(file_path)
702
+ elif os.path.isdir(file_path):
703
+ shutil.rmtree(file_path)
704
+
705
+ @staticmethod
706
+ @check_arguments
707
+ def ensure_file_does_not_exist(path: str) -> None:
708
+ if (os.path.isfile(path)):
709
+ os.remove(path)
710
+
711
+ @staticmethod
712
+ @check_arguments
713
+ def ensure_path_does_not_exist(path: str) -> None:
714
+ if (os.path.isfile(path)):
715
+ GeneralUtilities.ensure_file_does_not_exist(path)
716
+ if (os.path.isdir(path)):
717
+ GeneralUtilities.ensure_directory_does_not_exist(path)
718
+
719
+ @staticmethod
720
+ @check_arguments
721
+ def format_xml_file(filepath: str) -> None:
722
+ GeneralUtilities.format_xml_file_with_encoding(filepath, "utf-8")
723
+
724
+ @staticmethod
725
+ @check_arguments
726
+ def format_xml_file_with_encoding(filepath: str, encoding: str) -> None:
727
+ with open(filepath, 'r', encoding=encoding) as file:
728
+ text = file.read()
729
+ text = parse(text).toprettyxml()
730
+ with open(filepath, 'w', encoding=encoding) as file:
731
+ file.write(text)
732
+
733
+ @staticmethod
734
+ @check_arguments
735
+ def get_clusters_and_sectors_of_disk(diskpath: str) -> None:
736
+ GeneralUtilities.assert_condition(GeneralUtilities.current_system_is_windows(), "get_clusters_and_sectors_of_disk(diskpath) is only available on windows.")
737
+ sectorsPerCluster = ctypes.c_ulonglong(0)
738
+ bytesPerSector = ctypes.c_ulonglong(0)
739
+ rootPathName = ctypes.c_wchar_p(diskpath)
740
+ ctypes.windll.kernel32.GetDiskFreeSpaceW(rootPathName, ctypes.pointer(sectorsPerCluster), ctypes.pointer(bytesPerSector), None, None)
741
+ return (sectorsPerCluster.value, bytesPerSector.value)
742
+
743
+ @staticmethod
744
+ @check_arguments
745
+ def ensure_path_is_not_quoted(path: str) -> str:
746
+ if (path.startswith("\"") and path.endswith("\"")) or (path.startswith("'") and path.endswith("'")):
747
+ path = path[1:]
748
+ path = path[:-1]
749
+ return path
750
+ else:
751
+ return path
752
+
753
+ @staticmethod
754
+ @check_arguments
755
+ def get_missing_files(folderA: str, folderB: str) -> list:
756
+ folderA_length = len(folderA)
757
+ result = []
758
+ for fileA in GeneralUtilities.absolute_file_paths(folderA):
759
+ file = fileA[folderA_length:]
760
+ fileB = folderB + file
761
+ if not os.path.isfile(fileB):
762
+ result.append(fileB)
763
+ return result
764
+
765
+ @staticmethod
766
+ @check_arguments
767
+ def to_pascal_case(s: str) -> str:
768
+ return ''.join(current.lower() if prev.isalnum() else current.upper() for prev, current in zip(' ' + s, s) if current.isalnum())
769
+
770
+ @staticmethod
771
+ @check_arguments
772
+ def to_snake_case(s: str) -> str:
773
+ return '_'.join(t.lower() for t in re.split(r'[^a-zA-Z0-9]+', s) if t)
774
+
775
+ @staticmethod
776
+ @check_arguments
777
+ def to_camel_case(s: str) -> str:
778
+ words = [t.lower() for t in re.split(r'[^a-zA-Z0-9]+', s) if t]
779
+ return words[0] + ''.join(w.capitalize() for w in words[1:]) if words else ''
780
+
781
+ @staticmethod
782
+ @check_arguments
783
+ def to_kebab_case(s: str) -> str:
784
+ return '-'.join(t.lower() for t in re.split(r'[^a-zA-Z0-9]+', s) if t)
785
+
786
+ @staticmethod
787
+ @check_arguments
788
+ def find_between(s: str, start: str, end: str) -> str:
789
+ return s.split(start)[1].split(end)[0]
790
+
791
+ @staticmethod
792
+ @check_arguments
793
+ def write_lines_to_file(file: str, lines: list, encoding="utf-8") -> None:
794
+ lines = [GeneralUtilities.strip_new_line_character(line) for line in lines]
795
+ content = "\n".join(lines)
796
+ GeneralUtilities.write_text_to_file(file, content, encoding)
797
+
798
+ @staticmethod
799
+ @check_arguments
800
+ def write_text_to_file(file: str, content: str, encoding="utf-8") -> None:
801
+ GeneralUtilities.write_binary_to_file(file, bytes(bytearray(content, encoding)))
802
+
803
+ @staticmethod
804
+ @check_arguments
805
+ def write_binary_to_file(file: str, content: bytes) -> None:
806
+ with open(file, "wb") as file_object:
807
+ file_object.write(content)
808
+
809
+ @staticmethod
810
+ def is_binary_file(path: str):
811
+ content = GeneralUtilities.read_binary_from_file(path)
812
+ binary_content_indicators = [b'\x00', b'\x01', b'\x02', b'\x03', b'\x04', b'\x05', b'\x06', b'\x07', b'\x08', b'\x0E', b'\x1F']
813
+ for binary_content_indicator in binary_content_indicators:
814
+ if binary_content_indicator in content:
815
+ return True
816
+ return False
817
+
818
+ @staticmethod
819
+ @check_arguments
820
+ def read_lines_from_file(file: str, encoding="utf-8") -> list[str]:
821
+ content = GeneralUtilities.read_text_from_file(file, encoding)
822
+ if len(content) == 0:
823
+ return []
824
+ else:
825
+ return [GeneralUtilities.strip_new_line_character(line) for line in content.split('\n')]
826
+
827
+ @staticmethod
828
+ @check_arguments
829
+ def read_nonempty_lines_from_file(file: str, encoding="utf-8") -> list[str]:
830
+ return [line for line in GeneralUtilities.read_lines_from_file(file, encoding) if GeneralUtilities.string_has_content(line)]
831
+
832
+ @staticmethod
833
+ @check_arguments
834
+ def read_text_from_file(file: str, encoding="utf-8") -> str:
835
+ GeneralUtilities.assert_file_exists(file)
836
+ return GeneralUtilities.bytes_to_string(GeneralUtilities.read_binary_from_file(file), encoding)
837
+
838
+ @staticmethod
839
+ @check_arguments
840
+ def read_text_from_file_without_linebreak(file: str, encoding="utf-8") -> str:
841
+ return GeneralUtilities.read_text_from_file(file,encoding).replace("\n","").replace("\r","")
842
+
843
+ @staticmethod
844
+ @check_arguments
845
+ def read_binary_from_file(file: str) -> bytes:
846
+ try:
847
+ file=GeneralUtilities.normalize_path(file)
848
+ with open(file, "rb") as file_object:
849
+ return file_object.read()
850
+ except Exception as e:
851
+ raise ValueError(f"Could not read file '{file}'.") from e
852
+
853
+ @staticmethod
854
+ @check_arguments
855
+ def timedelta_to_simple_string(delta: timedelta) -> str:
856
+ return (datetime(1970, 1, 1, 0, 0, 0) + delta).strftime('%H:%M:%S')
857
+
858
+ @staticmethod
859
+ @check_arguments
860
+ def resolve_relative_path_from_current_working_directory(path: str) -> str:
861
+ return GeneralUtilities.resolve_relative_path(path, os.getcwd())
862
+
863
+ @staticmethod
864
+ @check_arguments
865
+ def resolve_relative_path(path: str, base_path: str):
866
+ if (os.path.isabs(path)):
867
+ return path
868
+ else:
869
+ return str(Path(os.path.join(base_path, path)).resolve())
870
+
871
+ @staticmethod
872
+ @check_arguments
873
+ def get_metadata_for_file_for_clone_folder_structure(file: str) -> str:
874
+ size = os.path.getsize(file)
875
+ last_modified_timestamp = os.path.getmtime(file)
876
+ hash_value = GeneralUtilities.get_sha256_of_file(file)
877
+ last_access_timestamp = os.path.getatime(file)
878
+ return f'{{"size":"{size}","sha256":"{hash_value}","mtime":"{last_modified_timestamp}","atime":"{last_access_timestamp}"}}'
879
+
880
+ @staticmethod
881
+ @check_arguments
882
+ def clone_folder_structure(source: str, target: str, copy_only_metadata: bool):
883
+ source = GeneralUtilities.resolve_relative_path(source, os.getcwd())
884
+ target = GeneralUtilities.resolve_relative_path(target, os.getcwd())
885
+ length_of_source = len(source)
886
+ for source_file in GeneralUtilities.absolute_file_paths(source):
887
+ target_file = target+source_file[length_of_source:]
888
+ GeneralUtilities.ensure_directory_exists(os.path.dirname(target_file))
889
+ if copy_only_metadata:
890
+ with open(target_file, 'w', encoding='utf8') as f:
891
+ f.write(GeneralUtilities.get_metadata_for_file_for_clone_folder_structure(source_file))
892
+ else:
893
+ shutil.copyfile(source_file, target_file)
894
+
895
+ @staticmethod
896
+ @check_arguments
897
+ def current_user_has_elevated_privileges() -> bool:
898
+ try:
899
+ return os.getuid() == 0
900
+ except AttributeError:
901
+ return ctypes.windll.shell32.IsUserAnAdmin() == 1
902
+
903
+ @staticmethod
904
+ @check_arguments
905
+ def ensure_elevated_privileges() -> None:
906
+ if (not GeneralUtilities.current_user_has_elevated_privileges()):
907
+ raise ValueError("Not enough privileges.")
908
+
909
+ @staticmethod
910
+ @check_arguments
911
+ def rename_names_of_all_files_and_folders(folder: str, replace_from: str, replace_to: str, replace_only_full_match=False):
912
+ for file in GeneralUtilities.get_direct_files_of_folder(folder):
913
+ GeneralUtilities.replace_in_filename(file, replace_from, replace_to, replace_only_full_match)
914
+ for sub_folder in GeneralUtilities.get_direct_folders_of_folder(folder):
915
+ GeneralUtilities.rename_names_of_all_files_and_folders(sub_folder, replace_from, replace_to, replace_only_full_match)
916
+ GeneralUtilities.replace_in_foldername(folder, replace_from, replace_to, replace_only_full_match)
917
+
918
+ @staticmethod
919
+ @check_arguments
920
+ def get_direct_files_of_folder(folder: str) -> list[str]:
921
+ result = [os.path.join(folder, f) for f in listdir(folder) if isfile(join(folder, f))]
922
+ result = sorted(result, key=str.casefold)
923
+ return result
924
+
925
+ @staticmethod
926
+ @check_arguments
927
+ def get_direct_folders_of_folder(folder: str) -> list[str]:
928
+ result = [os.path.join(folder, f) for f in listdir(folder) if isdir(join(folder, f))]
929
+ result = sorted(result, key=str.casefold)
930
+ return result
931
+
932
+ @staticmethod
933
+ @check_arguments
934
+ def get_all_files_of_folder(folder: str) -> list[str]:
935
+ result = list()
936
+ result.extend(GeneralUtilities.get_direct_files_of_folder(folder))
937
+ for subfolder in GeneralUtilities.get_direct_folders_of_folder(folder):
938
+ result.extend(GeneralUtilities.get_all_files_of_folder(subfolder))
939
+ result = sorted(result, key=str.casefold)
940
+ return result
941
+
942
+ @staticmethod
943
+ @check_arguments
944
+ def get_all_folders_of_folder(folder: str) -> list[str]:
945
+ result = list()
946
+ subfolders = GeneralUtilities.get_direct_folders_of_folder(folder)
947
+ result.extend(subfolders)
948
+ for subfolder in subfolders:
949
+ result.extend(GeneralUtilities.get_all_folders_of_folder(subfolder))
950
+ result = sorted(result, key=str.casefold)
951
+ return result
952
+
953
+ @staticmethod
954
+ @check_arguments
955
+ def get_all_objects_of_folder(folder: str) -> list[str]:
956
+ return sorted(GeneralUtilities.get_all_files_of_folder(folder) + GeneralUtilities.get_all_folders_of_folder(folder), key=str.casefold)
957
+
958
+ @staticmethod
959
+ @check_arguments
960
+ def replace_in_filename(file: str, replace_from: str, replace_to: str, replace_only_full_match=False):
961
+ filename = Path(file).name
962
+ if (GeneralUtilities.__should_get_replaced(filename, replace_from, replace_only_full_match)):
963
+ folder_of_file = os.path.dirname(file)
964
+ os.rename(file, os.path.join(folder_of_file, filename.replace(replace_from, replace_to)))
965
+
966
+ @staticmethod
967
+ @check_arguments
968
+ def replace_in_foldername(folder: str, replace_from: str, replace_to: str, replace_only_full_match=False):
969
+ foldername = Path(folder).name
970
+ if (GeneralUtilities.__should_get_replaced(foldername, replace_from, replace_only_full_match)):
971
+ folder_of_folder = os.path.dirname(folder)
972
+ os.rename(folder, os.path.join(folder_of_folder, foldername.replace(replace_from, replace_to)))
973
+
974
+ @staticmethod
975
+ @check_arguments
976
+ def __should_get_replaced(input_text, search_text, replace_only_full_match) -> bool:
977
+ if replace_only_full_match:
978
+ return input_text == search_text
979
+ else:
980
+ return search_text in input_text
981
+
982
+ @staticmethod
983
+ @check_arguments
984
+ def str_none_safe(variable) -> str:
985
+ if variable is None:
986
+ return ''
987
+ else:
988
+ return str(variable)
989
+
990
+ @staticmethod
991
+ @check_arguments
992
+ def arguments_to_array(arguments_as_string: str) -> list[str]:
993
+ if arguments_as_string is None:
994
+ return []
995
+ if GeneralUtilities.string_has_content(arguments_as_string):
996
+ return arguments_as_string.split(" ") # TODO this function should get improved to allow whitespaces in quote-substrings
997
+ else:
998
+ return []
999
+
1000
+ @staticmethod
1001
+ @check_arguments
1002
+ def arguments_to_array_for_log(arguments_as_string: str) -> list[str]:
1003
+ if arguments_as_string is None:
1004
+ return None
1005
+ return GeneralUtilities.arguments_to_array(arguments_as_string)
1006
+
1007
+ @staticmethod
1008
+ @check_arguments
1009
+ def get_sha256_of_file(file: str) -> str:
1010
+ sha256 = hashlib.sha256()
1011
+ with open(file, "rb") as fileObject:
1012
+ for chunk in iter(lambda: fileObject.read(4096), b""):
1013
+ sha256.update(chunk)
1014
+ return sha256.hexdigest()
1015
+
1016
+ @staticmethod
1017
+ @check_arguments
1018
+ def remove_duplicates(input_list) -> list:
1019
+ result = []
1020
+ for item in input_list:
1021
+ if not item in result:
1022
+ result.append(item)
1023
+ return result
1024
+
1025
+ @staticmethod
1026
+ @check_arguments
1027
+ def print_stacktrace() -> None:
1028
+ for line in traceback.format_stack():
1029
+ GeneralUtilities.write_message_to_stderr(line.strip())
1030
+
1031
+ @staticmethod
1032
+ @check_arguments
1033
+ def string_to_boolean(value: str) -> bool:
1034
+ value = value.strip().lower()
1035
+ if value in ('yes', 'y', 'true', 't', '1'):
1036
+ return True
1037
+ elif value in ('no', 'n', 'false', 'f', '0'):
1038
+ return False
1039
+ else:
1040
+ raise ValueError(f"Can not convert '{value}' to a boolean value")
1041
+
1042
+ @staticmethod
1043
+ @check_arguments
1044
+ def file_is_empty(file: str) -> bool:
1045
+ return os.stat(file).st_size == 0
1046
+
1047
+ @staticmethod
1048
+ @check_arguments
1049
+ def folder_is_empty(folder: str) -> bool:
1050
+ return len(GeneralUtilities.get_direct_files_of_folder(folder)) == 0 and len(GeneralUtilities.get_direct_folders_of_folder(folder)) == 0
1051
+
1052
+ @staticmethod
1053
+ @check_arguments
1054
+ def get_time_based_logfile_by_folder(folder: str, name: str = "Log") -> str:
1055
+ return os.path.join(GeneralUtilities.resolve_relative_path_from_current_working_directory(folder), f"{GeneralUtilities.get_time_based_logfilename(name)}.log")
1056
+
1057
+ @staticmethod
1058
+ @check_arguments
1059
+ def get_now() -> datetime:
1060
+ return datetime.now().astimezone().replace(microsecond=0)
1061
+
1062
+ @staticmethod
1063
+ @check_arguments
1064
+ def get_time_based_logfilename(name: str = "Log") -> str:
1065
+ d = GeneralUtilities.get_now()
1066
+ return f"{name}_{GeneralUtilities.datetime_to_string_for_logfile_name(d,False)}"
1067
+
1068
+ @staticmethod
1069
+ @check_arguments
1070
+ def bytes_to_string(payload: bytes, encoding: str = 'utf-8') -> str:
1071
+ return payload.decode(encoding, errors="ignore")
1072
+
1073
+ @staticmethod
1074
+ @check_arguments
1075
+ def string_to_bytes(payload: str, encoding: str = 'utf-8') -> bytes:
1076
+ return payload.encode(encoding, errors="ignore")
1077
+
1078
+ @staticmethod
1079
+ @check_arguments
1080
+ def contains_line(lines, regex: str) -> bool:
1081
+ for line in lines:
1082
+ if (re.match(regex, line)):
1083
+ return True
1084
+ return False
1085
+
1086
+ @staticmethod
1087
+ @check_arguments
1088
+ def read_csv_file(file: str, ignore_first_line: bool = False, treat_number_sign_at_begin_of_line_as_comment: bool = True, trim_values: bool = True, encoding="utf-8", ignore_empty_lines: bool = True, separator_character: str = ";", values_are_surrounded_by_quotes: bool = False) -> list[list[str]]:
1089
+ lines = GeneralUtilities.read_lines_from_file(file, encoding)
1090
+
1091
+ if ignore_first_line:
1092
+ lines = lines[1:]
1093
+ result = list()
1094
+ line: str = None
1095
+ for line_loopvariable in lines:
1096
+ use_line = True
1097
+ line = line_loopvariable
1098
+
1099
+ if trim_values:
1100
+ line = line.strip()
1101
+ if ignore_empty_lines:
1102
+ if not GeneralUtilities.string_has_content(line):
1103
+ use_line = False
1104
+
1105
+ if treat_number_sign_at_begin_of_line_as_comment:
1106
+ if line.startswith("#"):
1107
+ use_line = False
1108
+
1109
+ if use_line:
1110
+ if separator_character in line:
1111
+ raw_values_of_line = GeneralUtilities.to_list(line, separator_character)
1112
+ else:
1113
+ raw_values_of_line = [line]
1114
+ if trim_values:
1115
+ raw_values_of_line = [value.strip() for value in raw_values_of_line]
1116
+ values_of_line = []
1117
+ for raw_value_of_line in raw_values_of_line:
1118
+ value_of_line = raw_value_of_line
1119
+ if values_are_surrounded_by_quotes:
1120
+ value_of_line = value_of_line[1:]
1121
+ value_of_line = value_of_line[:-1]
1122
+ value_of_line = value_of_line.replace('""', '"')
1123
+ values_of_line.append(value_of_line)
1124
+ result.extend([values_of_line])
1125
+ return result
1126
+
1127
+ @staticmethod
1128
+ @check_arguments
1129
+ def epew_is_available() -> bool:
1130
+ return GeneralUtilities.tool_is_available("epew")
1131
+
1132
+ @staticmethod
1133
+ @check_arguments
1134
+ def tool_is_available(toolname: str) -> bool:
1135
+ try:
1136
+ return shutil.which(toolname) is not None
1137
+ except:
1138
+ return False
1139
+
1140
+ @staticmethod
1141
+ @check_arguments
1142
+ @deprecated
1143
+ def absolute_file_paths(directory: str) -> list[str]:
1144
+ return GeneralUtilities.get_all_files_of_folder(directory)
1145
+
1146
+ @staticmethod
1147
+ @check_arguments
1148
+ def to_list(list_as_string: str, separator: str = ",") -> list[str]:
1149
+ result = list()
1150
+ if list_as_string is not None:
1151
+ list_as_string = list_as_string.strip()
1152
+ if list_as_string == GeneralUtilities.empty_string:
1153
+ pass
1154
+ elif separator in list_as_string:
1155
+ for item in list_as_string.split(separator):
1156
+ result.append(item.strip())
1157
+ else:
1158
+ result.append(list_as_string)
1159
+ return result
1160
+
1161
+ @staticmethod
1162
+ @check_arguments
1163
+ def get_next_square_number(number: int) -> int:
1164
+ GeneralUtilities.assert_condition(number >= 0, "get_next_square_number is only applicable for nonnegative numbers")
1165
+ if number == 0:
1166
+ return 1
1167
+ root = 0
1168
+ square = 0
1169
+ while square < number:
1170
+ root = root+1
1171
+ square = root*root
1172
+ return root*root
1173
+
1174
+ @staticmethod
1175
+ @check_arguments
1176
+ def generate_password(length: int = 16, alphabet: str = None) -> None:
1177
+ if alphabet is None:
1178
+ alphabet = strin.ascii_letters + strin.digits+"_"
1179
+ return GeneralUtilities.empty_string.join(secrets.choice(alphabet) for i in range(length))
1180
+
1181
+ @staticmethod
1182
+ @check_arguments
1183
+ def assert_condition(condition: bool, information: str = None) -> None:
1184
+ """Throws an exception if the condition is false."""
1185
+ if (not condition):
1186
+ if information is None:
1187
+ information = "Internal assertion error."
1188
+ raise ValueError("Condition failed. "+information)
1189
+
1190
+ @staticmethod
1191
+ def current_system_is_windows():
1192
+ return platform.system() == 'Windows'
1193
+
1194
+ @staticmethod
1195
+ def current_system_is_linux():
1196
+ return platform.system() == 'Linux'
1197
+
1198
+ @staticmethod
1199
+ def current_system_is_macos():
1200
+ return platform.system() == 'Darwin'
1201
+
1202
+ @staticmethod
1203
+ def get_temp_folder() -> str:
1204
+ """Returns an absolute path to the system-temp-folder. Use this instead of tempfile.gettempdir() directly:
1205
+ in some Linux-build-containers TMPDIR/TEMP/TMP is set to a relative value, which makes tempfile.gettempdir()
1206
+ return a relative path. A relative temp-path would create temp-content relative to the current working-directory
1207
+ (e.g. inside a codeunit-folder) and would not be cleaned up reliably. In that case the platform-default absolute
1208
+ temp-folder is used instead."""
1209
+ temp_folder = tempfile.gettempdir()
1210
+ if not os.path.isabs(temp_folder):
1211
+ if GeneralUtilities.current_system_is_windows():
1212
+ temp_folder = os.path.join(os.environ.get("SystemRoot", "C:\\Windows"), "Temp")
1213
+ else:
1214
+ temp_folder = "/tmp"
1215
+ return temp_folder
1216
+
1217
+ @staticmethod
1218
+ def _internal_with_absolute_temp_folder_environment_variables(environment: dict) -> dict:
1219
+ """Returns a copy of the given environment-dictionary in which the temp-folder-environment-variables (TMPDIR/TMP/TEMP)
1220
+ are replaced by the absolute system-temp-folder if they are set to a relative value. This is required because spawned
1221
+ child-processes resolve their temp-folder via these variables (e.g. .NET-tools like the coverlet-collector or CycloneDX
1222
+ via Path.GetTempPath()); a relative value (as set in some Linux-build-containers) would otherwise make them create
1223
+ temp-content relative to their current working-directory (e.g. a leftover 'tmp/<guid>'-folder inside a codeunit-folder)
1224
+ instead of in the system-temp-folder."""
1225
+ result = dict(environment)
1226
+ absolute_temp_folder = GeneralUtilities.get_temp_folder()
1227
+ for temp_environment_variable_name in ("TMPDIR", "TMP", "TEMP"):
1228
+ value = result.get(temp_environment_variable_name)
1229
+ if value is not None and not os.path.isabs(value):
1230
+ result[temp_environment_variable_name] = absolute_temp_folder
1231
+ return result
1232
+
1233
+ @staticmethod
1234
+ @check_arguments
1235
+ def get_line():
1236
+ return "--------------------------"
1237
+
1238
+ @staticmethod
1239
+ def get_longline():
1240
+ return GeneralUtilities.get_line() + GeneralUtilities.get_line()
1241
+
1242
+ @staticmethod
1243
+ @check_arguments
1244
+ def get_icon_check_empty(positive: bool) -> str:
1245
+ if positive:
1246
+ return "✅"
1247
+ else:
1248
+ return GeneralUtilities.empty_string
1249
+
1250
+ @staticmethod
1251
+ @check_arguments
1252
+ def get_icon_check_cross(positive: bool) -> str:
1253
+ if positive:
1254
+ return "✅"
1255
+ else:
1256
+ return "❌"
1257
+
1258
+ @staticmethod
1259
+ @check_arguments
1260
+ def get_certificate_expiry_date(certificate_file: str) -> datetime:
1261
+ with open(certificate_file, encoding="utf-8") as certificate_file_content:
1262
+ cert = crypto.load_certificate(crypto.FILETYPE_PEM, certificate_file_content.read())
1263
+ date_as_bytes = cert.get_notAfter()
1264
+ date_as_string = date_as_bytes.decode("utf-8")
1265
+ result = datetime.strptime(date_as_string, '%Y%m%d%H%M%SZ')
1266
+ return result
1267
+
1268
+ @staticmethod
1269
+ @check_arguments
1270
+ def certificate_is_expired(certificate_file: str) -> bool:
1271
+ return GeneralUtilities.get_certificate_expiry_date(certificate_file) < GeneralUtilities.get_now()
1272
+
1273
+ @staticmethod
1274
+ @check_arguments
1275
+ def internet_connection_is_available() -> bool:
1276
+ """Returns True as soon as any of a small set of well-known public hosts can be reached.
1277
+ Returns False only when none of the hosts answered (= no internet connectivity)."""
1278
+ hosts: list[str] = [
1279
+ "https://www.google.com",
1280
+ "https://www.wikipedia.org",
1281
+ "https://www.github.com",
1282
+ "https://www.cloudflare.com",
1283
+ ]
1284
+ for host in hosts:
1285
+ try:
1286
+ with urllib.request.urlopen(host, timeout=10):
1287
+ return True
1288
+ except Exception:
1289
+ continue
1290
+ return False
1291
+
1292
+ @staticmethod
1293
+ @check_arguments
1294
+ def replace_variable_in_string(input_string: str, variable_name: str, variable_value: str) -> str:
1295
+ GeneralUtilities.assert_condition(not "__" in variable_name, f"'{variable_name}' is an invalid variable name because it contains '__' which is treated as control-sequence.")
1296
+ return input_string.replace(f"__[{variable_name}]__", variable_value)
1297
+
1298
+ @staticmethod
1299
+ @check_arguments
1300
+ def replace_variable(prefix:str, variable_name:str, suffix:str, value:str, content: str) -> str:
1301
+ GeneralUtilities.assert_condition(not "__" in variable_name, f"'{variable_name}' is an invalid variable name because it contains '__' which is treated as control-sequence.")
1302
+ pattern = re.escape(GeneralUtilities.str_none_safe( prefix)) + r"\s*" +"__"+ re.escape(variable_name) + "__"+r"\s*" + re.escape(GeneralUtilities.str_none_safe( suffix))
1303
+ result:str= re.sub(pattern, value, content)
1304
+ GeneralUtilities.assert_condition(not f"__{variable_name}__" in result, f"Variable '{variable_name}' was not replaced in the content. This is likely caused by an error in the content or the variable name.")
1305
+ return result
1306
+
1307
+ @staticmethod
1308
+ @check_arguments
1309
+ def input(prompt: str, print_result: bool) -> str: # This function is a workaround for usescases like python scripts which calls input(...) using epew because then the prompt is not printed by the built-in-input-function.
1310
+ GeneralUtilities.write_message_to_stdout(prompt)
1311
+ result: str = input()
1312
+ if print_result:
1313
+ GeneralUtilities.write_message_to_stdout(f"Result: {result}")
1314
+ return result
1315
+
1316
+ @staticmethod
1317
+ @check_arguments
1318
+ def run_program_simple(program: str, arguments: list[str], cwd: str = None) -> tuple[int, str, str]:
1319
+ if cwd is None:
1320
+ cwd = os.getcwd()
1321
+ cmd = [program]+arguments
1322
+ with subprocess.Popen(cmd, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE) as process:
1323
+ stdout, stderr = process.communicate()
1324
+ exit_code = process.wait()
1325
+ return (exit_code, stdout, stderr)
1326
+
1327
+ @staticmethod
1328
+ @check_arguments
1329
+ def assert_file_exists(file: str,message=None) -> None:
1330
+ if message is None:
1331
+ message=f"File '{file}' does not exist."
1332
+ GeneralUtilities.assert_condition(os.path.isfile(file), message)
1333
+
1334
+ @staticmethod
1335
+ @check_arguments
1336
+ def assert_file_does_not_exist(file: str,message=None) -> None:
1337
+ if message is None:
1338
+ message=f"File '{file}' exists."
1339
+ GeneralUtilities.assert_condition(not os.path.isfile(file), message)
1340
+
1341
+ @staticmethod
1342
+ @check_arguments
1343
+ def assert_folder_exists(folder: str,message=None) -> None:
1344
+ if message is None:
1345
+ message=f"Folder '{folder}' does not exist."
1346
+ GeneralUtilities.assert_condition(os.path.isdir(folder),message )
1347
+
1348
+ @staticmethod
1349
+ @check_arguments
1350
+ def assert_folder_does_not_exist(folder: str,message=None) -> None:
1351
+ if message is None:
1352
+ message= f"Folder '{folder}' exists."
1353
+ GeneralUtilities.assert_condition(not os.path.isdir(folder), f"Folder '{folder}' exists.")
1354
+
1355
+ @staticmethod
1356
+ @check_arguments
1357
+ def assert_not_null(obj,message:str=None) -> str:
1358
+ if message is None:
1359
+ message="Variable is not set"
1360
+ GeneralUtilities.assert_condition(obj is not None, message)
1361
+
1362
+ @staticmethod
1363
+ @check_arguments
1364
+ def retry_action(action, amount_of_attempts: int, action_name: str = None,delay_in_seconds:int=2) -> None:
1365
+ amount_of_fails = 0
1366
+ last_exception:Exception=None
1367
+ GeneralUtilities.assert_condition(0<amount_of_attempts,"amount_of_attempts must be greater than 0.")
1368
+ while amount_of_fails<amount_of_attempts:
1369
+ try:
1370
+ result = action()
1371
+ return result
1372
+ except Exception as e:
1373
+ time.sleep(delay_in_seconds)
1374
+ amount_of_fails = amount_of_fails+1
1375
+ last_exception=e
1376
+ GeneralUtilities.assert_not_null(last_exception)
1377
+ message = "Action"
1378
+ if action_name is not None:
1379
+ message = f"{message} \"{action_name}\""
1380
+ message = f"{message} failed {amount_of_attempts} time(s)."
1381
+ GeneralUtilities.write_message_to_stderr(message)
1382
+ raise last_exception
1383
+
1384
+
1385
+ @staticmethod
1386
+ @check_arguments
1387
+ def normalize_path(path)->str:
1388
+ path=str(path)
1389
+ path=os.path.expandvars(path)
1390
+ if GeneralUtilities.current_system_is_windows():
1391
+ path=path.replace("/","\\")
1392
+ else:
1393
+ path=path.replace("\\","/")
1394
+ return path
1395
+
1396
+ @staticmethod
1397
+ @check_arguments
1398
+ def int_to_string(number: int, leading_zeroplaces: int, trailing_zeroplaces: int) -> str:
1399
+ return GeneralUtilities.float_to_string(float(number), leading_zeroplaces, trailing_zeroplaces)
1400
+
1401
+ @staticmethod
1402
+ @check_arguments
1403
+ def float_to_string(number: float, leading_zeroplaces: int, trailing_zeroplaces: int) -> str:
1404
+ plain_str = str(number)
1405
+ GeneralUtilities.assert_condition("." in plain_str)
1406
+ splitted: list[str] = plain_str.split(".")
1407
+ return splitted[0].zfill(leading_zeroplaces)+"."+splitted[1].ljust(trailing_zeroplaces, '0')
1408
+
1409
+ @staticmethod
1410
+ @check_arguments
1411
+ def process_is_running_by_name(process_name: str) -> bool:
1412
+ processes: list[psutil.Process] = list(psutil.process_iter())
1413
+ for p in processes:
1414
+ if p.name() == process_name:
1415
+ return True
1416
+ return False
1417
+
1418
+ @staticmethod
1419
+ @check_arguments
1420
+ def process_is_running_by_id(process_id: int) -> bool:
1421
+ processes: list[psutil.Process] = list(psutil.process_iter())
1422
+ for p in processes:
1423
+ if p.pid == process_id:
1424
+ return True
1425
+ return False
1426
+
1427
+ @staticmethod
1428
+ @check_arguments
1429
+ def kill_process(process_id: int, include_child_processes: bool) -> bool:
1430
+ if GeneralUtilities. process_is_running_by_id(process_id):
1431
+ GeneralUtilities.write_message_to_stdout(f"Process with id {process_id} is running. Terminating it...")
1432
+ process = psutil.Process(process_id)
1433
+ if include_child_processes:
1434
+ for child in process.children(recursive=True):
1435
+ if GeneralUtilities.process_is_running_by_id(child.pid):
1436
+ child.kill()
1437
+ if GeneralUtilities.process_is_running_by_id(process_id):
1438
+ process.kill()
1439
+ else:
1440
+ GeneralUtilities.write_message_to_stdout(f"Process with id {process_id} is not running anymore.")
1441
+
1442
+ @staticmethod
1443
+ @check_arguments
1444
+ def get_only_item_from_list(list_with_one_element:list):
1445
+ GeneralUtilities.assert_condition(len(list_with_one_element)==1,f"List does not contain exactly one item. It contains {len(list_with_one_element)} items.")
1446
+ return list_with_one_element[0]
1447
+
1448
+ @staticmethod
1449
+ @check_arguments
1450
+ def trim_newlines(s: str) -> str:
1451
+ return s.strip("\n")
1452
+
1453
+ @staticmethod
1454
+ @check_arguments
1455
+ def log_merger(folder:str):
1456
+ """For each log file in this folder this function looks for log-rotation-files and merges them together again into one file."""
1457
+ all_files=GeneralUtilities.get_direct_files_of_folder(folder)
1458
+ for log_file in all_files:
1459
+ if "AutoRename" in log_file:
1460
+ continue
1461
+ filename=os.path.basename(log_file)
1462
+ filename_without_extension=Path(log_file).stem
1463
+ if not ".archive." in filename:
1464
+
1465
+ #merge with rotated logs
1466
+ if filename.endswith(".log") and not ".archive." in filename:
1467
+ rotated_log_files=sorted([f for f in all_files if os.path.basename(f).startswith(filename_without_extension+".archive.")], key=GeneralUtilities._internal_extract_log_file_number)
1468
+ result=GeneralUtilities.empty_string
1469
+ if 0<len(rotated_log_files):
1470
+ for rotated_log_file in rotated_log_files:
1471
+ result+="\n"+GeneralUtilities.read_text_from_file(rotated_log_file)
1472
+ result+="\n"+GeneralUtilities.read_text_from_file(log_file)
1473
+ GeneralUtilities.write_text_to_file(log_file, result)
1474
+ for rotated_log_file in rotated_log_files:
1475
+ GeneralUtilities.ensure_file_does_not_exist(rotated_log_file)
1476
+
1477
+ #normalize
1478
+ logs=GeneralUtilities.read_text_from_file(log_file)
1479
+ logs=logs.replace("\r\n", "\n")
1480
+ logs=logs.replace("\r", GeneralUtilities.empty_string)
1481
+ logs=re.sub(r"\n+", "\n", logs)
1482
+ logs=GeneralUtilities.trim_newlines(logs)
1483
+ GeneralUtilities.write_text_to_file(log_file, logs)
1484
+
1485
+ @staticmethod
1486
+ @check_arguments
1487
+ def _internal_extract_log_file_number(filename: str) -> int:
1488
+ m = re.search(r"\.archive\.(\d+)\.log$", filename)
1489
+ if m:
1490
+ return int(m.group(1))
1491
+ else:
1492
+ raise ValueError(f"Filename '{filename}' does not match the expected pattern for rotated log files.")
1493
+
1494
+ @staticmethod
1495
+ @check_arguments
1496
+ def _internal_load_resource(relative_path: str) -> bytes:
1497
+ res = resources.files("ScriptCollection.Resources")
1498
+ for part in relative_path.split("/"):
1499
+ res = res.joinpath(part)
1500
+ result= res.read_bytes()
1501
+ return result
1502
+
1503
+ @staticmethod
1504
+ @check_arguments
1505
+ def escape_json_string_value(str_value: str) -> str:
1506
+ """For input 'a"b\\c' this function returns 'a\\"b\\\\c'. This function does not add surrounding quotes. It only escapes the characters inside the string."""
1507
+ json_escaped = json.dumps(str_value)
1508
+ return json_escaped[1:-1]
1509
+
1510
+ @staticmethod
1511
+ @check_arguments
1512
+ def escape_json_property_value(str_value: str) -> str:
1513
+ # remove all characters not in [a-zA-Z0-9_]
1514
+ sanitized = re.sub(r'[^a-zA-Z0-9_]', '', str_value)
1515
+
1516
+ # check if result starts with [a-zA-Z_]
1517
+ if not sanitized or len(sanitized) == 0 or not re.match(r'[a-zA-Z_]', sanitized[0]):
1518
+ raise ValueError(f"Invalid JSON property name after sanitizing: '{sanitized}'. Value must start with [a-zA-Z_].")
1519
+
1520
+ return sanitized
1521
+
1522
+ @staticmethod
1523
+ @check_arguments
1524
+ def escape_yaml_string_value(str_value: str) -> str:
1525
+ """For input 'a"b\\c' this function returns 'a\\"b\\\\c'. This function does not add surrounding quotes. It only escapes the characters inside the string."""
1526
+ result= GeneralUtilities.escape_json_string_value(str_value)
1527
+ #do other adaptions here if desired/required
1528
+ return result
1529
+
1530
+ @staticmethod
1531
+ @check_arguments
1532
+ def escape_yaml_property_value(str_value: str) -> str:
1533
+ result= GeneralUtilities.escape_json_property_value(str_value)
1534
+ #do other adaptions here if desired/required
1535
+ return result
1536
+
1537
+
1538
+ @staticmethod
1539
+ @check_arguments
1540
+ def get_current_platform() -> Platform:
1541
+ system = platform.system().lower()
1542
+ arch = platform.machine().lower()
1543
+
1544
+ if system == "windows" and GeneralUtilities. system_is_x64(arch):
1545
+ return Platform.Windows_AMD64
1546
+ elif system == "linux" and GeneralUtilities.system_is_x64(arch):
1547
+ return Platform.Linux_AMD64
1548
+ elif system == "linux" and GeneralUtilities.system_is_arm64(arch):
1549
+ return Platform.Linux_ARM64
1550
+ elif system == "darwin" and GeneralUtilities.system_is_arm64(arch):
1551
+ return Platform.MacOS_ARM64
1552
+ else:
1553
+ raise ValueError(f"Unsupported platform: {system}/{arch}")
1554
+
1555
+ @staticmethod
1556
+ @check_arguments
1557
+ def current_system_is_x64():
1558
+ return GeneralUtilities.system_is_x64(platform.machine().lower())
1559
+
1560
+ @staticmethod
1561
+ @check_arguments
1562
+ def system_is_x64(arch:str):
1563
+ return arch in ("x86_64", "amd64")
1564
+
1565
+ @staticmethod
1566
+ @check_arguments
1567
+ def current_system_is_arm64():
1568
+ return GeneralUtilities.system_is_arm64(platform.machine().lower())
1569
+
1570
+ @staticmethod
1571
+ @check_arguments
1572
+ def system_is_arm64(arch:str):
1573
+ return arch in ("arm", "arm64", "aarch64")
1574
+
1575
+ @staticmethod
1576
+ @check_arguments
1577
+ def platform_to_short_str(platform_value: Platform) -> str:
1578
+ mapping = {
1579
+ Platform.Windows_AMD64: "win-x64",
1580
+ Platform.Linux_AMD64: "linux-x64",
1581
+ Platform.Linux_ARM64: "linux-arm64",
1582
+ Platform.MacOS_ARM64: "osx-arm64",
1583
+ }
1584
+ return mapping[platform_value]
1585
+
1586
+ @staticmethod
1587
+ @check_arguments
1588
+ def platform_from_short_str(platform_str: str) -> Platform:
1589
+ mapping = {
1590
+ "win-x64": Platform.Windows_AMD64,
1591
+ "linux-x64": Platform.Linux_AMD64,
1592
+ "linux-arm64": Platform.Linux_ARM64,
1593
+ "osx-arm64": Platform.MacOS_ARM64,
1594
+ }
1595
+ if platform_str not in mapping:
1596
+ raise ValueError(f"Unsupported platform string: {platform_str}")
1597
+ return mapping[platform_str]
1598
+
1599
+ @staticmethod
1600
+ @check_arguments
1601
+ def platform_to_dash_str(platform_value: Platform) -> str:
1602
+ mapping = {
1603
+ Platform.Windows_AMD64: "Windows-x64",
1604
+ Platform.Linux_AMD64: "Linux-x64",
1605
+ Platform.Linux_ARM64: "Linux-arm64",
1606
+ Platform.MacOS_ARM64: "MacOS-arm64",
1607
+ }
1608
+ return mapping[platform_value]
1609
+
1610
+ @staticmethod
1611
+ @check_arguments
1612
+ def platform_from_dash_str(platform_str: str) -> Platform:
1613
+ mapping = {
1614
+ "Windows-x64": Platform.Windows_AMD64,
1615
+ "Linux-x64": Platform.Linux_AMD64,
1616
+ "Linux-arm64": Platform.Linux_ARM64,
1617
+ "MacOS-arm64": Platform.MacOS_ARM64,
1618
+ }
1619
+ if platform_str not in mapping:
1620
+ raise ValueError(f"Unsupported platform string: {platform_str}")
1621
+ return mapping[platform_str]
1622
+
1623
+ @staticmethod
1624
+ @check_arguments
1625
+ def platform_to_docker_platform_str(platform_value: Platform) -> str:
1626
+ mapping = {
1627
+ Platform.Windows_AMD64: "windows/amd64",
1628
+ Platform.Linux_AMD64: "linux/amd64",
1629
+ Platform.Linux_ARM64: "linux/arm64",
1630
+ Platform.MacOS_ARM64: "linux/arm64", # macOS → linux container
1631
+ }
1632
+ return mapping[platform_value]
1633
+
1634
+ @staticmethod
1635
+ @check_arguments
1636
+ def platform_to_dotnet_runtime_identifier(platform_value: Platform) -> str:
1637
+ mapping = {
1638
+ Platform.Windows_AMD64: "win-x64",
1639
+ Platform.Linux_AMD64: "linux-x64",
1640
+ Platform.Linux_ARM64: "linux-arm64",
1641
+ Platform.MacOS_ARM64: "osx-arm64",
1642
+ }
1643
+ return mapping[platform_value]
1644
+
1645
+ @staticmethod
1646
+ @check_arguments
1647
+ def platform_to_go_runtime_identifier(platform_value: Platform) -> str:
1648
+ mapping = {
1649
+ Platform.Linux_AMD64: "amd64",
1650
+ Platform.Linux_ARM64: "arm64",
1651
+ }
1652
+ return mapping[platform_value]
1653
+
1654
+ @staticmethod
1655
+ @check_arguments
1656
+ def get_all_platforms() -> list[Platform]:
1657
+ return [
1658
+ Platform.Windows_AMD64,
1659
+ Platform.Linux_AMD64,
1660
+ Platform.Linux_ARM64,
1661
+ Platform.MacOS_ARM64,
1662
+ ]
1663
+
1664
+ @staticmethod
1665
+ @check_arguments
1666
+ def get_scriptcollection_configuration_folder() -> str:
1667
+ result = os.path.join(str(Path.home()), ".ScriptCollection")
1668
+ result = GeneralUtilities.normalize_path(result)
1669
+ GeneralUtilities.ensure_directory_exists(result)
1670
+ return result
1671
+
1672
+ @staticmethod
1673
+ @check_arguments
1674
+ def get_python_executable() -> str:
1675
+ configured_file = os.path.join(GeneralUtilities.get_scriptcollection_configuration_folder(), "PythonExecutable.txt")
1676
+ if os.path.isfile(configured_file):
1677
+ result = GeneralUtilities.read_text_from_file(configured_file).strip()
1678
+ GeneralUtilities.assert_condition(os.path.isfile(result), f"Python executable does not exist: '{result}'")
1679
+ elif GeneralUtilities.string_has_content(sys.executable):
1680
+ result = sys.executable
1681
+ else:
1682
+ result = "python"
1683
+ return result
1684
+
1685
+ @staticmethod
1686
+ @check_arguments
1687
+ def get_docker_executable() -> str:
1688
+ configured_file = os.path.join(GeneralUtilities.get_scriptcollection_configuration_folder(), "DockerExecutable.txt")
1689
+ if os.path.isfile(configured_file):
1690
+ result = GeneralUtilities.read_text_from_file(configured_file).strip()
1691
+ GeneralUtilities.assert_condition(os.path.isfile(result), f"Docker executable does not exist: '{result}'")
1692
+ elif GeneralUtilities.string_has_content(sys.executable):
1693
+ result = sys.executable
1694
+ else:
1695
+ result = "docker"
1696
+ return result