-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored install code to now be in user_setup.py
- Loading branch information
Showing
5 changed files
with
90 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
dependencies = ["\"RSQLite\"", "\"plyr\"", "\"gplots\"", "\"ggplot2\""] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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__": | ||
|
@@ -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, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|