atomicshop 2.19.13__py3-none-any.whl → 2.19.14__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.

Potentially problematic release.


This version of atomicshop might be problematic. Click here for more details.

atomicshop/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  """Atomic Basic functions and classes to make developer life easier"""
2
2
 
3
3
  __author__ = "Den Kras"
4
- __version__ = '2.19.13'
4
+ __version__ = '2.19.14'
@@ -2,9 +2,11 @@ import threading
2
2
  from pathlib import Path
3
3
  import time
4
4
  import multiprocessing.managers
5
+ import queue
6
+
5
7
 
6
8
  from ..wrappers.pywin32w.win_event_log.subscribes import process_create, process_terminate
7
- from .. import get_process_list, get_process_name_cmd_dll
9
+ from .. import get_process_list
8
10
  from ..print_api import print_api
9
11
 
10
12
 
@@ -15,6 +17,10 @@ WAIT_FOR_PROCESS_POLLER_PID_SECONDS: int = 3
15
17
  WAIT_FOR_PROCESS_POLLER_PID_COUNTS: int = WAIT_FOR_PROCESS_POLLER_PID_SECONDS * 10
16
18
 
17
19
 
20
+ class PidProcessConverterPIDNotFoundError(Exception):
21
+ pass
22
+
23
+
18
24
  class SimpleProcessPool:
19
25
  """
20
26
  THE POOL OF PROCESSES IS NOT REAL TIME!!!
@@ -106,14 +112,16 @@ class SimpleProcessPool:
106
112
 
107
113
  self._processes: dict = dict()
108
114
  self._running: bool = False
115
+ self.shared_dict_update_queue: queue.Queue = queue.Queue()
116
+ self.empty_cmdline_queue: queue.Queue = queue.Queue()
109
117
 
110
118
  def start(self):
111
119
  self._running = True
112
120
 
113
121
  self._processes = get_process_list.GetProcessList(
114
- get_method='pywin32', connect_on_init=True).get_processes(as_dict=True)
122
+ get_method='psutil', connect_on_init=True).get_processes(as_dict=True)
115
123
 
116
- thread_get_queue = threading.Thread(target=self._start_main_thread, args=(self.process_pool_shared_dict_proxy,))
124
+ thread_get_queue = threading.Thread(target=self._start_main_thread, args=(self.shared_dict_update_queue,))
117
125
  thread_get_queue.daemon = True
118
126
  thread_get_queue.start()
119
127
 
@@ -121,13 +129,21 @@ class SimpleProcessPool:
121
129
  thread_process_termination.daemon = True
122
130
  thread_process_termination.start()
123
131
 
132
+ thread_get_psutil_commandline = threading.Thread(target=self._thread_get_psutil_commandline)
133
+ thread_get_psutil_commandline.daemon = True
134
+ thread_get_psutil_commandline.start()
135
+
136
+ thread_update_shared_dict = threading.Thread(target=self._update_shared_dict, args=(self.shared_dict_update_queue,))
137
+ thread_update_shared_dict.daemon = True
138
+ thread_update_shared_dict.start()
139
+
124
140
  def stop(self):
125
141
  self._running = False
126
142
 
127
143
  def get_processes(self):
128
144
  return self._processes
129
145
 
130
- def _start_main_thread(self, process_pool_shared_dict_proxy):
146
+ def _start_main_thread(self, shared_dict_update_queue):
131
147
  get_instance = process_create.ProcessCreateSubscriber()
132
148
  get_instance.start()
133
149
 
@@ -137,17 +153,35 @@ class SimpleProcessPool:
137
153
  process_name = Path(event['NewProcessName']).name
138
154
  command_line = event['CommandLine']
139
155
 
156
+ # The event log tracing method doesn't always give the command line, unlike the psutil method.
157
+ # So, we'll get the command line from the current psutil snapshot separately.
158
+ if command_line == '':
159
+ self.empty_cmdline_queue.put(process_id)
160
+
140
161
  self._processes[process_id] = {
141
162
  'name': process_name,
142
163
  'cmdline': command_line
143
164
  }
144
165
 
145
166
  # Update the multiprocessing shared dict proxy.
146
- if process_pool_shared_dict_proxy is not None:
147
- process_pool_shared_dict_proxy.clear()
148
- process_pool_shared_dict_proxy.update(self._processes)
167
+ shared_dict_update_queue.put(dict(self._processes))
149
168
 
150
- # print_api(f'Process [{process_id}] added to the pool.', color='blue')
169
+ def _thread_get_psutil_commandline(self):
170
+ """
171
+ This function will get an entry from the queue where command line is missing and get the command line
172
+ from the psutil snapshot.
173
+ """
174
+
175
+ while self._running:
176
+ empty_cmd_pid = self.empty_cmdline_queue.get()
177
+ current_psutil_process_snapshot: dict = get_process_list.GetProcessList(
178
+ get_method='psutil', connect_on_init=True).get_processes(as_dict=True)
179
+ command_line = current_psutil_process_snapshot[empty_cmd_pid]['cmdline']
180
+
181
+ self._processes[empty_cmd_pid]['cmdline'] = command_line
182
+
183
+ # Update the multiprocessing shared dict proxy.
184
+ self.shared_dict_update_queue.put(dict(self._processes))
151
185
 
152
186
  def _thread_process_termination(self):
153
187
  process_terminate_instance = process_terminate.ProcessTerminateSubscriber()
@@ -188,6 +222,15 @@ class SimpleProcessPool:
188
222
  _ = self._processes.pop(process_id, None)
189
223
  # print_api(f'Process [{process_id}] removed from the pool.', color='yellow')
190
224
 
225
+ self.shared_dict_update_queue.put(dict(self._processes))
226
+
227
+ def _update_shared_dict(self, shared_dict_update_queue):
228
+ while self._running:
229
+ current_process_pool = shared_dict_update_queue.get()
230
+ if self.process_pool_shared_dict_proxy is not None:
231
+ self.process_pool_shared_dict_proxy.clear()
232
+ self.process_pool_shared_dict_proxy.update(current_process_pool)
233
+
191
234
 
192
235
  class PidProcessConverter:
193
236
  """
@@ -204,7 +247,7 @@ class PidProcessConverter:
204
247
 
205
248
  self.process_pool_shared_dict_proxy: multiprocessing.managers.DictProxy = process_pool_shared_dict_proxy
206
249
 
207
- self.get_process_with_dll_instance = get_process_name_cmd_dll.ProcessNameCmdline(load_dll=True)
250
+ self.get_process_with_psutil = get_process_list.GetProcessList(get_method='psutil', connect_on_init=True)
208
251
 
209
252
  def get_process_by_pid(self, pid: int):
210
253
  """
@@ -219,18 +262,20 @@ class PidProcessConverter:
219
262
  counter = 0
220
263
  process_dict: dict = dict()
221
264
  while counter < WAIT_FOR_PROCESS_POLLER_PID_COUNTS:
222
- if pid not in self.process_pool_shared_dict_proxy:
265
+ # We need it so that the pool will not change in the middle of the process.
266
+ current_pid_pool = convert_proxy_dict_to_dict(self.process_pool_shared_dict_proxy)
267
+ if pid not in current_pid_pool:
223
268
  # print(dict(self.process_pool_shared_dict_proxy))
224
269
  time.sleep(0.1)
225
270
  counter += 1
226
271
  else:
227
- process_dict = self.process_pool_shared_dict_proxy[pid]
272
+ process_dict = current_pid_pool[pid]
228
273
  break
229
274
 
230
- if counter == WAIT_FOR_PROCESS_POLLER_PID_COUNTS and not process_dict:
231
- print_api(f"Error: The PID [{pid}] is not in the pool, trying DLL snapshot.", color='yellow')
275
+ if not process_dict:
276
+ print_api(f"Error: The PID [{pid}] is not in the pool, trying psutil snapshot.", color='yellow')
232
277
  # Last resort, try to get the process name by current process snapshot.
233
- processes = self.get_process_with_dll_instance.get_process_details(as_dict=True)
278
+ processes = self.get_process_with_psutil.get_processes(as_dict=True)
234
279
  if pid not in processes:
235
280
  print_api(f"Error: Couldn't get the process name for PID: {pid}.", color='red')
236
281
  process_dict = {
@@ -242,3 +287,25 @@ class PidProcessConverter:
242
287
 
243
288
  return process_dict
244
289
 
290
+
291
+ def convert_proxy_dict_to_dict(proxy_dict: multiprocessing.managers.DictProxy) -> dict:
292
+ """
293
+ Convert the multiprocessing shared dict proxy to a normal dict.
294
+
295
+ :param proxy_dict: multiprocessing.managers.DictProxy, the shared dict proxy.
296
+ :return: dict, the normal dict.
297
+ """
298
+
299
+ # Create a snapshot of the keys
300
+ keys = list(proxy_dict.keys())
301
+ current_pid_pool = {}
302
+
303
+ for key in keys:
304
+ try:
305
+ # Attempt to retrieve the value for each key
306
+ current_pid_pool[key] = proxy_dict[key]
307
+ except KeyError:
308
+ # The key was removed concurrently; skip it
309
+ continue
310
+
311
+ return current_pid_pool
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: atomicshop
3
- Version: 2.19.13
3
+ Version: 2.19.14
4
4
  Summary: Atomic functions and classes to make developer life easier
5
5
  Author: Denis Kras
6
6
  License: MIT License
@@ -1,4 +1,4 @@
1
- atomicshop/__init__.py,sha256=eRHmU5tHbd8diqaPTRFDlAasPNcCSvV7P_hocFodM0M,124
1
+ atomicshop/__init__.py,sha256=jnzkSiVKr7rNaRPe51te8qpwDBNj2V37gzYMQVqPkoQ,124
2
2
  atomicshop/_basics_temp.py,sha256=6cu2dd6r2dLrd1BRNcVDKTHlsHs_26Gpw8QS6v32lQ0,3699
3
3
  atomicshop/_create_pdf_demo.py,sha256=Yi-PGZuMg0RKvQmLqVeLIZYadqEZwUm-4A9JxBl_vYA,3713
4
4
  atomicshop/_patch_import.py,sha256=ENp55sKVJ0e6-4lBvZnpz9PQCt3Otbur7F6aXDlyje4,6334
@@ -170,7 +170,7 @@ atomicshop/permissions/ubuntu_permissions.py,sha256=n8z1vcIXDts4zLVue33dtJiTopjg
170
170
  atomicshop/permissions/win_permissions.py,sha256=eDQm1jfK9x_hkbLqIJjFTwfqinAWQ0iSr0kW3XrF1BE,1272
171
171
  atomicshop/process_poller/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
172
172
  atomicshop/process_poller/process_pool.py,sha256=4Qs427qd7OcBxu5PMFU5PTmyuxRy0vgj2GLsRt0IoEw,9565
173
- atomicshop/process_poller/simple_process_pool.py,sha256=E0w66imLQQ0phHuTQfTaApOTurX0duE9vw1trjUY7rI,9894
173
+ atomicshop/process_poller/simple_process_pool.py,sha256=ap9nCywHZ-eta7EHe0mTYS45-zYvMjgvfESXAuG0Wi4,12578
174
174
  atomicshop/process_poller/tracer_base.py,sha256=IOiHcnmF-MccOSCErixN5mve9RifZ9cPnGVHCIRchrs,1091
175
175
  atomicshop/process_poller/pollers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
176
176
  atomicshop/process_poller/pollers/psutil_pywin32wmi_dll.py,sha256=XRRfOIy62iOYU8IKRcyECWiL0rqQ35DeYbPsv_SHDVM,4510
@@ -329,8 +329,8 @@ atomicshop/wrappers/socketw/statistics_csv.py,sha256=fgMzDXI0cybwUEqAxprRmY3lqbh
329
329
  atomicshop/wrappers/winregw/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
330
330
  atomicshop/wrappers/winregw/winreg_installed_software.py,sha256=Qzmyktvob1qp6Tjk2DjLfAqr_yXV0sgWzdMW_9kwNjY,2345
331
331
  atomicshop/wrappers/winregw/winreg_network.py,sha256=AENV88H1qDidrcpyM9OwEZxX5svfi-Jb4N6FkS1xtqA,8851
332
- atomicshop-2.19.13.dist-info/LICENSE.txt,sha256=lLU7EYycfYcK2NR_1gfnhnRC8b8ccOTElACYplgZN88,1094
333
- atomicshop-2.19.13.dist-info/METADATA,sha256=w6lD6Ue1Ffz4syR_hzVwv9PF65O42HFgdkvH3adb7PU,10631
334
- atomicshop-2.19.13.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
335
- atomicshop-2.19.13.dist-info/top_level.txt,sha256=EgKJB-7xcrAPeqTRF2laD_Np2gNGYkJkd4OyXqpJphA,11
336
- atomicshop-2.19.13.dist-info/RECORD,,
332
+ atomicshop-2.19.14.dist-info/LICENSE.txt,sha256=lLU7EYycfYcK2NR_1gfnhnRC8b8ccOTElACYplgZN88,1094
333
+ atomicshop-2.19.14.dist-info/METADATA,sha256=1ZQp-Wt5osHkFGnEavFkPEC41-jmfqJsSsktj0T7aBk,10631
334
+ atomicshop-2.19.14.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
335
+ atomicshop-2.19.14.dist-info/top_level.txt,sha256=EgKJB-7xcrAPeqTRF2laD_Np2gNGYkJkd4OyXqpJphA,11
336
+ atomicshop-2.19.14.dist-info/RECORD,,