summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/server/outer_packages.py
blob: ab25ea6822d51c401fe14f42bd3105e25293d770 (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
66
#!/router/bin/python

import sys,site
import platform,os
import tarfile
import errno
import pwd

CURRENT_PATH        = os.path.dirname(os.path.realpath(__file__))                
ROOT_PATH           = os.path.abspath(os.path.join(CURRENT_PATH, os.pardir))      # path to trex_control_plane directory
PATH_TO_PYTHON_LIB  = os.path.abspath(os.path.join(ROOT_PATH, 'python_lib')) 

SERVER_MODULES = ['enum34-1.0.4',
            # 'jsonrpclib-0.1.3',
            'jsonrpclib-pelix-0.2.5',
            'zmq',
            'python-daemon-2.0.5',
            'lockfile-0.10.2',
            'termstyle'
            ]

def extract_zmq_package ():
    """make sure zmq package is available"""

    os.chdir(PATH_TO_PYTHON_LIB)
    if not os.path.exists('zmq'):
        if os.path.exists('zmq_fedora.tar.gz'): # make sure tar file is available for extraction
            try:
                tar = tarfile.open("zmq_fedora.tar.gz")
                # finally, extract the tarfile locally
                tar.extractall()
            except OSError as err:
                if err.errno == errno.EACCES:
                    # fall back. try extracting using currently logged in user
                    stat_info = os.stat(PATH_TO_PYTHON_LIB)
                    uid = stat_info.st_uid
                    logged_user = pwd.getpwuid(uid).pw_name
                    if logged_user != 'root':
                        try:
                            os.system("sudo -u {user} tar -zxvf zmq_fedora.tar.gz".format(user = logged_user))
                        except:
                            raise OSError(13, 'Permission denied: Please make sure that logged user have sudo access and writing privileges to `python_lib` directory.')
                    else:
                        raise OSError(13, 'Permission denied: Please make sure that logged user have sudo access and writing privileges to `python_lib` directory.')
            finally:
                tar.close()
        else:
            raise IOError("File 'zmq_fedora.tar.gz' couldn't be located at python_lib directory.")
    os.chdir(CURRENT_PATH)

def import_server_modules ():
    # must be in a higher priority 
    sys.path.insert(0, PATH_TO_PYTHON_LIB)
    sys.path.append(ROOT_PATH)
    extract_zmq_package()
    import_module_list(SERVER_MODULES)

def import_module_list (modules_list):
    assert(isinstance(modules_list, list))
    for p in modules_list:
        full_path   = os.path.join(PATH_TO_PYTHON_LIB, p)
        fix_path    = os.path.normcase(full_path) 
        site.addsitedir(full_path)


import_server_modules()