common_utils.py 3.06 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright (c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge,
# to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

21
22
import os
import sys
Deshui Yu's avatar
Deshui Yu committed
23
import json
24
import socket
25
from pathlib import Path
chicm-ms's avatar
chicm-ms committed
26
27
import ruamel.yaml as yaml
import psutil
28
from .constants import ERROR_INFO, NORMAL_INFO, WARNING_INFO, COLOR_RED_FORMAT, COLOR_YELLOW_FORMAT
Deshui Yu's avatar
Deshui Yu committed
29
30
31
32
33

def get_yml_content(file_path):
    '''Load yaml file content'''
    try:
        with open(file_path, 'r') as file:
34
            return yaml.load(file, Loader=yaml.Loader)
35
36
    except yaml.scanner.ScannerError as err:
        print_error('yaml file format error!')
chicm-ms's avatar
chicm-ms committed
37
        print_error(err)
38
39
40
41
        exit(1)
    except Exception as exception:
        print_error(exception)
        exit(1)
Deshui Yu's avatar
Deshui Yu committed
42
43
44
45
46
47
48

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:
49
        print_error('json file format error!')
chicm-ms's avatar
chicm-ms committed
50
        print_error(err)
Deshui Yu's avatar
Deshui Yu committed
51
52
53
54
        return None

def print_error(content):
    '''Print error information to screen'''
55
    print(COLOR_RED_FORMAT % (ERROR_INFO % content))
Deshui Yu's avatar
Deshui Yu committed
56
57
58
59
60

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

61
62
63
64
def print_warning(content):
    '''Print warning information to screen'''
    print(COLOR_YELLOW_FORMAT % (WARNING_INFO % content))

Deshui Yu's avatar
Deshui Yu committed
65
66
67
68
69
70
71
def detect_process(pid):
    '''Detect if a process is alive'''
    try:
        process = psutil.Process(pid)
        return process.is_running()
    except:
        return False
72
73
74

def detect_port(port):
    '''Detect if the port is used'''
chicm-ms's avatar
chicm-ms committed
75
    socket_test = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
76
77
    try:
        socket_test.connect(('127.0.0.1', int(port)))
SparkSnail's avatar
SparkSnail committed
78
        socket_test.close()
79
80
81
        return True
    except:
        return False
82
83

def get_user():
chicm-ms's avatar
chicm-ms committed
84
    if sys.platform == 'win32':
85
86
87
88
89
90
91
92
93
        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:
        return str(Path(sitepackages_path).parents[2])