Skip to content

Commit 3f8a928

Browse files
committed
[GR-18033] ginstall: avoid reinstallation of already installed packages
PullRequest: graalpython/637
2 parents 46273ae + d029aba commit 3f8a928

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed

graalpython/lib-graalpython/modules/ginstall.py

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,24 @@
4343
import shutil
4444
import site
4545
import subprocess
46-
import sys
4746
import tempfile
47+
import importlib
48+
import sys
49+
50+
51+
def pip_package(name=None):
52+
def decorator(func):
53+
def wrapper(*args, **kwargs):
54+
_name = name if name else func.__name__
55+
try:
56+
importlib.import_module(_name)
57+
del sys.modules[_name]
58+
except ImportError:
59+
print("Installing required dependency: {}".format(_name))
60+
func(*args, **kwargs)
61+
return wrapper
62+
return decorator
63+
4864

4965
def system(cmd, msg=""):
5066
print("+", cmd)
@@ -54,6 +70,7 @@ def system(cmd, msg=""):
5470

5571

5672
def known_packages():
73+
@pip_package()
5774
def pytest(**kwargs):
5875
wcwidth(**kwargs)
5976
importlib_metadata(**kwargs)
@@ -125,9 +142,11 @@ def _importtestmodule(self):
125142
"""
126143
install_from_pypi("pytest==5.0.1", patch=patch, **kwargs)
127144

145+
@pip_package()
128146
def py(**kwargs):
129147
install_from_pypi("py==1.8.0", **kwargs)
130148

149+
@pip_package()
131150
def attrs(**kwargs):
132151
patch = """
133152
--- a/setup.py
@@ -143,17 +162,21 @@ def attrs(**kwargs):
143162
"""
144163
install_from_pypi("attrs==19.1.0", patch=patch, **kwargs)
145164

165+
@pip_package()
146166
def pyparsing(**kwargs):
147167
install_from_pypi("pyparsing==2.4.2", **kwargs)
148168

169+
@pip_package()
149170
def packaging(**kwargs):
150171
six(**kwargs)
151172
pyparsing(**kwargs)
152173
install_from_pypi("packaging==19.0", **kwargs)
153174

175+
@pip_package()
154176
def more_itertools(**kwargs):
155177
install_from_pypi("more-itertools==7.0.0", **kwargs)
156178

179+
@pip_package()
157180
def atomicwrites(**kwargs):
158181
patch = """
159182
--- a/setup.py
@@ -171,65 +194,80 @@ def atomicwrites(**kwargs):
171194
"""
172195
install_from_pypi("atomicwrites==1.3.0", patch=patch, **kwargs)
173196

197+
@pip_package()
174198
def pluggy(**kwargs):
175199
zipp(**kwargs)
176200
install_from_pypi("pluggy==0.12.0", **kwargs)
177201

202+
@pip_package()
178203
def zipp(**kwargs):
179204
setuptools_scm(**kwargs)
180205
install_from_pypi("zipp==0.5.0", **kwargs)
181206

207+
@pip_package()
182208
def importlib_metadata(**kwargs):
183209
zipp(**kwargs)
184210
install_from_pypi("importlib-metadata==0.19", **kwargs)
185211

212+
@pip_package()
186213
def wcwidth(**kwargs):
214+
six(**kwargs)
187215
install_from_pypi("wcwidth==0.1.7", **kwargs)
188216

217+
@pip_package()
189218
def PyYAML(**kwargs):
190219
install_from_pypi("PyYAML==3.13", **kwargs)
191220

221+
@pip_package()
192222
def six(**kwargs):
193223
install_from_pypi("six==1.12.0", **kwargs)
194224

225+
@pip_package()
195226
def Cython(**kwargs):
196227
install_from_pypi("Cython==0.29.2", **kwargs)
197228

229+
@pip_package()
198230
def setuptools(**kwargs):
199-
try:
200-
import six as _six
201-
except ImportError:
202-
print("Installing required dependency: six")
203-
six(**kwargs)
231+
six(**kwargs)
204232
install_from_pypi("setuptools==41.0.1", **kwargs)
205233

234+
@pip_package()
206235
def pkgconfig(**kwargs):
207236
install_from_pypi("pkgconfig==1.5.1", **kwargs)
208237

238+
@pip_package()
209239
def wheel(**kwargs):
210240
install_from_pypi("wheel==0.33.4", **kwargs)
211241

242+
@pip_package()
212243
def protobuf(**kwargs):
213244
install_from_pypi("protobuf==3.8.0", **kwargs)
214245

246+
@pip_package()
215247
def Keras_preprocessing(**kwargs):
216248
install_from_pypi("Keras-Preprocessing==1.0.5", **kwargs)
217249

250+
@pip_package()
218251
def gast(**kwargs):
219252
install_from_pypi("gast==0.2.2", **kwargs)
220253

254+
@pip_package()
221255
def astor(**kwargs):
222256
install_from_pypi("astor==0.8.0", **kwargs)
223257

258+
@pip_package()
224259
def absl_py(**kwargs):
225260
install_from_pypi("absl-py==0.7.1", **kwargs)
226261

262+
@pip_package()
227263
def mock(**kwargs):
228264
install_from_pypi("mock==3.0.5", **kwargs)
229265

266+
@pip_package()
230267
def Markdown(**kwargs):
231268
install_from_pypi("Markdown==3.1.1", **kwargs)
232269

270+
@pip_package()
233271
def Werkzeug(**kwargs):
234272
install_from_pypi("Werkzeug==0.15.4", **kwargs)
235273

@@ -256,16 +294,13 @@ def Werkzeug(**kwargs):
256294
# h5py(**kwargs)
257295
# install_from_pypi("Keras-Applications==1.0.6", **kwargs)
258296

297+
@pip_package()
259298
def setuptools_scm(**kwargs):
260299
install_from_pypi("setuptools_scm==1.15.0", **kwargs)
261300

301+
@pip_package()
262302
def numpy(**kwargs):
263-
try:
264-
import setuptools as st
265-
except ImportError:
266-
print("Installing required dependency: setuptools")
267-
setuptools(**kwargs)
268-
303+
setuptools(**kwargs)
269304
patch = r'''
270305
diff --git a/numpy/__init__.py b/numpy/__init__.py
271306
index ba88c73..e4db404 100644
@@ -1092,44 +1127,21 @@ def get_lapack_lite_sources(ext, build_dir):
10921127
'''
10931128
install_from_pypi("numpy==1.16.4", patch=patch, env={"NPY_NUM_BUILD_JOBS": "1"})
10941129

1095-
1130+
@pip_package()
10961131
def dateutil(**kwargs):
1097-
try:
1098-
import setuptools_scm as st_scm
1099-
except ImportError:
1100-
print("Installing required dependency: setuptools_scm")
1101-
setuptools_scm(**kwargs)
1132+
setuptools_scm(**kwargs)
11021133
install_from_pypi("python-dateutil==2.7.5", **kwargs)
11031134

1104-
1135+
@pip_package()
11051136
def pytz(**kwargs):
11061137
install_from_pypi("pytz==2018.7", **kwargs)
11071138

1108-
1139+
@pip_package()
11091140
def pandas(**kwargs):
1110-
try:
1111-
import pytz as _dummy_pytz
1112-
except ImportError:
1113-
print("Installing required dependency: pytz")
1114-
pytz(**kwargs)
1115-
1116-
try:
1117-
import six as _dummy_six
1118-
except ImportError:
1119-
print("Installing required dependency: six")
1120-
six(**kwargs)
1121-
1122-
try:
1123-
import dateutil as __dummy_dateutil
1124-
except ImportError:
1125-
print("Installing required dependency: dateutil")
1126-
dateutil(**kwargs)
1127-
1128-
try:
1129-
import numpy as np
1130-
except ImportError:
1131-
print("Installing required dependency: numpy")
1132-
numpy(**kwargs)
1141+
pytz(**kwargs)
1142+
six(**kwargs)
1143+
dateutil(**kwargs)
1144+
numpy(**kwargs)
11331145

11341146
# download pandas-0.25.0
11351147
patch = r'''diff --git a/pandas/io/msgpack/_packer.cpp b/pandas/io/msgpack/_packer.cpp
@@ -1386,6 +1398,5 @@ def main(argv):
13861398
install_from_pypi(pkg, ignore_errors=False)
13871399

13881400

1389-
13901401
if __name__ == "__main__":
13911402
main(sys.argv[1:])

0 commit comments

Comments
 (0)