Skip to content

Commit

Permalink
Support pip install as root (microsoft#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhe-lz authored and QuanluZhang committed Sep 26, 2018
1 parent 6873a3c commit a20c510
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
27 changes: 15 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ easy-install: check-perm
easy-install: install-dependencies
easy-install: build
easy-install: install
easy-install: update-bashrc
easy-install: update-bash-config

easy-install:
#$(_INFO) Complete! #(_END)

Expand All @@ -131,6 +132,7 @@ pip-install: build
pip-install: install-node-modules
pip-install: install-scripts
pip-install: install-examples
pip-install: update-bash-config


# Target for NNI developers
Expand Down Expand Up @@ -253,9 +255,6 @@ install-scripts:
chmod +x $(BIN_PATH)/nnictl

install -Dm644 tools/bash-completion $(BASH_COMP_SCRIPT)
ifndef _ROOT
echo '[[ -f $(BASH_COMP_SCRIPT) ]] && source $(BASH_COMP_SCRIPT)' >> ~/.bash_completion
endif


.PHONY: install-examples
Expand All @@ -264,16 +263,20 @@ install-examples:
[ $(EXAMPLES_PATH) = ${PWD}/examples ] || cp -rT examples $(EXAMPLES_PATH)


.PHONY: update-bashrc
ifeq (, $(shell echo $$PATH | tr ':' '\n' | grep -x '$(BIN_PATH)')) # $(BIN_PATH) not in PATH
ifdef _ROOT
$(error $(BIN_PATH) not in PATH as root, which should never happen)
endif
update-bashrc:
.PHONY: update-bash-config
ifndef _ROOT
update-bash-config:
#$(_INFO) Updating bash configurations $(_END)
ifeq (, $(shell echo $$PATH | tr ':' '\n' | grep -x '$(BIN_PATH)')) # $(BIN_PATH) not in PATH
#$(_WARNING) NOTE: adding $(BIN_PATH) to PATH in bashrc $(_END)
echo 'export PATH="$$PATH:$(BIN_PATH)"' >> ~/.bashrc
else # $(BIN_PATH) already in PATH
update-bashrc: ;
endif
ifeq (, $(shell (source ~/.bash_completion ; command -v _nnictl) 2>/dev/null)) # completion not installed
#$(_WARNING) NOTE: adding $(BASH_COMP_SCRIPT) to ~/.bash_completion $(_END)
echo '[[ -f $(BASH_COMP_SCRIPT) ]] && source $(BASH_COMP_SCRIPT)' >> ~/.bash_completion
endif
else
update-bash-config: ;
endif


Expand Down
28 changes: 3 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,16 @@
import os
from setuptools import setup, find_packages
from setuptools.command.install import install
from subprocess import Popen
import subprocess

def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname), encoding='utf-8').read()

class CustomInstallCommand(install):
'''a customized install class in pip module'''
def makeInstall(self):
'''execute make pip-install command'''
cmds = ['make', 'pip-install']
process = Popen(cmds)
if process.wait() != 0:
print('Error: Make Install Failed')
exit(-1)

def writeEnvironmentVariables(self, variable_name):
'''write an environment variable into ~/.bashrc'''
paths = os.getenv("PATH").split(':')
bin_path = os.path.join(os.getenv('HOME'),'.local/'+variable_name+'/bin')

if bin_path not in paths:
bashrc_path = os.path.join(os.getenv('HOME'), '.bashrc')
process = Popen('echo export PATH=' + bin_path + ':\$PATH >> ' + bashrc_path, shell=True)
if process.wait() != 0:
print('Error: Write Environment Variables Failed')
exit(-1)

def run(self):
install.run(self)
self.makeInstall()
self.writeEnvironmentVariables('node')
self.writeEnvironmentVariables('yarn')
super().run()
subprocess.run(['make', 'pip-install'], check=True)

setup(
name = 'NNI',
Expand Down

0 comments on commit a20c510

Please sign in to comment.