12
12
import psutil
13
13
from prettytable import PrettyTable
14
14
15
- from logicytics import Log , Execute , Check , Get , FileManagement , Flag , DEBUG , DELETE_LOGS , CONFIG
15
+ from logicytics import Log , Execute , Check , Get , FileManagement , Flag , DEBUG , DELETE_LOGS , config
16
16
17
17
# Initialization
18
18
log = Log ({"log_level" : DEBUG , "delete_log" : DELETE_LOGS })
19
19
ACTION , SUB_ACTION = None , None
20
- MAX_WORKERS = CONFIG .getint ("Settings" , "max_workers" , fallback = min (32 , (os .cpu_count () or 1 ) + 4 ))
20
+ MAX_WORKERS = config .getint ("Settings" , "max_workers" , fallback = min (32 , (os .cpu_count () or 1 ) + 4 ))
21
21
log .debug (f"MAX_WORKERS: { MAX_WORKERS } " )
22
22
23
23
@@ -67,7 +67,8 @@ def __generate_execution_list(self) -> list[str]:
67
67
- Warns users about potential long execution times for certain actions
68
68
"""
69
69
execution_list = Get .list_of_files ("." , only_extensions = (".py" , ".exe" , ".ps1" , ".bat" ),
70
- exclude_files = ["Logicytics.py" ])
70
+ exclude_files = ["Logicytics.py" ],
71
+ exclude_dirs = ["logicytics" , "SysInternal_Suite" ])
71
72
files_to_remove = {
72
73
"sensitive_data_miner.py" ,
73
74
"dir_list.py" ,
@@ -101,7 +102,8 @@ def __generate_execution_list(self) -> list[str]:
101
102
elif ACTION == "modded" :
102
103
# Add all files in MODS to execution list
103
104
execution_list = Get .list_of_files ("../MODS" , only_extensions = (".py" , ".exe" , ".ps1" , ".bat" ),
104
- append_file_list = execution_list , exclude_files = ["Logicytics.py" ])
105
+ append_file_list = execution_list , exclude_files = ["Logicytics.py" ],
106
+ exclude_dirs = ["logicytics" , "SysInternal_Suite" ])
105
107
106
108
elif ACTION == "depth" :
107
109
log .warning (
@@ -128,6 +130,7 @@ def __generate_execution_list(self) -> list[str]:
128
130
log .critical ("Nothing is in the execution list.. This is due to faulty code or corrupted Logicytics files!" )
129
131
exit (1 )
130
132
133
+ log .debug (f"Execution list length: { len (execution_list )} " )
131
134
log .debug (f"The following will be executed: { execution_list } " )
132
135
return execution_list
133
136
@@ -208,18 +211,32 @@ def __performance(self):
208
211
end_time = datetime .now ()
209
212
end_memory = process .memory_full_info ().uss / 1024 / 1024 # MB
210
213
elapsed_time = end_time - start_time
211
- memory_delta = end_memory - start_memory
212
- memory_usage .append ((self .execution_list [file ], str ( memory_delta ) ))
214
+ memory_delta = max ( 0 , end_memory - start_memory ) # Clamps negative delta to 0
215
+ memory_usage .append ((self .execution_list [file ], f" { memory_delta } " ))
213
216
execution_times .append ((self .execution_list [file ], elapsed_time ))
214
217
log .info (f"{ self .execution_list [file ]} executed in { elapsed_time } " )
215
- log .info (f"{ self .execution_list [file ]} used { memory_delta :.2f} MB of memory" )
216
- log .debug (f"Started with { start_memory } MB of memory and ended with { end_memory } MB of memory" )
218
+ try :
219
+ if (end_memory - start_memory ) < 0 :
220
+ log .info (
221
+ f"{ self .execution_list [file ]} used { memory_delta :.3f} MB of memory - \033 [33mPossible Affected by outside processes\033 [0m" )
222
+ else :
223
+ log .info (f"{ self .execution_list [file ]} used { memory_delta :.3f} MB of memory" )
224
+ except Exception as e :
225
+ log .warning ("Failed to log memory usage delta, reason: " + str (e ))
226
+ log .debug (f"Started with { start_memory :.3f} MB of memory and ended with { end_memory :.3f} MB of memory" )
217
227
218
228
table = PrettyTable ()
219
229
table .field_names = ["Script" , "Execution Time" , "Memory Usage (MB)" ]
220
230
for script , elapsed_time in execution_times :
221
- memory = next (m [1 ] for m in memory_usage if m [0 ] == script )
222
- table .add_row ([script , elapsed_time , f"{ memory :.2f} " ])
231
+ try :
232
+ memory = f"{ float (next (m [1 ] for m in memory_usage if m [0 ] == script )):.3f} "
233
+ except StopIteration :
234
+ log .warning (f"No memory data found for { script } " )
235
+ memory = "N/A"
236
+ except Exception as e :
237
+ log .warning (f"Failed to log memory usage for { script } , reason: " + str (e ))
238
+ memory = "N/A"
239
+ table .add_row ([script , elapsed_time , f"{ memory } " ])
223
240
224
241
try :
225
242
with open (
@@ -228,9 +245,9 @@ def __performance(self):
228
245
) as f :
229
246
f .write (table .get_string ())
230
247
f .write (
231
- "\n Some values may be negative, Reason may be due to external resources playing with memory usage, "
232
- "close background tasks to get more accurate readings" )
233
- f .write ("Note: This is not a low-level memory logger, data here isn't 100% accurate!" )
248
+ "\n Some values may be negative, Reason may be due to external resources playing with memory usage,\n "
249
+ "Close background tasks to get more accurate readings\n \n " )
250
+ f .write ("Note: This is not a low-level memory logger, data here isn't 100% accurate!\n " )
234
251
log .info ("Performance check complete! Performance log found in ACCESS/LOGS/PERFORMANCE" )
235
252
except Exception as e :
236
253
log .error (f"Error writing performance log: { e } " )
@@ -434,10 +451,16 @@ def check_privileges():
434
451
log .warning ("UAC is enabled, this may cause issues - Please disable UAC if possible" )
435
452
436
453
437
- def zip_generated_files ():
438
- """Zips generated files based on the action."""
454
+ class ZIP :
455
+ @classmethod
456
+ def files (cls ):
457
+ """Zips generated files based on the action."""
458
+ if ACTION == "modded" :
459
+ cls .__and_log ("..\\ MODS" , "MODS" )
460
+ cls .__and_log ("." , "CODE" )
439
461
440
- def zip_and_log (directory : str , name : str ):
462
+ @staticmethod
463
+ def __and_log (directory : str , name : str ):
441
464
log .debug (f"Zipping directory '{ directory } ' with name '{ name } ' under action '{ ACTION } '" )
442
465
zip_values = FileManagement .Zip .and_hash (
443
466
directory ,
@@ -451,10 +474,6 @@ def zip_and_log(directory: str, name: str):
451
474
log .info (zip_loc )
452
475
log .debug (hash_loc )
453
476
454
- if ACTION == "modded" :
455
- zip_and_log ("..\\ MODS" , "MODS" )
456
- zip_and_log ("." , "CODE" )
457
-
458
477
459
478
def handle_sub_action ():
460
479
"""
@@ -472,8 +491,7 @@ def handle_sub_action():
472
491
elif SUB_ACTION == "reboot" :
473
492
subprocess .call ("shutdown /r /t 3" , shell = False )
474
493
# elif sub_action == "webhook":
475
- # Implement this in future
476
- # log.warning("This feature is not implemented yet! Sorry")
494
+ # TODO: Implement this in future v3.5
477
495
478
496
479
497
@log .function
@@ -501,7 +519,7 @@ def Logicytics():
501
519
# Execute scripts
502
520
ExecuteScript ().handler ()
503
521
# Zip generated files
504
- zip_generated_files ()
522
+ ZIP . files ()
505
523
# Finish with sub actions
506
524
handle_sub_action ()
507
525
# Finish
0 commit comments