"...composable_kernel.git" did not exist on "f3e61c0ab68790ea5da4612b2cb94bbdcfcefc0e"
Commit 5557a2e4 authored by Yuge Zhang's avatar Yuge Zhang Committed by liuzhe-lz
Browse files

Fix package installation in virtualenv and conda (#1640)

* Refactor package installation

* reformat docstring

* fix issue when user is root
parent 9d468d2c
...@@ -3,10 +3,11 @@ import sys ...@@ -3,10 +3,11 @@ import sys
import os import os
import signal import signal
import psutil import psutil
from .common_utils import print_error, print_normal, print_warning from .common_utils import print_error, print_normal, print_warning
def check_output_command(file_path, head=None, tail=None): def check_output_command(file_path, head=None, tail=None):
'''call check_output command to read content from a file''' """call check_output command to read content from a file"""
if os.path.exists(file_path): if os.path.exists(file_path):
if sys.platform == 'win32': if sys.platform == 'win32':
cmds = ['powershell.exe', 'type', file_path] cmds = ['powershell.exe', 'type', file_path]
...@@ -26,8 +27,9 @@ def check_output_command(file_path, head=None, tail=None): ...@@ -26,8 +27,9 @@ def check_output_command(file_path, head=None, tail=None):
print_error('{0} does not exist!'.format(file_path)) print_error('{0} does not exist!'.format(file_path))
exit(1) exit(1)
def kill_command(pid): def kill_command(pid):
'''kill command''' """kill command"""
if sys.platform == 'win32': if sys.platform == 'win32':
process = psutil.Process(pid=pid) process = psutil.Process(pid=pid)
process.send_signal(signal.CTRL_BREAK_EVENT) process.send_signal(signal.CTRL_BREAK_EVENT)
...@@ -35,21 +37,35 @@ def kill_command(pid): ...@@ -35,21 +37,35 @@ def kill_command(pid):
cmds = ['kill', str(pid)] cmds = ['kill', str(pid)]
call(cmds) call(cmds)
def install_package_command(package_name): def install_package_command(package_name):
'''install python package from pip''' """
#TODO refactor python logic Install python package from pip.
if sys.platform == "win32":
cmds = 'python -m pip install --user {0}'.format(package_name) Parameters
else: ----------
cmds = 'python3 -m pip install --user {0}'.format(package_name) package_name: str
call(cmds, shell=True) The name of package to be installed.
"""
call(_get_pip_install() + [package_name], shell=False)
def install_requirements_command(requirements_path): def install_requirements_command(requirements_path):
'''install requirements.txt''' """
cmds = 'cd ' + requirements_path + ' && {0} -m pip install --user -r requirements.txt' Install packages from `requirements.txt` in `requirements_path`.
#TODO refactor python logic
if sys.platform == "win32": Parameters
cmds = cmds.format('python') ----------
else: requirements_path: str
cmds = cmds.format('python3') Path to the directory that contains `requirements.txt`.
call(cmds, shell=True) """
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment