common_utils.py 3.85 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3

4
import os
SparkSnail's avatar
SparkSnail committed
5
import site
6
import sys
Deshui Yu's avatar
Deshui Yu committed
7
import json
8
import socket
9
from pathlib import Path
chicm-ms's avatar
chicm-ms committed
10
11
import ruamel.yaml as yaml
import psutil
12
from .constants import ERROR_INFO, NORMAL_INFO, WARNING_INFO, COLOR_RED_FORMAT, COLOR_YELLOW_FORMAT
Deshui Yu's avatar
Deshui Yu committed
13
14
15
16
17

def get_yml_content(file_path):
    '''Load yaml file content'''
    try:
        with open(file_path, 'r') as file:
18
            return yaml.load(file, Loader=yaml.Loader)
19
20
    except yaml.scanner.ScannerError as err:
        print_error('yaml file format error!')
chicm-ms's avatar
chicm-ms committed
21
        print_error(err)
22
23
24
25
        exit(1)
    except Exception as exception:
        print_error(exception)
        exit(1)
Deshui Yu's avatar
Deshui Yu committed
26
27
28
29
30
31
32

def get_json_content(file_path):
    '''Load json file content'''
    try:
        with open(file_path, 'r') as file:
            return json.load(file)
    except TypeError as err:
33
        print_error('json file format error!')
chicm-ms's avatar
chicm-ms committed
34
        print_error(err)
Deshui Yu's avatar
Deshui Yu committed
35
36
37
38
        return None

def print_error(content):
    '''Print error information to screen'''
39
    print(COLOR_RED_FORMAT % (ERROR_INFO % content))
Deshui Yu's avatar
Deshui Yu committed
40
41
42
43
44

def print_normal(content):
    '''Print error information to screen'''
    print(NORMAL_INFO % content)

45
46
47
48
def print_warning(content):
    '''Print warning information to screen'''
    print(COLOR_YELLOW_FORMAT % (WARNING_INFO % content))

Deshui Yu's avatar
Deshui Yu committed
49
50
51
52
53
54
55
def detect_process(pid):
    '''Detect if a process is alive'''
    try:
        process = psutil.Process(pid)
        return process.is_running()
    except:
        return False
56
57
58

def detect_port(port):
    '''Detect if the port is used'''
chicm-ms's avatar
chicm-ms committed
59
    socket_test = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
60
61
    try:
        socket_test.connect(('127.0.0.1', int(port)))
SparkSnail's avatar
SparkSnail committed
62
        socket_test.close()
63
64
65
        return True
    except:
        return False
66
67

def get_user():
chicm-ms's avatar
chicm-ms committed
68
    if sys.platform == 'win32':
69
70
71
72
73
74
75
76
        return os.environ['USERNAME']
    else:
        return os.environ['USER']

def get_python_dir(sitepackages_path):
    if sys.platform == "win32":
        return str(Path(sitepackages_path))
    else:
liuzhe-lz's avatar
liuzhe-lz committed
77
        return str(Path(sitepackages_path).parents[2])
SparkSnail's avatar
SparkSnail committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

def get_nni_installation_path():
    ''' Find nni lib from the following locations in order
    Return nni root directory if it exists
    '''
    def try_installation_path_sequentially(*sitepackages):
        '''Try different installation path sequentially util nni is found.
        Return None if nothing is found
        '''
        def _generate_installation_path(sitepackages_path):
            python_dir = get_python_dir(sitepackages_path)
            entry_file = os.path.join(python_dir, 'nni', 'main.js')
            if os.path.isfile(entry_file):
                return python_dir
            return None

        for sitepackage in sitepackages:
            python_dir = _generate_installation_path(sitepackage)
            if python_dir:
                return python_dir
        return None

    if os.getenv('VIRTUAL_ENV'):
        # if 'virtualenv' package is used, `site` has not attr getsitepackages, so we will instead use VIRTUAL_ENV
        # Note that conda venv will not have VIRTUAL_ENV
        python_dir = os.getenv('VIRTUAL_ENV')
    else:
        python_sitepackage = site.getsitepackages()[0]
        # If system-wide python is used, we will give priority to using `local sitepackage`--"usersitepackages()" given
        # that nni exists there
        if python_sitepackage.startswith('/usr') or python_sitepackage.startswith('/Library'):
            python_dir = try_installation_path_sequentially(site.getusersitepackages(), site.getsitepackages()[0])
        else:
            python_dir = try_installation_path_sequentially(site.getsitepackages()[0], site.getusersitepackages())

    if python_dir:
        entry_file = os.path.join(python_dir, 'nni', 'main.js')
        if os.path.isfile(entry_file):
            return os.path.join(python_dir, 'nni')
    print_error('Fail to find nni under python library')
    exit(1)