zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import shutil
import time
 
import subprocess
 
import logging
 
from unittest import mock
from contextlib import contextmanager
 
import random
 
from webodm import settings
 
logger = logging.getLogger('app.logger')
 
@contextmanager
def start_processing_node(args = []):
    current_dir = os.path.dirname(os.path.realpath(__file__))
    node_odm = subprocess.Popen(['node', 'index.js', '--port', '11223', '--test'] + args, shell=False,
                                cwd=os.path.join(current_dir, "..", "..", "nodeodm", "external", "NodeODM"))
    time.sleep(3)  # Wait for the server to launch
    yield node_odm
    node_odm.terminate()
    time.sleep(1)  # Wait for the server to stop
 
@contextmanager
def start_simple_auth_server(args = []):
    current_dir = os.path.dirname(os.path.realpath(__file__))
    s = subprocess.Popen(['python', 'simple_auth_server.py'] + args, shell=False,
                                cwd=os.path.join(current_dir, "scripts"))
    time.sleep(2)  # Wait for the server to launch
    yield s
    s.terminate()
    time.sleep(1)  # Wait for the server to stop
 
# We need to clear previous media_root content
# This points to the test directory, but just in case
# we double check that the directory is indeed a test directory
def clear_test_media_root():
    if "_test" in settings.MEDIA_ROOT:
        if os.path.exists(settings.MEDIA_ROOT):
            logger.info("Cleaning up {}".format(settings.MEDIA_ROOT))
            shutil.rmtree(settings.MEDIA_ROOT)
    else:
        logger.warning("We did not remove MEDIA_ROOT because we couldn't find a _test suffix in its path.")
 
 
@contextmanager
def catch_signal(signal):
    """Catch django signal and return the mocked call."""
    handler = mock.Mock()
    signal.connect(handler, dispatch_uid=str(random.random()))
    yield handler
    signal.disconnect(handler)