1212import psutil
1313from prettytable import PrettyTable
1414
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
1616
1717# Initialization
1818log = Log ({"log_level" : DEBUG , "delete_log" : DELETE_LOGS })
1919ACTION , 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 ))
2121log .debug (f"MAX_WORKERS: { MAX_WORKERS } " )
2222
2323
@@ -67,7 +67,8 @@ def __generate_execution_list(self) -> list[str]:
6767 - Warns users about potential long execution times for certain actions
6868 """
6969 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" ])
7172 files_to_remove = {
7273 "sensitive_data_miner.py" ,
7374 "dir_list.py" ,
@@ -101,7 +102,8 @@ def __generate_execution_list(self) -> list[str]:
101102 elif ACTION == "modded" :
102103 # Add all files in MODS to execution list
103104 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" ])
105107
106108 elif ACTION == "depth" :
107109 log .warning (
@@ -128,6 +130,7 @@ def __generate_execution_list(self) -> list[str]:
128130 log .critical ("Nothing is in the execution list.. This is due to faulty code or corrupted Logicytics files!" )
129131 exit (1 )
130132
133+ log .debug (f"Execution list length: { len (execution_list )} " )
131134 log .debug (f"The following will be executed: { execution_list } " )
132135 return execution_list
133136
@@ -208,18 +211,32 @@ def __performance(self):
208211 end_time = datetime .now ()
209212 end_memory = process .memory_full_info ().uss / 1024 / 1024 # MB
210213 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 } " ))
213216 execution_times .append ((self .execution_list [file ], elapsed_time ))
214217 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" )
217227
218228 table = PrettyTable ()
219229 table .field_names = ["Script" , "Execution Time" , "Memory Usage (MB)" ]
220230 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 } " ])
223240
224241 try :
225242 with open (
@@ -228,9 +245,9 @@ def __performance(self):
228245 ) as f :
229246 f .write (table .get_string ())
230247 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 " )
234251 log .info ("Performance check complete! Performance log found in ACCESS/LOGS/PERFORMANCE" )
235252 except Exception as e :
236253 log .error (f"Error writing performance log: { e } " )
@@ -434,10 +451,16 @@ def check_privileges():
434451 log .warning ("UAC is enabled, this may cause issues - Please disable UAC if possible" )
435452
436453
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" )
439461
440- def zip_and_log (directory : str , name : str ):
462+ @staticmethod
463+ def __and_log (directory : str , name : str ):
441464 log .debug (f"Zipping directory '{ directory } ' with name '{ name } ' under action '{ ACTION } '" )
442465 zip_values = FileManagement .Zip .and_hash (
443466 directory ,
@@ -451,10 +474,6 @@ def zip_and_log(directory: str, name: str):
451474 log .info (zip_loc )
452475 log .debug (hash_loc )
453476
454- if ACTION == "modded" :
455- zip_and_log ("..\\ MODS" , "MODS" )
456- zip_and_log ("." , "CODE" )
457-
458477
459478def handle_sub_action ():
460479 """
@@ -472,8 +491,7 @@ def handle_sub_action():
472491 elif SUB_ACTION == "reboot" :
473492 subprocess .call ("shutdown /r /t 3" , shell = False )
474493 # 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
477495
478496
479497@log .function
@@ -501,7 +519,7 @@ def Logicytics():
501519 # Execute scripts
502520 ExecuteScript ().handler ()
503521 # Zip generated files
504- zip_generated_files ()
522+ ZIP . files ()
505523 # Finish with sub actions
506524 handle_sub_action ()
507525 # Finish
0 commit comments