From cd073802e062bc03db94639d7b187bdabd7113ec Mon Sep 17 00:00:00 2001 From: zhangyuge Date: Mon, 21 Oct 2019 21:12:11 +0800 Subject: [PATCH 1/3] Refactor package installation --- tools/nni_cmd/command_utils.py | 37 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/tools/nni_cmd/command_utils.py b/tools/nni_cmd/command_utils.py index f8c7a0acfd..8501252870 100644 --- a/tools/nni_cmd/command_utils.py +++ b/tools/nni_cmd/command_utils.py @@ -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] @@ -26,8 +27,9 @@ 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) @@ -35,21 +37,20 @@ def kill_command(pid): 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""" + 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 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 + return ret From a77763040cd696704bdeb4dc3d7b1fe51a3b9e61 Mon Sep 17 00:00:00 2001 From: zhangyuge Date: Tue, 22 Oct 2019 10:53:19 +0800 Subject: [PATCH 2/3] reformat docstring --- tools/nni_cmd/command_utils.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tools/nni_cmd/command_utils.py b/tools/nni_cmd/command_utils.py index 8501252870..15e3a2c618 100644 --- a/tools/nni_cmd/command_utils.py +++ b/tools/nni_cmd/command_utils.py @@ -39,12 +39,26 @@ def kill_command(pid): def install_package_command(package_name): - """install python package from pip""" + """ + 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""" + """ + 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) From 26f33b40ffe968bb2300c8bb827c7cf7e4c4ac2b Mon Sep 17 00:00:00 2001 From: zhangyuge Date: Tue, 22 Oct 2019 15:48:24 +0800 Subject: [PATCH 3/3] fix issue when user is root --- tools/nni_cmd/command_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/nni_cmd/command_utils.py b/tools/nni_cmd/command_utils.py index 15e3a2c618..a3bcb81965 100644 --- a/tools/nni_cmd/command_utils.py +++ b/tools/nni_cmd/command_utils.py @@ -65,6 +65,7 @@ def install_requirements_command(requirements_path): 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: + 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