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

Fix package installation in virtualenv and conda #1640

Merged
merged 3 commits into from
Oct 22, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 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"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you changed docstring format, I recommend you update the docstring following the discussed format :)

if os.path.exists(file_path):
if sys.platform == 'win32':
cmds = ['powershell.exe', 'type', file_path]
Expand All @@ -26,30 +27,44 @@ 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:
ret.append("--user") # not in virtualenv or conda
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if install as root?

return ret