Skip to content

Commit 88fde89

Browse files
committed
refacto
1 parent ceaa575 commit 88fde89

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

httpdbg/hooks/external.py

+42-48
Original file line numberDiff line numberDiff line change
@@ -17,77 +17,71 @@
1717
@contextmanager
1818
def watcher_external(records: HTTPRecords) -> Generator[HTTPRecords, None, None]:
1919
remove_environment_variable = False
20-
try:
21-
watcher = None
2220

23-
if HTTPDBG_MULTIPROCESS_DIR not in os.environ:
24-
with tempfile.TemporaryDirectory(
25-
prefix="httpdbg"
26-
) as httpdbg_multiprocess_dir:
21+
if HTTPDBG_MULTIPROCESS_DIR not in os.environ:
22+
with tempfile.TemporaryDirectory(prefix="httpdbg") as httpdbg_multiprocess_dir:
2723

28-
logger().info(f"watcher_external {httpdbg_multiprocess_dir}")
29-
remove_environment_variable = True
30-
os.environ[HTTPDBG_MULTIPROCESS_DIR] = httpdbg_multiprocess_dir
24+
logger().info(f"watcher_external {httpdbg_multiprocess_dir}")
25+
remove_environment_variable = True
26+
os.environ[HTTPDBG_MULTIPROCESS_DIR] = httpdbg_multiprocess_dir
3127

32-
# we use a custom sitecustomize.py script to record the request in the subprocesses.
33-
# It doesn't work if the subprocess is created using the "fork" method
34-
# instead of the "spawn" method.
35-
template = os.path.join(
36-
os.path.dirname(os.path.abspath(__file__)),
37-
"template_sitecustomize.py",
38-
)
39-
sitecustomize = os.path.join(
40-
httpdbg_multiprocess_dir, "sitecustomize.py"
41-
)
28+
# we use a custom sitecustomize.py script to record the request in the subprocesses.
29+
# It doesn't work if the subprocess is created using the "fork" method
30+
# instead of the "spawn" method.
31+
template = os.path.join(
32+
os.path.dirname(os.path.abspath(__file__)),
33+
"template_sitecustomize.py",
34+
)
35+
sitecustomize = os.path.join(httpdbg_multiprocess_dir, "sitecustomize.py")
4236

43-
shutil.copy(template, sitecustomize)
37+
shutil.copy(template, sitecustomize)
4438

45-
if "PYTHONPATH" in os.environ:
46-
os.environ["PYTHONPATH"] = (
47-
f"{httpdbg_multiprocess_dir}:{os.environ['PYTHONPATH']}"
48-
)
49-
else:
50-
os.environ["PYTHONPATH"] = httpdbg_multiprocess_dir
39+
if "PYTHONPATH" in os.environ:
40+
os.environ["PYTHONPATH"] = (
41+
f"{httpdbg_multiprocess_dir}:{os.environ['PYTHONPATH']}"
42+
)
43+
else:
44+
os.environ["PYTHONPATH"] = httpdbg_multiprocess_dir
5145

52-
watcher = WatcherSubprocessDirThread(records)
46+
try:
47+
watcher = WatcherSubprocessDirThread(
48+
records, httpdbg_multiprocess_dir, 1.0
49+
)
5350
watcher.start()
5451

5552
yield records
56-
53+
finally:
5754
watcher.shutdown()
58-
watcher.join()
59-
else:
60-
yield records
61-
except Exception as ex:
62-
if watcher:
63-
watcher.shutdown()
64-
raise ex
65-
finally:
66-
if remove_environment_variable and (HTTPDBG_MULTIPROCESS_DIR in os.environ):
67-
del os.environ[HTTPDBG_MULTIPROCESS_DIR]
55+
if remove_environment_variable and (
56+
HTTPDBG_MULTIPROCESS_DIR in os.environ
57+
):
58+
del os.environ[HTTPDBG_MULTIPROCESS_DIR]
59+
else:
60+
yield records
6861

6962

7063
class WatcherSubprocessDirThread(threading.Thread):
71-
def __init__(self, records: HTTPRecords, delay: int = 1) -> None:
72-
self.stop = False
73-
self.records = records
74-
self.delay = delay
64+
def __init__(self, records: HTTPRecords, directory: str, delay: float) -> None:
65+
self.records: HTTPRecords = records
66+
self.directory: str = directory
67+
self.delay: float = delay
68+
self._running: bool = True
7569
threading.Thread.__init__(self)
7670

7771
def load_dump(self):
78-
for dump in glob.glob(
79-
f"{os.environ[HTTPDBG_MULTIPROCESS_DIR]}/*.httpdbgrecords"
80-
):
72+
for dump in glob.glob(f"{self.directory}/*.httpdbgrecords"):
8173
with open(dump, "rb") as dumpfile:
8274
newrecords: HTTPRecords = pickle.load(dumpfile)
8375
self.records.requests.update(newrecords.requests)
8476
self.records.initiators.update(newrecords.initiators)
8577
self.records.groups.update(newrecords.groups)
8678

8779
def run(self):
88-
while not self.stop:
89-
time.sleep(self.delay)
80+
while self._running and (HTTPDBG_MULTIPROCESS_DIR in os.environ):
9081
self.load_dump()
82+
time.sleep(self.delay)
9183

9284
def shutdown(self):
93-
self.stop = True
85+
self._running = False
86+
self.join(timeout=5)
87+
self.load_dump()

0 commit comments

Comments
 (0)