Skip to content

Commit

Permalink
install packages in inst/python-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
brucehoff committed Jun 22, 2017
1 parent 424f9c0 commit fee02e8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 60 deletions.
23 changes: 0 additions & 23 deletions R/shared.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,6 @@
# Author: bhoff
###############################################################################

.getPythonFolderPath<-function(rootDir) {
file.path(rootDir, "python")
}

.addEggsToPath<-function(dir){
# modules with .egg extensions (such as future and synapseClient) need to be explicitly added to the sys.path
pyImport("sys")
pyImport("glob")
pyExec(sprintf("sys.path+=glob.glob('%s/*.egg')", dir))
}

.addPythonAndLibFoldersToSysPath<-function(srcDir) {
pyImport("sys")
pyExec(sprintf("sys.path.append('%s')", .getPythonFolderPath(srcDir)))
pyExec(sprintf("sys.path.append('%s')", file.path(srcDir, "lib")))
pyExec(sprintf("sys.path.append('%s')", file.path(srcDir, "lib/python3.5")))
sitePackageDir <- file.path(srcDir, "lib/python3.5/site-packages")
pyExec(sprintf("sys.path.append('%s')", sitePackageDir))
#add all .eggs to paths
.addEggsToPath(sitePackageDir)
}

.addSynPrefix<-function(name) {
paste("syn", toupper(substring(name,1,1)), substring(name,2,nchar(name)), sep="")
}
Expand All @@ -38,7 +16,6 @@
# rootDir is the folder containing the 'python' folder
#
.getSynapseFunctionInfo<-function(rootDir) {
#pyImport("sys")
pyExec(sprintf("sys.path.append(\"%s\")", file.path(rootDir, "python")))
pyImport("functionInfo")

Expand Down
32 changes: 20 additions & 12 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
# package initialization
#
#
# Author: bhoff
###############################################################################

# TODO the package should be able to return the version of the underlying Python package
# i.e. the value of synapseclient.__version__
.onLoad <- function(libname, pkgname) {
.addPythonAndLibFoldersToSysPath(system.file(package="synapser"))
.addPythonAndFoldersToSysPath(system.file(package="synapser"))

# TODO This line is in '.onLoad' in PythonEmbedInR but fails to persist on Windows
# long term, need to determine why and fix it
Sys.setenv(PYTHONPATH=system.file("lib", package="PythonEmbedInR"))
# TODO to be on the safe side, we repeat this too. Long term we need to look at the root cause.
Sys.setenv(PYTHONHOME=system.file(package="PythonEmbedInR"))


.defineRPackageFunctions()

# TODO fix module import failure
#pyImport("urllib3")
#pyExec("urllib3.disable_warnings()")

pyImport("synapseclient")
pyExec("syn=synapseclient.Synapse()")
message("synapseclient version:", pyGet("synapseclient.__version__"))
}

.addEggsToPath<-function(dir) {
# modules with .egg extensions (such as future and synapseClient) need to be explicitly added to the sys.path
pyImport("sys")
pyImport("glob")
pyExec(sprintf("sys.path+=glob.glob('%s/*.egg')", dir))
}

.addPythonAndFoldersToSysPath<-function(srcDir) {
pyImport("sys")
pyExec(sprintf("sys.path.append('%s')", file.path(srcDir, "python")))
packageDir<-file.path(srcDir, "python-packages")
pyExec(sprintf("sys.path.append('%s')", packageDir))
#add all .eggs to paths
.addEggsToPath(packageDir)
}


.defineFunction<-function(synName, pyName) {
force(synName)
force(pyName)
Expand All @@ -47,9 +57,7 @@
}
}

.onAttach <-
function(libname, pkgname)
{
.onAttach <- function(libname, pkgname) {
tou <- "\nTERMS OF USE NOTICE:
When using Synapse, remember that the terms and conditions of use require that you:
1) Attribute data contributors when discussing these data or results from these data.
Expand Down
3 changes: 3 additions & 0 deletions cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
rm -rf inst/python/__pycache__
rm -rf inst/python-packages
2 changes: 2 additions & 0 deletions cleanup.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
./cleanup
9 changes: 2 additions & 7 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ set -e

export PWD_FROM_R=${ALT_PWD-`pwd`}

# This code is for linking the package to the R Python client
rm -rf inst/lib
rm -rf inst/bin
rm -rf inst/python/__pycache__
rm -rf inst/python-packages

#export MODULE_INSTALLATION_FOLDER=$PWD/inst
#mkdir -p $MODULE_INSTALLATION_FOLDER
#export PYTHONUSERBASE=$MODULE_INSTALLATION_FOLDER

# This code is for linking the package to the R Python client
Rscript --vanilla tools/installPythonClient.R $PWD_FROM_R

# build the .Rd files
Expand Down
33 changes: 15 additions & 18 deletions inst/python/installPythonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,29 @@ def main(path):
else:
print('No PYTHONPATH env variable')

moduleInstallationFolder=path+os.sep+"inst"
moduleInstallationFolder=path+os.sep+"inst"+os.sep+"python-packages"
sys.path.insert(0, moduleInstallationFolder)
# The preferred approach is to use pip...

call_pip(['install', 'pip', '--upgrade', '--quiet'])
call_pip(['install', '--user', 'urllib3', '--upgrade', '--quiet'])
call_pip(['install', '--user', 'requests', '--upgrade', '--quiet'])
call_pip(['install', '--user', 'six', '--upgrade', '--quiet'])
call_pip(['install', '--user', 'backports.csv', '--upgrade', '--quiet'])
call_pip('pip', moduleInstallationFolder)
call_pip('urllib3', moduleInstallationFolder)
call_pip('requests', moduleInstallationFolder)
call_pip('six', moduleInstallationFolder)
call_pip('backports.csv', moduleInstallationFolder)

# ...but - for some reason - pip breaks when we install future and the python synapse client
# my guess is that pip 'shells out' to call setup.py and hops to another version of
# python on the machine


#pip.main(['install', '--user', 'future', '--upgrade'])
packageName = "future-0.15.2"
linkPrefix = "https://pypi.python.org/packages/5a/f4/99abde815842bc6e97d5a7806ad51236630da14ca2f3b1fce94c0bb94d3d/"
installPackage(packageName, linkPrefix, path)
installPackage(packageName, linkPrefix, path, moduleInstallationFolder)

#pip.main(['install', '--user', 'synapseclient', '--upgrade'])
packageName = "synapseclient-1.6.1"
linkPrefix = "https://pypi.python.org/packages/37/fd/5672e85abc68f4323e19e470cb7eeb0f8dc610566f124c930c3026404fb9/"
installPackage(packageName, linkPrefix, path)
packageName = "synapseclient-1.7.1"
linkPrefix = "https://pypi.python.org/packages/56/da/e489aad73886e6572737ccfe679b3a2bc9e68b05636d4ac30302d0dcf261/"
installPackage(packageName, linkPrefix, path, moduleInstallationFolder)

def installPackage(packageName, linkPrefix, path):
def installPackage(packageName, linkPrefix, path, moduleInstallationFolder):
# download
zipFileName = packageName + ".tar.gz"
x = urllib.request.urlopen(linkPrefix+zipFileName)
Expand Down Expand Up @@ -82,13 +79,13 @@ def installPackage(packageName, linkPrefix, path):

sys.argv=['setup.py', 'install', '--user']
# TODO how do we get 'setup.py' to install into inst/lib?
distutils.core.run_setup(script_name='setup.py', script_args=['install', '--user', '--upgrade', '--quiet'])
distutils.core.run_setup(script_name='setup.py', script_args=['install', '--upgrade', '--quiet', '--target', moduleInstallationFolder])
# step back one level before remove the directory
os.chdir(path)
shutil.rmtree(packageDir)
else:
os.chdir(path)
call_pip(['install', '--user', localZipFile, '--upgrade', '--quiet'])
call_pip(['install', localZipFile, '--upgrade', '--quiet', '--target', moduleInstallationFolder])
os.remove(localZipFile)
finally:
time.sleep(10)
Expand All @@ -107,7 +104,7 @@ def installPackage(packageName, linkPrefix, path):
# os we'll let the system remove the temp file
# os.remove(outfilepath)

def call_pip(args):
def call_pip(packageName, moduleInstallationFolder):
origStdout=sys.stdout
origStderr=sys.stderr
outfile=tempfile.mkstemp()
Expand All @@ -118,7 +115,7 @@ def call_pip(args):
sys.stderr = outfilehandle

try:
rc = pip.main(args)
rc = pip.main(['install', packageName, '--upgrade', '--quiet', '--target', moduleInstallationFolder])
if rc!=0:
raise Exception('pip.main returned '+str(rc))
finally:
Expand Down

0 comments on commit fee02e8

Please sign in to comment.