-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
223 lines (192 loc) · 7.81 KB
/
Copy pathsetup.py
File metadata and controls
223 lines (192 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import os
import platform
import subprocess
import sys
def _running_noninteractively():
"""True when there is no human at a terminal to answer a prompt.
This covers CI systems (GitHub Actions sets CI=true), any environment
where CI/PIP_NO_INPUT is set, and plain non-tty stdin.
"""
if os.environ.get("CI") or os.environ.get("PIP_NO_INPUT"):
return True
try:
return not sys.stdin.isatty()
except Exception:
return True
def _is_packaging_only_invocation():
"""True when setup.py is only being asked to build metadata/artifacts
(sdist, bdist_wheel, egg_info, ...), not to actually install the
package. These commands run on build machines (e.g. CI building a
release) that don't need R/rpy2 present at all -- R is a *runtime*
dependency of the installed package, not a build-time one.
"""
packaging_commands = {
"sdist", "bdist_wheel", "bdist", "egg_info", "dist_info",
"check", "--version", "--help", "--help-commands",
}
return any(arg in packaging_commands for arg in sys.argv[1:])
def check_r_installed():
current_platform = platform.system()
if current_platform == "Windows":
try:
subprocess.run(
["reg", "query", "HKLM\\Software\\R-core\\R"], check=True
)
print("R is already installed on Windows.")
return True
except (subprocess.CalledProcessError, FileNotFoundError):
print("R is not installed on Windows.")
return False
elif current_platform in ("Linux", "Darwin"):
try:
subprocess.run(["which", "R"], check=True)
print(f"R is already installed on {current_platform}.")
return True
except (subprocess.CalledProcessError, FileNotFoundError):
print(f"R is not installed on {current_platform}.")
return False
else:
print("Unsupported platform. Unable to check for R installation.")
return False
def install_r():
current_platform = platform.system()
if current_platform == "Windows":
install_command = (
"Start-Process powershell -Verb runAs -ArgumentList "
"'-Command \"& {Invoke-WebRequest "
"https://cran.r-project.org/bin/windows/base/R-4.1.2-win.exe "
"-OutFile R.exe}; Start-Process R.exe -ArgumentList "
"'/SILENT' -Wait}'"
)
subprocess.run(install_command, shell=True)
elif current_platform == "Linux":
install_command = (
"sudo apt update -qq && "
"sudo apt-key adv --keyserver keyserver.ubuntu.com "
"--recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 && "
"sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/' && "
"sudo apt update && "
"sudo apt -y install r-base"
)
subprocess.run(install_command, shell=True)
elif current_platform == "Darwin":
subprocess.run("brew install r", shell=True)
else:
print("Unsupported platform. Unable to install R.")
def install_packages():
"""Install the R-side dependencies, retrying into a local library
('r-learningmachine') if the default library isn't writable."""
attempts = [
{"lib": None},
{"lib": "r-learningmachine"},
]
for attempt in attempts:
lib = attempt["lib"]
lib_arg = f", lib='{lib}'" if lib else ""
try:
if lib:
subprocess.run(["mkdir", "-p", lib], check=True)
subprocess.run(
["Rscript", "-e",
f"utils::install.packages('remotes', dependencies=TRUE{lib_arg})"],
check=True,
)
subprocess.run(
["Rscript", "-e",
f"utils::install.packages(c('R6', 'Rcpp', 'skimr'), dependencies=TRUE{lib_arg})"],
check=True,
)
subprocess.run(
["Rscript", "-e",
f"remotes::install_github('Techtonique/learningmachine'{lib_arg})"],
check=True,
)
print(f"R package installation succeeded (lib={lib!r}).")
return True
except subprocess.CalledProcessError as e:
print(f"Error occurred while installing R packages (lib={lib!r}): {e}")
print(f"Return code: {e.returncode}")
print(
"Warning: could not install the R 'learningmachine' package "
"automatically. Install it manually, e.g.:\n"
" Rscript -e \"remotes::install_github('Techtonique/learningmachine')\""
)
return False
def ensure_r_available():
"""Make sure R is present, installing it (or asking to) only when it
actually makes sense to do so."""
if check_r_installed():
print("No R installation needed.")
return
if _running_noninteractively():
if os.environ.get("LEARNINGMACHINE_AUTO_INSTALL_R") == "1":
print("Non-interactive environment: auto-installing R "
"(LEARNINGMACHINE_AUTO_INSTALL_R=1 was set).")
install_r()
else:
print(
"R is not installed and this looks like a non-interactive "
"environment (CI or no TTY), so setup.py will NOT prompt "
"for input and will NOT attempt to install R automatically.\n"
"Set the environment variable LEARNINGMACHINE_AUTO_INSTALL_R=1 "
"before running setup.py if you want it to try installing R, "
"or install R yourself first: https://cloud.r-project.org/"
)
return
# Interactive session with a real human at a terminal.
try:
install_r_prompt = int(input("Try installing R? 1-yes, 2-no: "))
except (EOFError, ValueError):
install_r_prompt = 2
if install_r_prompt == 1:
print("Installing R...")
install_r()
else:
print(
"Skipping R installation. Install R manually first: "
"https://cloud.r-project.org/"
)
# Building an sdist/wheel (e.g. in CI to publish a release) never needs R
# or rpy2 on the build machine -- those are runtime dependencies for
# whoever installs the package. Only run the R setup dance for actual
# install-type invocations.
if not _is_packaging_only_invocation():
ensure_r_available()
if check_r_installed():
install_packages()
subprocess.run([sys.executable, "-m", "pip", "install", "rpy2"])
else:
print(f"Packaging-only invocation ({' '.join(sys.argv[1:])}); "
"skipping R/rpy2 setup.")
from setuptools import setup, find_packages
from codecs import open
from os import path
# 4 - Package setup -----------------------------------------------
"""The setup script."""
setup(
author="T. Moudiki",
author_email="thierry.moudiki@gmail.com",
python_requires=">=3.6",
classifiers=[
"Development Status :: 2 - Pre-Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Natural Language :: English",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
description="Machine Learning with uncertainty quantification and interpretability",
install_requires=['numpy', 'pandas', 'rpy2>=3.4.5', 'scikit-learn', 'scipy'],
license="BSD Clause Clear license",
long_description="Machine Learning with uncertainty quantification and interpretability.",
include_package_data=True,
keywords="learningmachine",
name="learningmachine",
packages=find_packages(include=["learningmachine", "learningmachine.*"]),
test_suite="tests",
url="https://github.com/Techtonique/learningmachine_python",
version="2.10.0",
zip_safe=False,
)