Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepend to $PYTHONPATH or $EBPYTHONPREFIXES in generated module files by automatically scanning for python site package directories #4539

Merged
merged 27 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
27c32ef
Add option for preferring EBPYTHONPREFIXES
Micket May 22, 2024
4652509
Add automatic detection of python site-package dir in installations
Micket May 22, 2024
1acb45a
Switch to using prepend_paths instead of append_paths
Micket Jul 3, 2024
d1c889f
Fix relative paths when globbing for python paths
Micket Jul 4, 2024
0310bed
Simplify generated EBPYTHONPREFIXES path
Micket Jul 4, 2024
a1e4ecc
Rework logic to allow nonstandard PYTHONPATHs
Micket Jul 6, 2024
4ed16be
Use defaultdict with sets for added_paths_per_key
Micket Jul 7, 2024
9f3cf6a
Exclude base python installations themselves from automatic PYTHONPATH
Micket Aug 13, 2024
037827b
Fix logic when someone else added PYTHONPATH/EBPYTHONPREFIXES
Micket Aug 15, 2024
38da36b
Add runtime_only flag to dependencies()
Micket Aug 15, 2024
46a52df
Switch to more reliable way of checking for direct dependencies.
Micket Aug 15, 2024
fb51411
Drop extra check for multiple PYTHONPATH's
Micket Aug 15, 2024
a064006
Fix check for Python runtime dep
Micket Aug 15, 2024
d281fc8
Switch method of detecting base python install
Micket Aug 16, 2024
11cffaa
Add all the found python paths even when using PYTHONPATH
Micket Aug 16, 2024
f90a12b
Drop lib64 variant as it's not supported by sitecustomize.py
Micket Aug 16, 2024
1d78e6c
Default --prefer-ebpythonprefixes to false
Micket Aug 16, 2024
7b7a59f
Rename options and parameters for ebpythonprefixes change
Micket Sep 19, 2024
dc488f5
Switch to using relpath to make code robust
Micket Sep 23, 2024
7e417d3
Use relative paths when looking for python paths
Micket Sep 26, 2024
9813bc8
Replace glob(root_dir=...) which only is supported in python 3.10
Micket Sep 27, 2024
206736e
add test to verify that $PYTHONPATH or $EBPYTHONPREFIXES are set corr…
boegel Sep 30, 2024
5eb6d07
move logic to set $PYTHONPATH or $EBPYTHONPREFIX to dedicated make_mo…
boegel Sep 30, 2024
835e63c
Merge pull request #5 from boegel/ebpythonprefixes2
Micket Sep 30, 2024
76fec7b
take into account multi_deps when determining whether to use $PYTHONP…
boegel Sep 30, 2024
11f132e
remove force_pythonpath easyconfig paramter, can be re-introduced lat…
boegel Sep 30, 2024
c3b5011
Merge pull request #6 from boegel/ebpythonprefixes2
Micket Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion easybuild/framework/easyblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
from easybuild.tools.build_details import get_build_stats
from easybuild.tools.build_log import EasyBuildError, EasyBuildExit, dry_run_msg, dry_run_warning, dry_run_set_dirs
from easybuild.tools.build_log import print_error, print_msg, print_warning
from easybuild.tools.config import CHECKSUM_PRIORITY_JSON, DEFAULT_ENVVAR_USERS_MODULES
from easybuild.tools.config import CHECKSUM_PRIORITY_JSON, DEFAULT_ENVVAR_USERS_MODULES, PYTHONPATH, EBPYTHONPREFIXES
from easybuild.tools.config import FORCE_DOWNLOAD_ALL, FORCE_DOWNLOAD_PATCHES, FORCE_DOWNLOAD_SOURCES
from easybuild.tools.config import EASYBUILD_SOURCES_URL # noqa
from easybuild.tools.config import build_option, build_path, get_log_filename, get_repository, get_repositorypath
Expand Down Expand Up @@ -1390,6 +1390,49 @@ def make_module_description(self):
"""
return self.module_generator.get_description()

def make_module_pythonpath(self):
"""
Add lines for module file to update $PYTHONPATH or $EBPYTHONPREFIXES,
if they aren't already present and the standard lib/python*/site-packages subdirectory exists
"""
lines = []
if not os.path.isfile(os.path.join(self.installdir, 'bin', 'python')): # only needed when not a python install
python_subdir_pattern = os.path.join(self.installdir, 'lib', 'python*', 'site-packages')
candidate_paths = (os.path.relpath(path, self.installdir) for path in glob.glob(python_subdir_pattern))
python_paths = [path for path in candidate_paths if re.match(r'lib/python\d+\.\d+/site-packages', path)]

# determine whether Python is a runtime dependency;
# if so, we assume it was installed with EasyBuild, and hence is aware of $EBPYTHONPREFIXES
runtime_deps = [dep['name'] for dep in self.cfg.dependencies(runtime_only=True)]

# don't use $EBPYTHONPREFIXES unless we can and it's preferred or necesary (due to use of multi_deps)
use_ebpythonprefixes = False
multi_deps = self.cfg['multi_deps']

if 'Python' in runtime_deps:
self.log.info("Found Python runtime dependency, so considering $EBPYTHONPREFIXES...")

if build_option('prefer_python_search_path') == EBPYTHONPREFIXES:
self.log.info("Preferred Python search path is $EBPYTHONPREFIXES, so using that")
use_ebpythonprefixes = True

elif multi_deps and 'Python' in multi_deps:
self.log.info("Python is listed in 'multi_deps', so using $EBPYTHONPREFIXES instead of $PYTHONPATH")
use_ebpythonprefixes = True

if python_paths:
# add paths unless they were already added
if use_ebpythonprefixes:
path = '' # EBPYTHONPREFIXES are relative to the install dir
if path not in self.module_generator.added_paths_per_key[EBPYTHONPREFIXES]:
lines.append(self.module_generator.prepend_paths(EBPYTHONPREFIXES, path))
else:
for python_path in python_paths:
if python_path not in self.module_generator.added_paths_per_key[PYTHONPATH]:
lines.append(self.module_generator.prepend_paths(PYTHONPATH, python_path))

return lines

def make_module_extra(self, altroot=None, altversion=None):
"""
Set extra stuff in module file, e.g. $EBROOT*, $EBVERSION*, etc.
Expand Down Expand Up @@ -1438,6 +1481,9 @@ def make_module_extra(self, altroot=None, altversion=None):
value, type(value))
lines.append(self.module_generator.append_paths(key, value, allow_abs=self.cfg['allow_append_abs_path']))

# add lines to update $PYTHONPATH or $EBPYTHONPREFIXES
lines.extend(self.make_module_pythonpath())

modloadmsg = self.cfg['modloadmsg']
if modloadmsg:
# add trailing newline to prevent that shell prompt is 'glued' to module load message
Expand Down
8 changes: 6 additions & 2 deletions easybuild/framework/easyconfig/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,15 +1105,19 @@ def filter_deps(self, deps):

return retained_deps

def dependencies(self, build_only=False):
def dependencies(self, build_only=False, runtime_only=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we have either an "enum" dep_type=BUILD|RUNTIME|BOTH or at least use include_build=True, include_runtime=True because now we have a potentially "invalid" call: When both are True

Maybe we should at least rename to exclude_runtime, exclude_build which would be mostly backwards compatible and setting both to True seems more reasonable to return nothing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's follow up on that in a separate PR, I want to get this PR merged ASAP.

Maybe open an issue for it, so we can tag it with 5.0 milestone.

"""
Returns an array of parsed dependencies (after filtering, if requested)
dependency = {'name': '', 'version': '', 'system': (False|True), 'versionsuffix': '', 'toolchain': ''}
Iterable builddependencies are flattened when not iterating.

:param build_only: only return build dependencies, discard others
:param runtime_only: only return runtime dependencies, discard others
"""
deps = self.builddependencies()
if runtime_only:
deps = []
else:
deps = self.builddependencies()

if not build_only:
# use += rather than .extend to get a new list rather than updating list of build deps in place...
Expand Down
7 changes: 7 additions & 0 deletions easybuild/tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
OUTPUT_STYLE_RICH = 'rich'
OUTPUT_STYLES = (OUTPUT_STYLE_AUTO, OUTPUT_STYLE_BASIC, OUTPUT_STYLE_NO_COLOR, OUTPUT_STYLE_RICH)

PYTHONPATH = 'PYTHONPATH'
EBPYTHONPREFIXES = 'EBPYTHONPREFIXES'
PYTHON_SEARCH_PATH_TYPES = [PYTHONPATH, EBPYTHONPREFIXES]


class Singleton(ABCMeta):
"""Serves as metaclass for classes that should implement the Singleton pattern.
Expand Down Expand Up @@ -407,6 +411,9 @@ def mk_full_default_path(name, prefix=DEFAULT_PREFIX):
OUTPUT_STYLE_AUTO: [
'output_style',
],
PYTHONPATH: [
'prefer_python_search_path',
]
}
# build option that do not have a perfectly matching command line option
BUILD_OPTIONS_OTHER = {
Expand Down
5 changes: 3 additions & 2 deletions easybuild/tools/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import os
import re
import tempfile
from collections import defaultdict
from contextlib import contextmanager
from easybuild.tools import LooseVersion
from textwrap import wrap
Expand Down Expand Up @@ -153,7 +154,7 @@ def start_module_creation(self):
raise EasyBuildError('Module creation already in process. '
'You cannot create multiple modules at the same time!')
# Mapping of keys/env vars to paths already added
self.added_paths_per_key = dict()
self.added_paths_per_key = defaultdict(set)
txt = self.MODULE_SHEBANG
if txt:
txt += '\n'
Expand Down Expand Up @@ -212,7 +213,7 @@ def _filter_paths(self, key, paths):
print_warning('Module creation has not been started. Call start_module_creation first!')
return paths

added_paths = self.added_paths_per_key.setdefault(key, set())
added_paths = self.added_paths_per_key[key]
# paths can be a string
if isinstance(paths, str):
if paths in added_paths:
Expand Down
5 changes: 5 additions & 0 deletions easybuild/tools/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
from easybuild.tools.config import OUTPUT_STYLE_AUTO, OUTPUT_STYLES, WARN
from easybuild.tools.config import get_pretend_installpath, init, init_build_options, mk_full_default_path
from easybuild.tools.config import BuildOptions, ConfigurationVariables
from easybuild.tools.config import PYTHON_SEARCH_PATH_TYPES, PYTHONPATH
from easybuild.tools.configobj import ConfigObj, ConfigObjError
from easybuild.tools.docs import FORMAT_JSON, FORMAT_MD, FORMAT_RST, FORMAT_TXT
from easybuild.tools.docs import avail_cfgfile_constants, avail_easyconfig_constants, avail_easyconfig_licenses
Expand Down Expand Up @@ -490,6 +491,10 @@ def override_options(self):
None, 'store_true', False),
'pre-create-installdir': ("Create installation directory before submitting build jobs",
None, 'store_true', True),
'prefer-python-search-path': (("Prefer using specified environment variable when possible to specify where"
" Python packages were installed; see also "
"https://docs.easybuild.io/python-search-path"),
'choice', 'store_or_None', PYTHONPATH, PYTHON_SEARCH_PATH_TYPES),
'pretend': (("Does the build/installation in a test directory located in $HOME/easybuildinstall"),
None, 'store_true', False, 'p'),
'read-only-installdir': ("Set read-only permissions on installation directory after installation",
Expand Down
64 changes: 64 additions & 0 deletions test/framework/toy_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -4238,6 +4238,70 @@ def test_eb_error(self):
stderr = stderr.getvalue()
self.assertTrue(regex.search(stderr), f"Pattern '{regex.pattern}' should be found in {stderr}")

def test_toy_python(self):
"""
Test whether $PYTHONPATH or $EBPYTHONPREFIXES are set correctly.
"""
# generate fake Python modules that we can use as runtime dependency for toy
# (required condition for use of $EBPYTHONPREFIXES)
fake_mods_path = os.path.join(self.test_prefix, 'modules')
for pyver in ('2.7', '3.6'):
fake_python_mod = os.path.join(fake_mods_path, 'Python', pyver)
if get_module_syntax() == 'Lua':
fake_python_mod += '.lua'
write_file(fake_python_mod, '')
else:
write_file(fake_python_mod, '#%Module')
self.modtool.use(fake_mods_path)

test_ecs = os.path.join(os.path.dirname(__file__), 'easyconfigs', 'test_ecs')
toy_ec = os.path.join(test_ecs, 't', 'toy', 'toy-0.0.eb')

test_ec_txt = read_file(toy_ec)
test_ec_txt += "\npostinstallcmds.append('mkdir -p %(installdir)s/lib/python3.6/site-packages')"
test_ec_txt += "\npostinstallcmds.append('touch %(installdir)s/lib/python3.6/site-packages/foo.py')"

test_ec = os.path.join(self.test_prefix, 'test.eb')
write_file(test_ec, test_ec_txt)
self.run_test_toy_build_with_output(ec_file=test_ec)

toy_mod = os.path.join(self.test_installpath, 'modules', 'all', 'toy', '0.0')
if get_module_syntax() == 'Lua':
toy_mod += '.lua'
toy_mod_txt = read_file(toy_mod)

pythonpath_regex = re.compile('^prepend.path.*PYTHONPATH.*lib/python3.6/site-packages', re.M)

self.assertTrue(pythonpath_regex.search(toy_mod_txt),
f"Pattern '{pythonpath_regex.pattern}' found in: {toy_mod_txt}")

# also check when opting in to use $EBPYTHONPREFIXES instead of $PYTHONPATH
args = ['--prefer-python-search-path=EBPYTHONPREFIXES']
self.run_test_toy_build_with_output(ec_file=test_ec, extra_args=args)
toy_mod_txt = read_file(toy_mod)
# if Python is not listed as a runtime dependency then $PYTHONPATH is still used,
# because the Python dependency used must be aware of $EBPYTHONPREFIXES
# (see sitecustomize.py installed by Python easyblock)
self.assertTrue(pythonpath_regex.search(toy_mod_txt),
f"Pattern '{pythonpath_regex.pattern}' found in: {toy_mod_txt}")

# if Python is listed as runtime dependency, then $EBPYTHONPREFIXES is used if it's preferred
write_file(test_ec, test_ec_txt + "\ndependencies = [('Python', '3.6', '', SYSTEM)]")
self.run_test_toy_build_with_output(ec_file=test_ec, extra_args=args)
toy_mod_txt = read_file(toy_mod)

ebpythonprefixes_regex = re.compile('^prepend.path.*EBPYTHONPREFIXES.*root', re.M)
self.assertTrue(ebpythonprefixes_regex.search(toy_mod_txt),
f"Pattern '{ebpythonprefixes_regex.pattern}' found in: {toy_mod_txt}")

# if Python is listed in multi_deps, then $EBPYTHONPREFIXES is used, even if it's not explicitely preferred
write_file(test_ec, test_ec_txt + "\nmulti_deps = {'Python': ['2.7', '3.6']}")
self.run_test_toy_build_with_output(ec_file=test_ec)
toy_mod_txt = read_file(toy_mod)

self.assertTrue(ebpythonprefixes_regex.search(toy_mod_txt),
f"Pattern '{ebpythonprefixes_regex.pattern}' found in: {toy_mod_txt}")


def suite():
""" return all the tests in this file """
Expand Down
Loading