forked from numenta/nupic.core-legacy
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathhtm_install.py
More file actions
238 lines (207 loc) · 9.11 KB
/
htm_install.py
File metadata and controls
238 lines (207 loc) · 9.11 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# ----------------------------------------------------------------------
# HTM Community Edition of NuPIC
# Copyright (C) 2013-2024, Numenta, Inc.
# Migrated to scikit-build-core: David Keeney, dkeeney@gmail.com, Dec 2024
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# 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 Affero Public License (http://www.gnu.org/licenses) for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program.
# ----------------------------------------------------------------------
# python -m wheel unpack dist/htm-2.2.0-cp313-cp313-win_amd64.whl
# htm_install.py
import subprocess
import os
import glob
import shutil
import sys
import contextlib
import re
def main():
# Check Python version
if sys.version_info < (3, 9):
print("Error: This project requires Python 3.9 or later.")
print("You are running Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
sys.exit(1)
# Check if running in a virtual environment
if not is_running_in_docker() and not in_venv():
print("Error: Not running in a python virtual environment.")
print("Please create a virtual environment before running this script.")
print("You can create one using:")
print(" python -m venv .venv")
print("And activate it using:")
print(" .venv\\Scripts\\activate (Windows)")
print(" source .venv/bin/activate (Linux/macOS)")
sys.exit(1)
# Install build dependencies
print("Installing build dependencies...")
pip_args = [sys.executable, '-m', 'pip', 'install', '--upgrade']
# Add --ignore-installed and --break-system-packages for Docker
if is_running_in_docker():
pip_args.extend(['--ignore-installed', '--break-system-packages'])
pip_args.extend(['pip', 'build', 'setuptools', 'wheel', 'pybind11',
'packaging', 'pytest', 'requests'])
subprocess.run(pip_args, check=True)
# Get the project version and minimum Cmake version from pyproject.toml
# Install toml if needed (for Python < 3.11)
if sys.version_info < (3, 11):
toml_args = [sys.executable, '-m', 'pip', 'install']
if is_running_in_docker():
toml_args.extend(['--ignore-installed', '--break-system-packages'])
toml_args.append('toml')
subprocess.run(toml_args, check=True)
import toml
# toml package expects text mode
with open("pyproject.toml", "r") as f:
pyproject = toml.load(f)
else:
import tomllib as toml
# tomllib expects binary mode
with open("pyproject.toml", "rb") as f:
pyproject = toml.load(f)
project_version = pyproject["project"]["version"]
print(f"Version: {project_version}")
# Ensure CMake is installed and meets the minimum version requirement
import re
min_cmake_version = get_cmake_minimum_version()
if not check_cmake_version(min_cmake_version):
print(f"Installing CMake >={min_cmake_version} using pip...")
cmake_args = [sys.executable, '-m', 'pip', 'install']
if is_running_in_docker():
cmake_args.extend(['--ignore-installed', '--break-system-packages'])
cmake_args.append(f'cmake>={min_cmake_version}')
subprocess.run(cmake_args, check=True)
# if the htm_core library does not exist, go build it.
htm_core_lib_path = os.path.join("build", "Release", "lib")
if not (os.path.exists(os.path.join(htm_core_lib_path, "htm_core.lib")) or
os.path.exists(os.path.join(htm_core_lib_path, "libhtm_core.a"))) :
# Build the C++ components with CMake
print("Building C++ components...")
shutil.rmtree("build/cmake", ignore_errors=True) # Clear cache, Ignore errors if the directory doesn't exist
shutil.rmtree("dist", ignore_errors=True) # Clear wheels, Ignore errors if the directory doesn't exist
# Build the C++ htm_core library
cmake_command = [
"cmake",
"-S", ".",
"-B", "build/cmake",
"-DBINDING_BUILD=CPP_Only",
f"-DPROJECT_VERSION1={project_version}"
]
print("CMake command:", cmake_command) # Print the command before executing it
subprocess.run(cmake_command, check=True, shell=False)
cmake_command = [
"cmake",
"--build",
"build/cmake",
"--config",
"Release",
"--target",
"install"]
print("CMake command:", cmake_command)
subprocess.run(cmake_command, check=True)
print("C++ component build completed")
print("")
else:
print("C++ components already built. Skipping C++ build...")
wheel_file = find_wheel_file(project_version)
if wheel_file == None:
# Build the Python package with scikit-build-core
print("Building Python package...")
cmake_command = [sys.executable, "-m", "build"]
print("CMake command:", cmake_command)
subprocess.run(cmake_command, check=True)
print("")
else:
print("Wheel already exists, skipping build of extensions...")
# locate the .whl file we just created
wheel_file = find_wheel_file(project_version)
if wheel_file is None:
print(f"Error: Could not find the wheel we just created in the 'dist' directory.")
sys.exit(1)
# Unpack the wheel (for testing)
"""
print("Unpack the wheel...")
subprocess.run([sys.executable, '-m', 'wheel', 'unpack', wheel_file], check=True)
"""
# Install the package in Python.
print("Installing the wheel...")
cmake_command = [sys.executable, '-m', 'pip', 'install', '--force-reinstall']
if is_running_in_docker():
cmake_command.append('--break-system-packages')
cmake_command.append(wheel_file)
print("CMake command:", cmake_command)
subprocess.run(cmake_command, check=True)
print('Installation complete!')
def in_venv() -> bool:
"""Determine whether Python is running from a venv."""
import sys
if hasattr(sys, 'real_prefix'):
return True
pfx = getattr(sys, 'base_prefix', sys.prefix)
return pfx != sys.prefix
def is_running_in_docker():
""" Checks if the script is running inside a Docker container. """
# Check for DOCKER_CONTAINER environment variable (set in Dockerfile)
if os.environ.get('DOCKER_CONTAINER'):
return True
# Check cgroup for docker (traditional method)
try:
with open('/proc/1/cgroup', 'r') as f:
return 'docker' in f.read()
except FileNotFoundError:
return False # Not Linux, so probably not Docker
def get_cmake_minimum_version(cmake_file="CMakeLists.txt"):
"""Extracts the minimum required CMake version from a CMakeLists.txt file."""
with open(cmake_file, "r") as f:
for line in f:
match = re.search(r"cmake_minimum_required\(VERSION\s*(\s*[\d.]+)", line)
if match:
return match.group(1)
return None
def check_cmake_version(min_version):
"""Checks the CMake version and returns True if it meets the minimum requirement."""
from packaging import version
try:
result = subprocess.run(
["cmake", "--version"], capture_output=True, text=True, check=True
)
version_output = result.stdout
version_str = version_output.splitlines()[0].split()[-1]
# Parse the version string using packaging.version.Version
installed_version = version.parse(version_str)
required_version = version.parse(min_version)
if installed_version >= required_version:
print(f"Found CMake version {version_str}, which is sufficient.")
return True
else:
print(f"CMake version {version_str} is too old. Requires {min_version}.")
return False
except FileNotFoundError:
return False
except subprocess.CalledProcessError as e:
print(f"Error: CMake version check failed: {e}")
sys.exit(1)
except ValueError:
print(f"Error: Could not parse CMake version: {version_str}")
sys.exit(1)
def find_wheel_file(project_version):
"""Locates the wheel file in dist with matching Python and project versions."""
wheel_file = None
if os.path.exists('dist'):
wheel_files = [f for f in os.listdir('dist') if f.startswith('htm-') and f.endswith('.whl')]
pyver = f"cp{sys.version_info.major}{sys.version_info.minor}"
pjver = f"htm-{project_version}"
for whl in wheel_files:
if pyver in whl and pjver in whl:
wheel_file = os.path.join('dist', whl)
break
return wheel_file
if __name__ == "__main__":
main()