1#!/router/bin/python
2import sys, site
3import platform, os
4import pprint
5
6CURRENT_PATH        = os.path.dirname(os.path.realpath(__file__)) # alternate use  with: os.getcwd()
7TREX_PATH           = os.getenv('TREX_UNDER_TEST')     # path to <trex-core>/scripts directory, env. variable TREX_UNDER_TEST should override it.
8if not TREX_PATH or not os.path.isfile('%s/trex_daemon_server' % TREX_PATH):
9    TREX_PATH       = os.path.abspath(os.path.join(CURRENT_PATH, os.pardir, os.pardir))
10PATH_TO_PYTHON_LIB  = os.path.abspath(os.path.join(TREX_PATH, 'external_libs'))
11PATH_TO_CTRL_PLANE  = os.path.abspath(os.path.join(TREX_PATH, 'automation', 'trex_control_plane'))
12PATH_STF_API        = os.path.abspath(os.path.join(PATH_TO_CTRL_PLANE, 'stf'))
13PATH_STL_API        = os.path.abspath(os.path.join(PATH_TO_CTRL_PLANE, 'stl'))
14
15
16NIGHTLY_MODULES = [ {'name': 'ansi2html'},
17                    {'name': 'enum34-1.0.4'},
18                    {'name': 'rednose-0.4.1'},
19                    {'name': 'progressbar-2.2'},
20                    {'name': 'termstyle'},
21                    {'name': 'urllib3'},
22                    {'name': 'elasticsearch'},
23                    {'name': 'requests'},
24                    {'name': 'pyyaml-3.11', 'py-dep': True},
25                    {'name': 'nose-1.3.4', 'py-dep': True},
26                    ]
27
28
29def generate_module_path (module, is_python3, is_64bit, is_ucs2):
30    platform_path = [module['name']]
31
32    if module.get('py-dep'):
33        platform_path.append('python3' if is_python3 else 'python2')
34
35    if module.get('arch-dep'):
36        platform_path.append('ucs2' if is_ucs2 else 'ucs4')
37        platform_path.append('64bit' if is_64bit else '32bit')
38
39    return os.path.normcase(os.path.join(PATH_TO_PYTHON_LIB, *platform_path))
40
41
42def import_module_list(modules_list):
43
44    # platform data
45    is_64bit   = platform.architecture()[0] == '64bit'
46    is_python3 = (sys.version_info >= (3, 0))
47    is_ucs2    = (sys.maxunicode == 65535)
48
49    # regular modules
50    for p in modules_list:
51        full_path = generate_module_path(p, is_python3, is_64bit, is_ucs2)
52
53        if not os.path.exists(full_path):
54            print("Unable to find required module library: '{0}'".format(p['name']))
55            print("Please provide the correct path using PATH_TO_PYTHON_LIB variable")
56            print("current path used: '{0}'".format(full_path))
57            exit(0)
58        if full_path not in sys.path:
59            sys.path.insert(1, full_path)
60
61
62def import_nightly_modules ():
63    #sys.path.append(PATH_TO_CTRL_PLANE)
64    for path in (TREX_PATH, PATH_STL_API, PATH_STF_API):
65        if path not in sys.path:
66            sys.path.append(path)
67    import_module_list(NIGHTLY_MODULES)
68    #pprint.pprint(sys.path)
69
70
71import_nightly_modules()
72
73
74if __name__ == "__main__":
75    pass
76