diff options
author | Eric Veilleux <virx@virxcase.dev> | 2023-10-19 02:26:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-19 08:26:44 +0200 |
commit | 2f76115b317c87a57bdb97be050364f6dd2fdd76 (patch) | |
tree | 007ce3fc56f1e44f9822c445cd67f8554399c5aa | |
parent | 15f01649c7ca238dacf7aa7daa4bc52c4599c3bd (diff) |
Read `requires_py37` from `bot.cfg` (#638)
* Read requires_py37 from bot.cfgg
* Fix `install_requirements_file`
* Fix launching bots with `requires_py37`
* More universal solution
* Remove prints
* Revert .gitignore change
-rw-r--r-- | src/main/python/rlbot/agents/rlbot_runnable.py | 3 | ||||
-rw-r--r-- | src/main/python/rlbot/parsing/bot_config_bundle.py | 3 | ||||
-rw-r--r-- | src/main/python/rlbot/utils/virtual_environment_management.py | 74 |
3 files changed, 78 insertions, 2 deletions
diff --git a/src/main/python/rlbot/agents/rlbot_runnable.py b/src/main/python/rlbot/agents/rlbot_runnable.py index cca075e0..cb1748b4 100644 --- a/src/main/python/rlbot/agents/rlbot_runnable.py +++ b/src/main/python/rlbot/agents/rlbot_runnable.py @@ -9,6 +9,7 @@ NAME_KEY = "name" SUPPORTS_EARLY_START_KEY = "supports_early_start" REQUIRES_TKINTER = "requires_tkinter" USE_VIRTUAL_ENVIRONMENT_KEY = "use_virtual_environment" +REQUIRED_PYTHON_37 = "requires_py37" class RLBotRunnable: @@ -74,6 +75,8 @@ class RLBotRunnable: description="True if the tkinter library is needed.") location_config.add_value(USE_VIRTUAL_ENVIRONMENT_KEY, bool, description="True if the runnable wants to run in a virtual environment.") + location_config.add_value(REQUIRED_PYTHON_37, bool, + description="True if the runnable requires Python 3.7.") details_config = config.add_header_name(DETAILS_HEADER) details_config.add_value('developer', str, description="Name of the bot's creator/developer") diff --git a/src/main/python/rlbot/parsing/bot_config_bundle.py b/src/main/python/rlbot/parsing/bot_config_bundle.py index fe643737..94be1ded 100644 --- a/src/main/python/rlbot/parsing/bot_config_bundle.py +++ b/src/main/python/rlbot/parsing/bot_config_bundle.py @@ -10,7 +10,7 @@ from rlbot.agents.base_agent import BaseAgent, BOT_CONFIG_MODULE_HEADER, BOT_NAM PYTHON_FILE_KEY, LOGO_FILE_KEY, SUPPORTS_EARLY_START_KEY, LOADOUT_GENERATOR_FILE_KEY, SUPPORTS_STANDALONE from rlbot.agents.base_loadout_generator import BaseLoadoutGenerator from rlbot.agents.base_script import SCRIPT_FILE_KEY, BaseScript -from rlbot.agents.rlbot_runnable import RLBotRunnable, REQUIREMENTS_FILE_KEY, REQUIRES_TKINTER, USE_VIRTUAL_ENVIRONMENT_KEY +from rlbot.agents.rlbot_runnable import REQUIRED_PYTHON_37, RLBotRunnable, REQUIREMENTS_FILE_KEY, REQUIRES_TKINTER, USE_VIRTUAL_ENVIRONMENT_KEY from rlbot.utils.requirements_management import get_missing_packages, get_packages_needing_upgrade from rlbot.matchconfig.loadout_config import LoadoutConfig from rlbot.parsing.agent_config_parser import create_looks_configurations, PARTICIPANT_CONFIGURATION_HEADER, \ @@ -34,6 +34,7 @@ class RunnableConfigBundle: self.supports_early_start = self.base_agent_config.get(BOT_CONFIG_MODULE_HEADER, SUPPORTS_EARLY_START_KEY) self.requirements_file = self.get_absolute_path(BOT_CONFIG_MODULE_HEADER, REQUIREMENTS_FILE_KEY) self.use_virtual_environment = self.base_agent_config.getboolean(BOT_CONFIG_MODULE_HEADER, USE_VIRTUAL_ENVIRONMENT_KEY) + self.requires_py37 = self.base_agent_config.getboolean(BOT_CONFIG_MODULE_HEADER, REQUIRED_PYTHON_37) def get_logo_file(self): # logo.png is a convention we established during the wintertide tournament. diff --git a/src/main/python/rlbot/utils/virtual_environment_management.py b/src/main/python/rlbot/utils/virtual_environment_management.py index 34b9b02c..1ea35406 100644 --- a/src/main/python/rlbot/utils/virtual_environment_management.py +++ b/src/main/python/rlbot/utils/virtual_environment_management.py @@ -1,8 +1,10 @@ import os +import platform import sys from pathlib import Path from subprocess import run from types import SimpleNamespace +import types from typing import List from venv import EnvBuilder @@ -74,7 +76,77 @@ class EnvBuilderWithRequirements(EnvBuilder): if finished_process.returncode > 0: sys.stderr.write('FAILED to install requirements!') return - sys.stderr.write('done.\n') + sys.stderr.write('done.\n') + + def ensure_directories(self, env_dir): + """ + Create the directories for the environment. + + Returns a context object which holds paths in the environment, + for use by subsequent logic. + """ + + def create_if_needed(d): + if not os.path.exists(d): + os.makedirs(d) + elif os.path.islink(d) or os.path.isfile(d): + raise ValueError('Unable to create directory %r' % d) + + if os.pathsep in os.fspath(env_dir): + raise ValueError(f'Refusing to create a venv in {env_dir} because ' + f'it contains the PATH separator {os.pathsep}.') + if os.path.exists(env_dir) and self.clear: + self.clear_directory(env_dir) + context = types.SimpleNamespace() + context.env_dir = env_dir + context.env_name = os.path.split(env_dir)[1] + prompt = self.prompt if self.prompt is not None else context.env_name + context.prompt = '(%s) ' % prompt + create_if_needed(env_dir) + executable = sys._base_executable + if not executable: # see gh-96861 + raise ValueError('Unable to determine path to the running ' + 'Python interpreter. Provide an explicit path or ' + 'check that your PATH environment variable is ' + 'correctly set.') + dirname, exename = os.path.split(os.path.abspath(executable)) + + binpath = self._venv_path(env_dir, 'scripts') + context.env_exe = os.path.join(binpath, exename) + is_current_version = True + + if not os.path.exists(context.env_exe): + exename_old = 'python.exe' if platform.system() == "Windows" else 'python3' + env_exe = os.path.join(binpath, exename_old) + if os.path.exists(env_exe): + is_current_version = False + exename = exename_old + context.env_exe = env_exe + context.python_exe = exename + + if is_current_version: + context.executable = executable + context.python_dir = dirname + + incpath = self._venv_path(env_dir, 'include') + libpath = self._venv_path(env_dir, 'purelib') + context.inc_path = incpath + create_if_needed(incpath) + create_if_needed(libpath) + # Issue 21197: create lib64 as a symlink to lib on 64-bit non-OS X POSIX + if ((sys.maxsize > 2**32) and (os.name == 'posix') and + (sys.platform != 'darwin')): + link_path = os.path.join(env_dir, 'lib64') + if not os.path.exists(link_path): # Issue #21643 + os.symlink('lib', link_path) + context.bin_path = binpath + context.bin_name = os.path.relpath(binpath, env_dir) + create_if_needed(binpath) + + # Assign and update the command to use when launching the newly created + # environment, in case it isn't simply the executable script (e.g. bpo-45337) + context.env_exec_cmd = context.env_exe + return context def run_and_dump(self, args: List[str], timeout: int): finished_process = run(args, cwd=self.bundle.config_directory, capture_output=False, timeout=timeout) |