ScriptCollection 4.3.64__py3-none-any.whl → 4.4.1__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.
- ScriptCollection/AnionBuildPlatform.py +205 -205
- ScriptCollection/CertificateUpdater.py +157 -157
- ScriptCollection/Executables.py +4 -2
- ScriptCollection/GeneralUtilities.py +1696 -1696
- ScriptCollection/OCIImages/OCIImageManager.py +278 -278
- ScriptCollection/ProcessesRunner.py +43 -43
- ScriptCollection/ProgramRunnerSudo.py +110 -110
- ScriptCollection/SCLog.py +115 -115
- ScriptCollection/ScriptCollectionCore.py +4372 -4351
- ScriptCollection/TFCPS/DotNet/TFCPS_CodeUnitSpecific_DotNet.py +716 -709
- ScriptCollection/TFCPS/NodeJS/TFCPS_CodeUnitSpecific_NodeJS.py +235 -235
- ScriptCollection/TFCPS/Python/TFCPS_CodeUnitSpecific_Python.py +258 -258
- ScriptCollection/TFCPS/TFCPS_CodeUnitSpecific_Base.py +537 -524
- ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnit.py +157 -155
- ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnits.py +691 -681
- ScriptCollection/TFCPS/TFCPS_CreateRelease.py +98 -98
- ScriptCollection/TFCPS/TFCPS_Generic.py +44 -44
- ScriptCollection/TFCPS/TFCPS_MergeToMain.py +132 -132
- ScriptCollection/TFCPS/TFCPS_MergeToStable.py +358 -358
- ScriptCollection/TFCPS/TFCPS_PreBuildCodeunitsScript.py +48 -48
- ScriptCollection/TFCPS/TFCPS_Tools_General.py +30 -4
- {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.1.dist-info}/METADATA +168 -168
- {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.1.dist-info}/RECORD +26 -26
- {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.1.dist-info}/WHEEL +1 -1
- {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.1.dist-info}/entry_points.txt +0 -0
- {scriptcollection-4.3.64.dist-info → scriptcollection-4.4.1.dist-info}/top_level.txt +0 -0
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import psutil
|
|
2
|
-
from .GeneralUtilities import GeneralUtilities
|
|
3
|
-
from .ScriptCollectionCore import ScriptCollectionCore
|
|
4
|
-
|
|
5
|
-
# runs multiple processes in parallel and terminate all if at least one is terminated
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class ProcessStartInformation:
|
|
9
|
-
workingdirectory: str = None
|
|
10
|
-
program: str = None
|
|
11
|
-
arguments: str = None
|
|
12
|
-
|
|
13
|
-
def __init__(self, workingdirectory: str, program: str, arguments: str):
|
|
14
|
-
self.workingdirectory = workingdirectory
|
|
15
|
-
self.program = program
|
|
16
|
-
self.arguments = arguments
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class ProcessesRunner:
|
|
20
|
-
sc: ScriptCollectionCore = None
|
|
21
|
-
processes: list[ProcessStartInformation] = None
|
|
22
|
-
|
|
23
|
-
def __init__(self, processes: list[ProcessStartInformation]):
|
|
24
|
-
self.sc = ScriptCollectionCore()
|
|
25
|
-
self.processes = processes
|
|
26
|
-
|
|
27
|
-
@GeneralUtilities.check_arguments
|
|
28
|
-
def run(self):
|
|
29
|
-
pids: list[int] = list[int]()
|
|
30
|
-
for processstartinfo in self.processes:
|
|
31
|
-
pids.append(self.sc.run_program_async(processstartinfo.program, processstartinfo.argumentss, processstartinfo.workingdirectory))
|
|
32
|
-
enabled = True
|
|
33
|
-
while enabled:
|
|
34
|
-
for pid in pids:
|
|
35
|
-
if not psutil.pid_exists(pid):
|
|
36
|
-
enabled = False
|
|
37
|
-
# one program terminate so exit and terminate all now
|
|
38
|
-
processes = psutil.process_iter()
|
|
39
|
-
for pid in pids:
|
|
40
|
-
if psutil.pid_exists(pid):
|
|
41
|
-
for proc in processes:
|
|
42
|
-
if proc.pid == pid:
|
|
43
|
-
proc.kill()
|
|
1
|
+
import psutil
|
|
2
|
+
from .GeneralUtilities import GeneralUtilities
|
|
3
|
+
from .ScriptCollectionCore import ScriptCollectionCore
|
|
4
|
+
|
|
5
|
+
# runs multiple processes in parallel and terminate all if at least one is terminated
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ProcessStartInformation:
|
|
9
|
+
workingdirectory: str = None
|
|
10
|
+
program: str = None
|
|
11
|
+
arguments: str = None
|
|
12
|
+
|
|
13
|
+
def __init__(self, workingdirectory: str, program: str, arguments: str):
|
|
14
|
+
self.workingdirectory = workingdirectory
|
|
15
|
+
self.program = program
|
|
16
|
+
self.arguments = arguments
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ProcessesRunner:
|
|
20
|
+
sc: ScriptCollectionCore = None
|
|
21
|
+
processes: list[ProcessStartInformation] = None
|
|
22
|
+
|
|
23
|
+
def __init__(self, processes: list[ProcessStartInformation]):
|
|
24
|
+
self.sc = ScriptCollectionCore()
|
|
25
|
+
self.processes = processes
|
|
26
|
+
|
|
27
|
+
@GeneralUtilities.check_arguments
|
|
28
|
+
def run(self):
|
|
29
|
+
pids: list[int] = list[int]()
|
|
30
|
+
for processstartinfo in self.processes:
|
|
31
|
+
pids.append(self.sc.run_program_async(processstartinfo.program, processstartinfo.argumentss, processstartinfo.workingdirectory))
|
|
32
|
+
enabled = True
|
|
33
|
+
while enabled:
|
|
34
|
+
for pid in pids:
|
|
35
|
+
if not psutil.pid_exists(pid):
|
|
36
|
+
enabled = False
|
|
37
|
+
# one program terminate so exit and terminate all now
|
|
38
|
+
processes = psutil.process_iter()
|
|
39
|
+
for pid in pids:
|
|
40
|
+
if psutil.pid_exists(pid):
|
|
41
|
+
for proc in processes:
|
|
42
|
+
if proc.pid == pid:
|
|
43
|
+
proc.kill()
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
from subprocess import Popen
|
|
2
|
-
from .GeneralUtilities import GeneralUtilities
|
|
3
|
-
from .ProgramRunnerBase import ProgramRunnerBase
|
|
4
|
-
from .ScriptCollectionCore import ScriptCollectionCore
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class SudoPopenReader:
|
|
8
|
-
content: bytes = None
|
|
9
|
-
|
|
10
|
-
def __init__(self, content: bytes):
|
|
11
|
-
self.content = content
|
|
12
|
-
|
|
13
|
-
def readable(self) -> bool:
|
|
14
|
-
return True
|
|
15
|
-
|
|
16
|
-
def read(self) -> bytes:
|
|
17
|
-
return self.content
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
class SudoPopen:
|
|
21
|
-
returncode: int = None
|
|
22
|
-
stdout_str: str = None
|
|
23
|
-
stderr_str: str = None
|
|
24
|
-
pid: int = None
|
|
25
|
-
stdout: bytes = None
|
|
26
|
-
stderr: bytes = None
|
|
27
|
-
|
|
28
|
-
def __init__(self, exitcode: int, stdout: str, stderr: str, pid: int):
|
|
29
|
-
self.returncode: int = exitcode
|
|
30
|
-
self.stdout_str: str = stdout
|
|
31
|
-
self.stdout = str.encode(self.stdout_str)
|
|
32
|
-
self.stderr_str: str = stderr
|
|
33
|
-
self.stderr = str.encode(self.stderr_str)
|
|
34
|
-
self.pid = pid
|
|
35
|
-
|
|
36
|
-
def communicate(self):
|
|
37
|
-
return (self.stdout, self.stderr)
|
|
38
|
-
|
|
39
|
-
def wait(self):
|
|
40
|
-
return self.returncode
|
|
41
|
-
|
|
42
|
-
def poll(self) -> object:
|
|
43
|
-
return self.returncode
|
|
44
|
-
|
|
45
|
-
def __enter__(self):
|
|
46
|
-
return self
|
|
47
|
-
|
|
48
|
-
def __exit__(self, exc_type, exc_value, traceback):
|
|
49
|
-
pass
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
class ProgramRunnerSudo(ProgramRunnerBase):
|
|
53
|
-
__sc: ScriptCollectionCore = None
|
|
54
|
-
__password: str = None
|
|
55
|
-
|
|
56
|
-
@GeneralUtilities.check_arguments
|
|
57
|
-
def __init__(self,user_password:str):
|
|
58
|
-
GeneralUtilities.assert_condition(GeneralUtilities.current_system_is_linux(), "SudoRunner can only be only executed on Linux.")
|
|
59
|
-
self.__sc = ScriptCollectionCore()
|
|
60
|
-
self.__password = user_password
|
|
61
|
-
|
|
62
|
-
@GeneralUtilities.check_arguments
|
|
63
|
-
def will_be_executed_locally(self) -> bool:
|
|
64
|
-
return True
|
|
65
|
-
|
|
66
|
-
@GeneralUtilities.check_arguments
|
|
67
|
-
def run_program_internal(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, env_vars: dict = None) -> tuple[int, str, str, int]:
|
|
68
|
-
if env_vars is not None:
|
|
69
|
-
raise ValueError("env_vars is not supported in Sudo-runner")
|
|
70
|
-
argument = program+" " + ' '.join(GeneralUtilities.args_array_surround_with_quotes_if_required(arguments_as_array))
|
|
71
|
-
argument = f"echo {self.__password} | sudo -k -S {argument}" # TODO maybe add "exit" somewhere before argument or before sudo to correctly return the exit-code"
|
|
72
|
-
result = self.__sc.run_program_argsasarray("sh", ["-c", argument], working_directory)
|
|
73
|
-
return result
|
|
74
|
-
|
|
75
|
-
@GeneralUtilities.check_arguments
|
|
76
|
-
def run_program_argsasarray_async_helper(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> Popen:
|
|
77
|
-
if interactive:
|
|
78
|
-
raise ValueError("Interactive execution is not supported in Sudo-runner")
|
|
79
|
-
r: tuple[int, str, str, int] = self.run_program_internal(program, arguments_as_array, working_directory, custom_argument, env_vars)
|
|
80
|
-
popen: SudoPopen = SudoPopen(r[0], r[1], r[2], r[3])
|
|
81
|
-
return popen
|
|
82
|
-
|
|
83
|
-
# Return-values program_runner: Exitcode, StdOut, StdErr, Pid
|
|
84
|
-
@GeneralUtilities.check_arguments
|
|
85
|
-
def wait(self, process: Popen, custom_argument: object) -> tuple[int, str, str, int]:
|
|
86
|
-
raise ValueError("Wait is not supported in Sudo-runner")
|
|
87
|
-
|
|
88
|
-
# Return-values program_runner: Exitcode, StdOut, StdErr, Pid
|
|
89
|
-
@GeneralUtilities.check_arguments
|
|
90
|
-
def run_program_argsasarray(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> tuple[int, str, str, int]:
|
|
91
|
-
if interactive:
|
|
92
|
-
raise ValueError("Interactive execution is not supported in Sudo-runner")
|
|
93
|
-
return self.run_program_internal(program, arguments_as_array, working_directory, custom_argument, env_vars)
|
|
94
|
-
|
|
95
|
-
# Return-values program_runner: Exitcode, StdOut, StdErr, Pid
|
|
96
|
-
@GeneralUtilities.check_arguments
|
|
97
|
-
def run_program(self, program: str, arguments: str = "", working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> tuple[int, str, str, int]:
|
|
98
|
-
if interactive:
|
|
99
|
-
raise ValueError("Interactive execution is not supported in Sudo-runner")
|
|
100
|
-
return self.run_program_internal(program, arguments.split(" "), working_directory, custom_argument, env_vars)
|
|
101
|
-
|
|
102
|
-
# Return-values program_runner: Pid
|
|
103
|
-
@GeneralUtilities.check_arguments
|
|
104
|
-
def run_program_argsasarray_async(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> int:
|
|
105
|
-
raise ValueError("Async execution is not supported in Sudo-runner")
|
|
106
|
-
|
|
107
|
-
# Return-values program_runner: Pid
|
|
108
|
-
@GeneralUtilities.check_arguments
|
|
109
|
-
def run_program_async(self, program: str, arguments: str, working_directory: str, custom_argument: object, interactive: bool = False, env_vars: dict = None) -> int:
|
|
110
|
-
raise ValueError("Async execution is not supported in Sudo-runner")
|
|
1
|
+
from subprocess import Popen
|
|
2
|
+
from .GeneralUtilities import GeneralUtilities
|
|
3
|
+
from .ProgramRunnerBase import ProgramRunnerBase
|
|
4
|
+
from .ScriptCollectionCore import ScriptCollectionCore
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SudoPopenReader:
|
|
8
|
+
content: bytes = None
|
|
9
|
+
|
|
10
|
+
def __init__(self, content: bytes):
|
|
11
|
+
self.content = content
|
|
12
|
+
|
|
13
|
+
def readable(self) -> bool:
|
|
14
|
+
return True
|
|
15
|
+
|
|
16
|
+
def read(self) -> bytes:
|
|
17
|
+
return self.content
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class SudoPopen:
|
|
21
|
+
returncode: int = None
|
|
22
|
+
stdout_str: str = None
|
|
23
|
+
stderr_str: str = None
|
|
24
|
+
pid: int = None
|
|
25
|
+
stdout: bytes = None
|
|
26
|
+
stderr: bytes = None
|
|
27
|
+
|
|
28
|
+
def __init__(self, exitcode: int, stdout: str, stderr: str, pid: int):
|
|
29
|
+
self.returncode: int = exitcode
|
|
30
|
+
self.stdout_str: str = stdout
|
|
31
|
+
self.stdout = str.encode(self.stdout_str)
|
|
32
|
+
self.stderr_str: str = stderr
|
|
33
|
+
self.stderr = str.encode(self.stderr_str)
|
|
34
|
+
self.pid = pid
|
|
35
|
+
|
|
36
|
+
def communicate(self):
|
|
37
|
+
return (self.stdout, self.stderr)
|
|
38
|
+
|
|
39
|
+
def wait(self):
|
|
40
|
+
return self.returncode
|
|
41
|
+
|
|
42
|
+
def poll(self) -> object:
|
|
43
|
+
return self.returncode
|
|
44
|
+
|
|
45
|
+
def __enter__(self):
|
|
46
|
+
return self
|
|
47
|
+
|
|
48
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class ProgramRunnerSudo(ProgramRunnerBase):
|
|
53
|
+
__sc: ScriptCollectionCore = None
|
|
54
|
+
__password: str = None
|
|
55
|
+
|
|
56
|
+
@GeneralUtilities.check_arguments
|
|
57
|
+
def __init__(self,user_password:str):
|
|
58
|
+
GeneralUtilities.assert_condition(GeneralUtilities.current_system_is_linux(), "SudoRunner can only be only executed on Linux.")
|
|
59
|
+
self.__sc = ScriptCollectionCore()
|
|
60
|
+
self.__password = user_password
|
|
61
|
+
|
|
62
|
+
@GeneralUtilities.check_arguments
|
|
63
|
+
def will_be_executed_locally(self) -> bool:
|
|
64
|
+
return True
|
|
65
|
+
|
|
66
|
+
@GeneralUtilities.check_arguments
|
|
67
|
+
def run_program_internal(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, env_vars: dict = None) -> tuple[int, str, str, int]:
|
|
68
|
+
if env_vars is not None:
|
|
69
|
+
raise ValueError("env_vars is not supported in Sudo-runner")
|
|
70
|
+
argument = program+" " + ' '.join(GeneralUtilities.args_array_surround_with_quotes_if_required(arguments_as_array))
|
|
71
|
+
argument = f"echo {self.__password} | sudo -k -S {argument}" # TODO maybe add "exit" somewhere before argument or before sudo to correctly return the exit-code"
|
|
72
|
+
result = self.__sc.run_program_argsasarray("sh", ["-c", argument], working_directory)
|
|
73
|
+
return result
|
|
74
|
+
|
|
75
|
+
@GeneralUtilities.check_arguments
|
|
76
|
+
def run_program_argsasarray_async_helper(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> Popen:
|
|
77
|
+
if interactive:
|
|
78
|
+
raise ValueError("Interactive execution is not supported in Sudo-runner")
|
|
79
|
+
r: tuple[int, str, str, int] = self.run_program_internal(program, arguments_as_array, working_directory, custom_argument, env_vars)
|
|
80
|
+
popen: SudoPopen = SudoPopen(r[0], r[1], r[2], r[3])
|
|
81
|
+
return popen
|
|
82
|
+
|
|
83
|
+
# Return-values program_runner: Exitcode, StdOut, StdErr, Pid
|
|
84
|
+
@GeneralUtilities.check_arguments
|
|
85
|
+
def wait(self, process: Popen, custom_argument: object) -> tuple[int, str, str, int]:
|
|
86
|
+
raise ValueError("Wait is not supported in Sudo-runner")
|
|
87
|
+
|
|
88
|
+
# Return-values program_runner: Exitcode, StdOut, StdErr, Pid
|
|
89
|
+
@GeneralUtilities.check_arguments
|
|
90
|
+
def run_program_argsasarray(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> tuple[int, str, str, int]:
|
|
91
|
+
if interactive:
|
|
92
|
+
raise ValueError("Interactive execution is not supported in Sudo-runner")
|
|
93
|
+
return self.run_program_internal(program, arguments_as_array, working_directory, custom_argument, env_vars)
|
|
94
|
+
|
|
95
|
+
# Return-values program_runner: Exitcode, StdOut, StdErr, Pid
|
|
96
|
+
@GeneralUtilities.check_arguments
|
|
97
|
+
def run_program(self, program: str, arguments: str = "", working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> tuple[int, str, str, int]:
|
|
98
|
+
if interactive:
|
|
99
|
+
raise ValueError("Interactive execution is not supported in Sudo-runner")
|
|
100
|
+
return self.run_program_internal(program, arguments.split(" "), working_directory, custom_argument, env_vars)
|
|
101
|
+
|
|
102
|
+
# Return-values program_runner: Pid
|
|
103
|
+
@GeneralUtilities.check_arguments
|
|
104
|
+
def run_program_argsasarray_async(self, program: str, arguments_as_array: list[str] = [], working_directory: str = None, custom_argument: object = None, interactive: bool = False, env_vars: dict = None) -> int:
|
|
105
|
+
raise ValueError("Async execution is not supported in Sudo-runner")
|
|
106
|
+
|
|
107
|
+
# Return-values program_runner: Pid
|
|
108
|
+
@GeneralUtilities.check_arguments
|
|
109
|
+
def run_program_async(self, program: str, arguments: str, working_directory: str, custom_argument: object, interactive: bool = False, env_vars: dict = None) -> int:
|
|
110
|
+
raise ValueError("Async execution is not supported in Sudo-runner")
|
ScriptCollection/SCLog.py
CHANGED
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
import traceback
|
|
2
|
-
from enum import Enum
|
|
3
|
-
from datetime import datetime
|
|
4
|
-
from .GeneralUtilities import GeneralUtilities
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class LogLevel(Enum):
|
|
8
|
-
Quiet = 0
|
|
9
|
-
Error = 1
|
|
10
|
-
Warning = 2
|
|
11
|
-
Information = 3
|
|
12
|
-
Debug = 4
|
|
13
|
-
Diagnostic = 5
|
|
14
|
-
|
|
15
|
-
def __int__(self):
|
|
16
|
-
return self.value
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class SCLog:
|
|
20
|
-
loglevel: LogLevel = None #minimum loglevel
|
|
21
|
-
log_file: str = None
|
|
22
|
-
add_overhead_to_console: bool = None
|
|
23
|
-
add_overhead_to_logfile: bool = None
|
|
24
|
-
print_as_color: bool = None
|
|
25
|
-
add_milliseconds_to_logfile_entry: bool = None
|
|
26
|
-
subnamespace:str=None
|
|
27
|
-
|
|
28
|
-
def __init__(self, log_file: str = None, loglevel: LogLevel = None, print_as_color: bool = True):
|
|
29
|
-
self.log_file = log_file
|
|
30
|
-
if loglevel is None:
|
|
31
|
-
self.loglevel = LogLevel.Information
|
|
32
|
-
else:
|
|
33
|
-
self.loglevel = loglevel
|
|
34
|
-
self.add_overhead_to_console = False
|
|
35
|
-
self.add_overhead_to_logfile = False
|
|
36
|
-
self.add_milliseconds_to_logfile_entry = False
|
|
37
|
-
self.print_as_color = print_as_color
|
|
38
|
-
|
|
39
|
-
@GeneralUtilities.check_arguments
|
|
40
|
-
def log_exception(self, message: str, ex: Exception,loglevel:LogLevel = LogLevel.Error):
|
|
41
|
-
self.log(f"Exception: {message}; Exception-details: {str(ex)}; Traceback: {traceback.format_exc()}", loglevel)
|
|
42
|
-
|
|
43
|
-
@GeneralUtilities.check_arguments
|
|
44
|
-
def log(self, message: str, loglevel: LogLevel = None):
|
|
45
|
-
for line in GeneralUtilities.string_to_lines(message, True, False):
|
|
46
|
-
self.__log_line(line, loglevel)
|
|
47
|
-
|
|
48
|
-
@GeneralUtilities.check_arguments
|
|
49
|
-
def __log_line(self, message: str, loglevel: LogLevel = None):
|
|
50
|
-
|
|
51
|
-
print_to_console: bool = True
|
|
52
|
-
print_to_logfile: bool = self.log_file is not None
|
|
53
|
-
|
|
54
|
-
if loglevel is None:
|
|
55
|
-
loglevel = LogLevel.Information
|
|
56
|
-
|
|
57
|
-
if int(self.loglevel)<int(loglevel):
|
|
58
|
-
return
|
|
59
|
-
|
|
60
|
-
part1: str = GeneralUtilities.empty_string
|
|
61
|
-
part2: str = GeneralUtilities.empty_string
|
|
62
|
-
part3: str = "] "
|
|
63
|
-
if GeneralUtilities.string_has_content(self.subnamespace):
|
|
64
|
-
part3 = part3 + f"[{self.subnamespace}] "
|
|
65
|
-
part4: str = message
|
|
66
|
-
|
|
67
|
-
if loglevel == LogLevel.Warning and not message.startswith("Warning: "):
|
|
68
|
-
part4 = f"Warning: {message}"
|
|
69
|
-
if loglevel == LogLevel.Debug and not message.startswith("Debug: "):
|
|
70
|
-
part4 = f"Debug: {message}"
|
|
71
|
-
if loglevel == LogLevel.Diagnostic and not message.startswith("Diagnostic: "):
|
|
72
|
-
part4 = f"Diagnostic: {message}"
|
|
73
|
-
|
|
74
|
-
moment: datetime = datetime.now(datetime.now().astimezone().tzinfo)
|
|
75
|
-
|
|
76
|
-
part1 = f"[{GeneralUtilities.datetime_to_string_for_logfile_entry(moment, self.add_milliseconds_to_logfile_entry)}] ["
|
|
77
|
-
if loglevel == LogLevel.Information:
|
|
78
|
-
part2 = f"Information"
|
|
79
|
-
elif loglevel == LogLevel.Error:
|
|
80
|
-
part2 = f"Error"
|
|
81
|
-
elif loglevel == LogLevel.Warning:
|
|
82
|
-
part2 = f"Warning"
|
|
83
|
-
elif loglevel == LogLevel.Debug:
|
|
84
|
-
part2 = f"Debug"
|
|
85
|
-
elif loglevel == LogLevel.Diagnostic:
|
|
86
|
-
part2 = f"Diagnostic"
|
|
87
|
-
else:
|
|
88
|
-
raise ValueError("Unknown loglevel.")
|
|
89
|
-
|
|
90
|
-
if print_to_console:
|
|
91
|
-
print_to_std_out: bool = loglevel in (LogLevel.Debug, LogLevel.Information)
|
|
92
|
-
if self.add_overhead_to_console:
|
|
93
|
-
GeneralUtilities.print_text(part1, print_to_std_out)
|
|
94
|
-
if loglevel == LogLevel.Information:
|
|
95
|
-
GeneralUtilities.print_text_in_green(part2, print_to_std_out, self.print_as_color)
|
|
96
|
-
elif loglevel == LogLevel.Error:
|
|
97
|
-
GeneralUtilities.print_text_in_red(part2, print_to_std_out, self.print_as_color)
|
|
98
|
-
elif loglevel == LogLevel.Warning:
|
|
99
|
-
GeneralUtilities.print_text_in_yellow(part2, print_to_std_out, self.print_as_color)
|
|
100
|
-
elif loglevel == LogLevel.Debug:
|
|
101
|
-
GeneralUtilities.print_text_in_cyan(part2, print_to_std_out, self.print_as_color)
|
|
102
|
-
elif loglevel == LogLevel.Diagnostic:
|
|
103
|
-
GeneralUtilities.print_text_in_cyan(part2, print_to_std_out, self.print_as_color)
|
|
104
|
-
else:
|
|
105
|
-
raise ValueError("Unknown loglevel.")
|
|
106
|
-
GeneralUtilities.print_text(part3+part4+"\n", print_to_std_out)
|
|
107
|
-
else:
|
|
108
|
-
GeneralUtilities.print_text(part4+"\n", print_to_std_out)
|
|
109
|
-
|
|
110
|
-
if print_to_logfile:
|
|
111
|
-
GeneralUtilities.ensure_file_exists(self.log_file)
|
|
112
|
-
if self.add_overhead_to_logfile:
|
|
113
|
-
GeneralUtilities.append_line_to_file(self.log_file, part1+part2+part3+part4)
|
|
114
|
-
else:
|
|
115
|
-
GeneralUtilities.append_line_to_file(self.log_file, part4)
|
|
1
|
+
import traceback
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from .GeneralUtilities import GeneralUtilities
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class LogLevel(Enum):
|
|
8
|
+
Quiet = 0
|
|
9
|
+
Error = 1
|
|
10
|
+
Warning = 2
|
|
11
|
+
Information = 3
|
|
12
|
+
Debug = 4
|
|
13
|
+
Diagnostic = 5
|
|
14
|
+
|
|
15
|
+
def __int__(self):
|
|
16
|
+
return self.value
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SCLog:
|
|
20
|
+
loglevel: LogLevel = None #minimum loglevel
|
|
21
|
+
log_file: str = None
|
|
22
|
+
add_overhead_to_console: bool = None
|
|
23
|
+
add_overhead_to_logfile: bool = None
|
|
24
|
+
print_as_color: bool = None
|
|
25
|
+
add_milliseconds_to_logfile_entry: bool = None
|
|
26
|
+
subnamespace:str=None
|
|
27
|
+
|
|
28
|
+
def __init__(self, log_file: str = None, loglevel: LogLevel = None, print_as_color: bool = True):
|
|
29
|
+
self.log_file = log_file
|
|
30
|
+
if loglevel is None:
|
|
31
|
+
self.loglevel = LogLevel.Information
|
|
32
|
+
else:
|
|
33
|
+
self.loglevel = loglevel
|
|
34
|
+
self.add_overhead_to_console = False
|
|
35
|
+
self.add_overhead_to_logfile = False
|
|
36
|
+
self.add_milliseconds_to_logfile_entry = False
|
|
37
|
+
self.print_as_color = print_as_color
|
|
38
|
+
|
|
39
|
+
@GeneralUtilities.check_arguments
|
|
40
|
+
def log_exception(self, message: str, ex: Exception,loglevel:LogLevel = LogLevel.Error):
|
|
41
|
+
self.log(f"Exception: {message}; Exception-details: {str(ex)}; Traceback: {traceback.format_exc()}", loglevel)
|
|
42
|
+
|
|
43
|
+
@GeneralUtilities.check_arguments
|
|
44
|
+
def log(self, message: str, loglevel: LogLevel = None):
|
|
45
|
+
for line in GeneralUtilities.string_to_lines(message, True, False):
|
|
46
|
+
self.__log_line(line, loglevel)
|
|
47
|
+
|
|
48
|
+
@GeneralUtilities.check_arguments
|
|
49
|
+
def __log_line(self, message: str, loglevel: LogLevel = None):
|
|
50
|
+
|
|
51
|
+
print_to_console: bool = True
|
|
52
|
+
print_to_logfile: bool = self.log_file is not None
|
|
53
|
+
|
|
54
|
+
if loglevel is None:
|
|
55
|
+
loglevel = LogLevel.Information
|
|
56
|
+
|
|
57
|
+
if int(self.loglevel)<int(loglevel):
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
part1: str = GeneralUtilities.empty_string
|
|
61
|
+
part2: str = GeneralUtilities.empty_string
|
|
62
|
+
part3: str = "] "
|
|
63
|
+
if GeneralUtilities.string_has_content(self.subnamespace):
|
|
64
|
+
part3 = part3 + f"[{self.subnamespace}] "
|
|
65
|
+
part4: str = message
|
|
66
|
+
|
|
67
|
+
if loglevel == LogLevel.Warning and not message.startswith("Warning: "):
|
|
68
|
+
part4 = f"Warning: {message}"
|
|
69
|
+
if loglevel == LogLevel.Debug and not message.startswith("Debug: "):
|
|
70
|
+
part4 = f"Debug: {message}"
|
|
71
|
+
if loglevel == LogLevel.Diagnostic and not message.startswith("Diagnostic: "):
|
|
72
|
+
part4 = f"Diagnostic: {message}"
|
|
73
|
+
|
|
74
|
+
moment: datetime = datetime.now(datetime.now().astimezone().tzinfo)
|
|
75
|
+
|
|
76
|
+
part1 = f"[{GeneralUtilities.datetime_to_string_for_logfile_entry(moment, self.add_milliseconds_to_logfile_entry)}] ["
|
|
77
|
+
if loglevel == LogLevel.Information:
|
|
78
|
+
part2 = f"Information"
|
|
79
|
+
elif loglevel == LogLevel.Error:
|
|
80
|
+
part2 = f"Error"
|
|
81
|
+
elif loglevel == LogLevel.Warning:
|
|
82
|
+
part2 = f"Warning"
|
|
83
|
+
elif loglevel == LogLevel.Debug:
|
|
84
|
+
part2 = f"Debug"
|
|
85
|
+
elif loglevel == LogLevel.Diagnostic:
|
|
86
|
+
part2 = f"Diagnostic"
|
|
87
|
+
else:
|
|
88
|
+
raise ValueError("Unknown loglevel.")
|
|
89
|
+
|
|
90
|
+
if print_to_console:
|
|
91
|
+
print_to_std_out: bool = loglevel in (LogLevel.Debug, LogLevel.Information)
|
|
92
|
+
if self.add_overhead_to_console:
|
|
93
|
+
GeneralUtilities.print_text(part1, print_to_std_out)
|
|
94
|
+
if loglevel == LogLevel.Information:
|
|
95
|
+
GeneralUtilities.print_text_in_green(part2, print_to_std_out, self.print_as_color)
|
|
96
|
+
elif loglevel == LogLevel.Error:
|
|
97
|
+
GeneralUtilities.print_text_in_red(part2, print_to_std_out, self.print_as_color)
|
|
98
|
+
elif loglevel == LogLevel.Warning:
|
|
99
|
+
GeneralUtilities.print_text_in_yellow(part2, print_to_std_out, self.print_as_color)
|
|
100
|
+
elif loglevel == LogLevel.Debug:
|
|
101
|
+
GeneralUtilities.print_text_in_cyan(part2, print_to_std_out, self.print_as_color)
|
|
102
|
+
elif loglevel == LogLevel.Diagnostic:
|
|
103
|
+
GeneralUtilities.print_text_in_cyan(part2, print_to_std_out, self.print_as_color)
|
|
104
|
+
else:
|
|
105
|
+
raise ValueError("Unknown loglevel.")
|
|
106
|
+
GeneralUtilities.print_text(part3+part4+"\n", print_to_std_out)
|
|
107
|
+
else:
|
|
108
|
+
GeneralUtilities.print_text(part4+"\n", print_to_std_out)
|
|
109
|
+
|
|
110
|
+
if print_to_logfile:
|
|
111
|
+
GeneralUtilities.ensure_file_exists(self.log_file)
|
|
112
|
+
if self.add_overhead_to_logfile:
|
|
113
|
+
GeneralUtilities.append_line_to_file(self.log_file, part1+part2+part3+part4)
|
|
114
|
+
else:
|
|
115
|
+
GeneralUtilities.append_line_to_file(self.log_file, part4)
|