"docs/en_US/Release.md" did not exist on "45c6508eec59fbc40255d9703b6c606c76b1d842"
common_utils.py 2.21 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
5
import os
import sys
Deshui Yu's avatar
Deshui Yu committed
6
import json
7
import socket
chicm-ms's avatar
chicm-ms committed
8
9
import ruamel.yaml as yaml
import psutil
chicm-ms's avatar
chicm-ms committed
10
11
12
from colorama import Fore

from .constants import ERROR_INFO, NORMAL_INFO, WARNING_INFO
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
        return None

chicm-ms's avatar
chicm-ms committed
37
38

def print_error(*content):
Deshui Yu's avatar
Deshui Yu committed
39
    '''Print error information to screen'''
chicm-ms's avatar
chicm-ms committed
40
41
42
43
44
    print(Fore.RED + ERROR_INFO + ' '.join([str(c) for c in content]) + Fore.RESET)

def print_green(*content):
    '''Print information to screen in green'''
    print(Fore.GREEN + ' '.join([str(c) for c in content]) + Fore.RESET)
Deshui Yu's avatar
Deshui Yu committed
45

chicm-ms's avatar
chicm-ms committed
46
def print_normal(*content):
Deshui Yu's avatar
Deshui Yu committed
47
    '''Print error information to screen'''
chicm-ms's avatar
chicm-ms committed
48
    print(NORMAL_INFO, *content)
Deshui Yu's avatar
Deshui Yu committed
49

chicm-ms's avatar
chicm-ms committed
50
def print_warning(*content):
51
    '''Print warning information to screen'''
chicm-ms's avatar
chicm-ms committed
52
    print(Fore.YELLOW + WARNING_INFO + ' '.join([str(c) for c in content]) + Fore.RESET)
53

Deshui Yu's avatar
Deshui Yu committed
54
55
56
57
58
59
60
def detect_process(pid):
    '''Detect if a process is alive'''
    try:
        process = psutil.Process(pid)
        return process.is_running()
    except:
        return False
61
62
63

def detect_port(port):
    '''Detect if the port is used'''
chicm-ms's avatar
chicm-ms committed
64
    socket_test = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65
66
    try:
        socket_test.connect(('127.0.0.1', int(port)))
SparkSnail's avatar
SparkSnail committed
67
        socket_test.close()
68
69
70
        return True
    except:
        return False
71
72

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

78
79
80
81
82
83
84
85
def check_tensorboard_version():
    try:
        import tensorboard
        return tensorboard.__version__
    except:
        print_error('import tensorboard error!')
        exit(1)