-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.py
130 lines (104 loc) · 4.42 KB
/
dependencies.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
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
'''
Copyright (C) 2023 Orange Turbine
https://orangeturbine.com
Created by Jason van Gumster
This file is part of an Install Dependencies Example.
This example is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>.
'''
import os
import pkg_resources
import subprocess
import sys
from pathlib import Path
add_on_path = Path(__file__).parent # assuming this file is at root of add-on
requirements_txt = add_on_path / 'requirements.txt' # assuming requirements.txt is at root of add-on
deps_path = add_on_path / 'deps_public' # might not exist until install_deps is called
# Append dependencies folder to system path so we can import
# (important for Windows machines, but less so for Linux)
sys.path.append(os.fspath(deps_path))
class Dependencies:
# cache variables used to eliminate unnecessary computations
_checked = None
_requirements = None
@staticmethod
def install():
if Dependencies.check():
return True
# Create folder into which pip will install dependencies
try:
deps_path.mkdir(exist_ok=True)
except Exception as e:
print(f'Caught Exception while trying to create dependencies folder')
print(f' Exception: {e}')
print(f' Folder: {deps_path}')
return False
# Ensure pip is installed
try:
subprocess.check_call([sys.executable, "-m", "ensurepip", "--upgrade"])
except subprocess.CalledProcessError as e:
print(f'Caught CalledProcessError while trying to ensure pip is installed')
print(f' Exception: {e}')
print(f' {sys.executable=}')
return False
# Install dependencies from requirements.txt
try:
cmd = [
sys.executable,
"-m",
"pip",
"install",
"-r",
os.fspath(requirements_txt),
"--target",
os.fspath(deps_path)
]
print(f'Installing: {cmd}')
subprocess.check_call(cmd)
except subprocess.CalledProcessError as e:
print(f'Caught CalledProcessError while trying to install dependencies')
print(f' Exception: {e}')
print(f' Requirements: {requirements_txt}')
print(f' Folder: {deps_path}')
return False
return Dependencies.check(force=True)
@staticmethod
def check(*, force=False):
if force:
Dependencies._checked = None
elif Dependencies._checked is not None:
# Assume everything is installed
return Dependencies._checked
Dependencies._checked = False
if deps_path.exists():
try:
# Ensure all required dependencies are installed in dependencies folder
ws = pkg_resources.WorkingSet(entries=[ os.fspath(deps_path) ])
for dep in Dependencies.requirements(force=force):
ws.require(dep)
# If we get here, we found all required dependencies
Dependencies._checked = True
except Exception as e:
print(f'Caught Exception while trying to check dependencies')
print(f' Exception: {e}')
return Dependencies._checked
@staticmethod
def requirements(*, force=False):
if force:
Dependencies._requirements = None
elif Dependencies._requirements is not None:
return Dependencies._requirements
# load and cache requirements
with requirements_txt.open() as requirements:
dependencies = pkg_resources.parse_requirements(requirements)
Dependencies._requirements = [ dep.project_name for dep in dependencies ]
return Dependencies._requirements