summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_ext.py
blob: 2c35b024a94889777e13c83af5cc57f241569528 (plain)
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
57
58
59
60
61
62
63
64
65
import sys
import os
import warnings
import platform

# if not set - set it to default
TREX_STL_EXT_PATH = os.environ.get('TREX_STL_EXT_PATH')

# take default
if not TREX_STL_EXT_PATH or not os.path.exists(TREX_STL_EXT_PATH):
    CURRENT_PATH        = os.path.dirname(os.path.realpath(__file__))
    TREX_STL_EXT_PATH  = os.path.normpath(os.path.join(CURRENT_PATH, os.pardir, os.pardir, 'external_libs'))
if not os.path.exists(TREX_STL_EXT_PATH):
    # ../../../../external_libs
    TREX_STL_EXT_PATH   = os.path.normpath(os.path.join(CURRENT_PATH, os.pardir, os.pardir, os.pardir, os.pardir, 'external_libs'))
if not os.path.exists(TREX_STL_EXT_PATH):
    raise Exception('Could not determine path of external_libs, try setting TREX_STL_EXT_PATH variable')

# the modules required
# py-dep requires python2/python3 directories
# arch-dep requires ucs2/ucs4 and 32bit/64bit directories
CLIENT_UTILS_MODULES = [ {'name': 'texttable-0.8.4'},
                         {'name': 'pyyaml-3.11', 'py-dep': True},
                         {'name': 'scapy-2.3.1', 'py-dep': True},
                         {'name': 'pyzmq-14.5.0', 'py-dep': True, 'arch-dep': True}
                        ]


def generate_module_path (module, is_python3, is_64bit, is_ucs2):
    platform_path = [module['name']]

    if module.get('py-dep'):
        platform_path.append('python3' if is_python3 else 'python2')

    if module.get('arch-dep'):
        platform_path.append('ucs2' if is_ucs2 else 'ucs4')
        platform_path.append('64bit' if is_64bit else '32bit')

    return os.path.normcase(os.path.join(TREX_STL_EXT_PATH, *platform_path))


def import_module_list(modules_list):

    # platform data
    is_64bit   = platform.architecture()[0] == '64bit'
    is_python3 = (sys.version_info >= (3, 0))
    is_ucs2    = (sys.maxunicode == 65535)

    # regular modules
    for p in modules_list:
        full_path = generate_module_path(p, is_python3, is_64bit, is_ucs2)

        if not os.path.exists(full_path):
            print("Unable to find required module library: '{0}'".format(p['name']))
            print("Please provide the correct path using TREX_STL_EXT_PATH variable")
            print("current path used: '{0}'".format(full_path))
            exit(1)

        sys.path.insert(1, full_path)





import_module_list(CLIENT_UTILS_MODULES)