forked from WolframResearch/WolframClientForPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire.py
48 lines (29 loc) · 1.15 KB
/
require.py
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
from __future__ import absolute_import, print_function, unicode_literals
from functools import wraps
from wolframclient.utils.api import pip
def installed_modules():
return {i.key: i.version for i in pip.get_installed_distributions()}
def missing_requirements(*modules):
distributions = installed_modules()
for module in modules:
version = None
if isinstance(module, (tuple, list)):
module, version = module
if not module in distributions or version and not distributions[module] == version:
yield version and "%s==%s" % (module, version) or module
def require_module(*modules):
commands = list(missing_requirements(*modules))
if commands:
print("Update in progress: pip install %s --user" % " ".join(commands))
if pip.running_under_virtualenv():
pip.main(["install"] + commands)
else:
pip.main(["install", "--user"] + commands)
def require(*modules):
def outer(func):
@wraps(func)
def inner(*args, **kw):
require_module(*modules)
return func(*args, **kw)
return inner
return outer