Skip to content

Commit

Permalink
Refactored install code to now be in user_setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrsan committed Mar 1, 2017
1 parent c9655eb commit 76a090b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 76 deletions.
4 changes: 1 addition & 3 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ recursive-include arcproject/scripts/R_postprocessing *
recursive-include arcproject/templates *
recursive-include arcproject/plots *
recursive-include arcproject/data *
recursive-include arcproject/geo *
include arcproject/wqdb.sqlite
include r_dependencies.py
recursive-include arcproject/geo *
1 change: 1 addition & 0 deletions make_distribution.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ If(Test-Path $destination) {Remove-Item $destination}

# Make the wheel and copy files to the distribution folder
& "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcproject-wq-processing\python" .\setup.py bdist_wheel
Copy-Item .\user_setup.py .\distribution
Copy-Item .\dist\arcproject*.whl .\distribution
Copy-Item .\*.pyt .\distribution
Copy-Item *.pyt.xml .\distribution
Expand Down
1 change: 0 additions & 1 deletion r_dependencies.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
dependencies = ["\"RSQLite\"", "\"plyr\"", "\"gplots\"", "\"ggplot2\""]
72 changes: 0 additions & 72 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,12 @@

__author__ = "ambell, nickrsan"

import os
import subprocess
try:
import winreg
except ImportError:
import _winreg as winreg

try:
from arcproject import __version__ as version
except ImportError:
version = '2017.02.28'

from setuptools import setup
from setuptools.command.install import install

import r_dependencies

def get_r_exec():
"""
We make this a function and call it first because setuptools just puts "error:none" if it errors out inside the
main setup call. So, check before doing any other installation, which seems like a good idea anyway.
:return:
"""
try:
registry = winreg.ConnectRegistry("", winreg.HKEY_LOCAL_MACHINE) # open the registry
# current_r_version = winreg.QueryValue(registry, r"Software\R-core\R\Current Version") # get the PISCES location
key = winreg.OpenKey(registry, r"Software\R-core\R")
current_r_path = winreg.QueryValueEx(key, "InstallPath")[0]
current_r_version = winreg.QueryValueEx(key, "Current Version")[0]

winreg.CloseKey(registry)
except WindowsError:
raise WindowsError("Unable to get R path - Make sure R is installed on this machine!")

print("R located at {}".format(current_r_path))

major_version, minor_version, sub_version = current_r_version.split(".")
packages_version = "{}.{}".format(major_version, minor_version) # get the version format used for packages
new_r_package_folder = os.path.join(os.environ["USERPROFILE"], "Documents", "R", "win-library", packages_version)
return os.path.join(current_r_path, "bin", "Rscript.exe"), new_r_package_folder


def write_r_package_install_file(r_exec, new_r_package_folder):

print("Installing R packages using interpreter at {}".format(r_exec))

dependencies_file = os.path.join(os.path.split(os.path.abspath(__file__))[0], "arcproject", "scripts", "install_packages.R")
with open(dependencies_file, 'w') as rdeps:
# for every item in the dependecies list, fill the values into the installation expression, and write that out to the file
rdeps.write("install.packages(c({}), dependencies=TRUE, lib=\"{}\", repos='http://cran.us.r-project.org')".format(", ".join(r_dependencies.dependencies), new_r_package_folder.replace("\\", "\\\\")))

return dependencies_file


class CustomInstallCommand(install):
"""
Make a custom command so we can execute the R package installation after package installation
See https://blog.niteoweb.com/setuptools-run-custom-code-in-setup-py/ for why we're using this
"""
def run(self):
try:
r_exec, new_r_package_folder = get_r_exec()
except WindowsError:
raise WindowsError(
"R does not appear to be installed on this machine. Please install it, making sure to install with version number in registry (installation option) then try again")

install.run(self) # call the parent class's default actions

if not os.path.exists(new_r_package_folder):
os.makedirs(new_r_package_folder)

dependencies_file = write_r_package_install_file(r_exec=r_exec, new_r_package_folder=new_r_package_folder)

subprocess.call([r_exec, dependencies_file]) # call the code to set up R packages



if __name__ == "__main__":
Expand All @@ -94,7 +25,4 @@ def run(self):
author_email="[email protected]",
url='https://github.com/ucd-cws/amaptor',
include_package_data=True,
cmdclass={
'install': CustomInstallCommand,
},
)
88 changes: 88 additions & 0 deletions user_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from __future__ import print_function

import os
import sys
import subprocess

r_dependencies = ["\"RSQLite\"", "\"plyr\"", "\"gplots\"", "\"ggplot2\""]

try:
import winreg
except ImportError:
import _winreg as winreg

def get_r_exec():
"""
We make this a function and call it first because setuptools just puts "error:none" if it errors out inside the
main setup call. So, check before doing any other installation, which seems like a good idea anyway.
:return:
"""
try:
registry = winreg.ConnectRegistry("", winreg.HKEY_LOCAL_MACHINE) # open the registry
# current_r_version = winreg.QueryValue(registry, r"Software\R-core\R\Current Version") # get the PISCES location
key = winreg.OpenKey(registry, r"Software\R-core\R")
current_r_path = winreg.QueryValueEx(key, "InstallPath")[0]
current_r_version = winreg.QueryValueEx(key, "Current Version")[0]

winreg.CloseKey(registry)
except WindowsError:
raise WindowsError("Unable to get R path - Make sure R is installed on this machine!")

print("R located at {}".format(current_r_path))

major_version, minor_version, sub_version = current_r_version.split(".")
packages_version = "{}.{}".format(major_version, minor_version) # get the version format used for packages
new_r_package_folder = os.path.join(os.environ["USERPROFILE"], "Documents", "R", "win-library", packages_version)
return os.path.join(current_r_path, "bin", "Rscript.exe"), new_r_package_folder


def write_r_package_install_file(r_exec, new_r_package_folder):

print("Installing R packages using interpreter at {}".format(r_exec))

dependencies_file = os.path.join(os.path.split(os.path.abspath(__file__))[0], "install_packages.R")
with open(dependencies_file, 'w') as rdeps:
# for every item in the dependecies list, fill the values into the installation expression, and write that out to the file
rdeps.write("install.packages(c({}), dependencies=TRUE, lib=\"{}\", repos='http://cran.us.r-project.org')".format(", ".join(r_dependencies), new_r_package_folder.replace("\\", "\\\\")))

return dependencies_file


def set_up_r_dependencies(new_r_package_folder, r_exec):

if not os.path.exists(new_r_package_folder):
os.makedirs(new_r_package_folder)

dependencies_file = write_r_package_install_file(r_exec=r_exec, new_r_package_folder=new_r_package_folder)

subprocess.call([r_exec, dependencies_file]) # call the code to set up R packages


def find_wheels(path):
"""
find any Python wheel files in the directory provided by "path"
:param path:
:return: list of files in the provided path
"""

return [f for f in os.listdir(path) if (os.path.isfile(os.path.join(path, f)) and f.endswith(".whl"))]


if __name__ == "__main__":
try:
r_exec, new_r_package_folder = get_r_exec()
except WindowsError:
raise WindowsError(
"R does not appear to be installed on this machine. Please install it, making sure to install with version number in registry (installation option) then try again")

subprocess.call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])

install_folder = os.path.split(os.path.abspath(__file__))[0]
for wheel in find_wheels(install_folder):
print("Install wheel file {}".format(wheel))
subprocess.call([sys.executable, "-m", "pip", "install", os.path.join(install_folder, wheel)])
set_up_r_dependencies(new_r_package_folder, r_exec)

print("Installation complete")


0 comments on commit 76a090b

Please sign in to comment.