"docs/_removed/TrainingService/HybridMode.rst" did not exist on "c702241ec4a9dffa73001c69cbf41549619f8d8d"
command_utils.py 2.25 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
3
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

4
5
6
7
8
from subprocess import call, check_output
import sys
import os
import signal
import psutil
chicm-ms's avatar
chicm-ms committed
9
from .common_utils import print_error
10

11
12

def check_output_command(file_path, head=None, tail=None):
13
    """call check_output command to read content from a file"""
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    if os.path.exists(file_path):
        if sys.platform == 'win32':
            cmds = ['powershell.exe', 'type', file_path]
            if head:
                cmds += ['|', 'select', '-first', str(head)]
            elif tail:
                cmds += ['|', 'select', '-last', str(tail)]
            return check_output(cmds, shell=True).decode('utf-8')
        else:
            cmds = ['cat', file_path]
            if head:
                cmds = ['head', '-' + str(head), file_path]
            elif tail:
                cmds = ['tail', '-' + str(tail), file_path]
            return check_output(cmds, shell=False).decode('utf-8')
    else:
        print_error('{0} does not exist!'.format(file_path))
        exit(1)

33

34
def kill_command(pid):
35
    """kill command"""
36
37
38
39
40
41
42
    if sys.platform == 'win32':
        process = psutil.Process(pid=pid)
        process.send_signal(signal.CTRL_BREAK_EVENT)
    else:
        cmds = ['kill', str(pid)]
        call(cmds)

43

44
def install_package_command(package_name):
45
46
47
48
49
50
51
52
53
54
    """
    Install python package from pip.

    Parameters
    ----------
    package_name: str
        The name of package to be installed.
    """
    call(_get_pip_install() + [package_name], shell=False)

55
56

def install_requirements_command(requirements_path):
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    """
    Install packages from `requirements.txt` in `requirements_path`.

    Parameters
    ----------
    requirements_path: str
        Path to the directory that contains `requirements.txt`.
    """
    call(_get_pip_install() + ["-r", os.path.join(requirements_path, "requirements.txt")], shell=False)


def _get_pip_install():
    python = "python" if sys.platform == "win32" else "python3"
    ret = [python, "-m", "pip", "install"]
    if "CONDA_DEFAULT_ENV" not in os.environ and "VIRTUAL_ENV" not in os.environ and \
            (sys.platform != "win32" and os.getuid() != 0):  # on unix and not running in root
        ret.append("--user")  # not in virtualenv or conda
    return ret