Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Fix package installation in virtualenv and conda (#1640)
Browse files Browse the repository at this point in the history
* Refactor package installation

* reformat docstring

* fix issue when user is root
  • Loading branch information
Yuge Zhang authored and liuzhe-lz committed Oct 22, 2019
1 parent 9d468d2 commit 5557a2e
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions tools/nni_cmd/command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import os
import signal
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):
'''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 sys.platform == 'win32':
cmds = ['powershell.exe', 'type', file_path]
Expand All @@ -26,30 +27,45 @@ def check_output_command(file_path, head=None, tail=None):
print_error('{0} does not exist!'.format(file_path))
exit(1)


def kill_command(pid):
'''kill command'''
"""kill command"""
if sys.platform == 'win32':
process = psutil.Process(pid=pid)
process.send_signal(signal.CTRL_BREAK_EVENT)
else:
cmds = ['kill', str(pid)]
call(cmds)


def install_package_command(package_name):
'''install python package from pip'''
#TODO refactor python logic
if sys.platform == "win32":
cmds = 'python -m pip install --user {0}'.format(package_name)
else:
cmds = 'python3 -m pip install --user {0}'.format(package_name)
call(cmds, shell=True)
"""
Install python package from pip.
Parameters
----------
package_name: str
The name of package to be installed.
"""
call(_get_pip_install() + [package_name], shell=False)


def install_requirements_command(requirements_path):
'''install requirements.txt'''
cmds = 'cd ' + requirements_path + ' && {0} -m pip install --user -r requirements.txt'
#TODO refactor python logic
if sys.platform == "win32":
cmds = cmds.format('python')
else:
cmds = cmds.format('python3')
call(cmds, shell=True)
"""
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

0 comments on commit 5557a2e

Please sign in to comment.