PYTHONPATH : python patches methods for its extensions

This commit is contained in:
alalazo
2016-03-18 14:40:53 +01:00
parent 82ba0c6c07
commit ec8cc2b528
7 changed files with 53 additions and 27 deletions

View File

@@ -1,12 +1,12 @@
from spack import *
from spack.package import PythonExtension
class PyNose(PythonExtension):
class PyNose(Package):
"""nose extends the test loading and running features of unittest,
making it easier to write, find and run tests."""
homepage = "https://pypi.python.org/pypi/nose"
url = "https://pypi.python.org/packages/source/n/nose/nose-1.3.4.tar.gz"
url = "https://pypi.python.org/packages/source/n/nose/nose-1.3.4.tar.gz"
version('1.3.4', '6ed7169887580ddc9a8e16048d38274d')
version('1.3.6', '0ca546d81ca8309080fc80cb389e7a16')

View File

@@ -1,11 +1,14 @@
import functools
import glob
import inspect
import os
import re
from contextlib import closing
from llnl.util.lang import match_predicate
from spack.util.environment import *
from spack import *
import spack
from llnl.util.lang import match_predicate
from spack import *
from spack.util.environment import *
class Python(Package):
@@ -111,6 +114,31 @@ def modify_module(self, module, spec, ext_spec):
else:
module.python = Executable(join_path(spec.prefix.bin, 'python'))
# The code below patches the any python extension to have good defaults for `setup_dependent_environment` and
# `setup_environment` only if the extension didn't override any of these functions explicitly.
def _setup_env(self, env):
site_packages = glob.glob(join_path(self.spec.prefix.lib, "python*/site-packages"))
if site_packages:
env.prepend_path('PYTHONPATH', site_packages[0])
def _setup_denv(self, env, extension_spec):
pass
pkg_cls = type(ext_spec.package) # Retrieve the type we may want to patch
if 'python' in pkg_cls.extendees:
# List of overrides we are interested in
interesting_overrides = ['setup_environment', 'setup_dependent_environment']
overrides_found = [
(name, defining_cls) for name, _, defining_cls, _, in inspect.classify_class_attrs(pkg_cls)
if
name in interesting_overrides and # The attribute has the right name
issubclass(defining_cls, Package) and defining_cls is not Package # and is an actual override
]
if not overrides_found:
# If no override were found go on patching
pkg_cls.setup_environment = functools.wraps(Package.setup_environment)(_setup_env)
pkg_cls.setup_dependent_environment = functools.wraps(Package.setup_dependent_environment)(_setup_denv)
# Add variables for lib/pythonX.Y and lib/pythonX.Y/site-packages dirs.
module.python_lib_dir = os.path.join(ext_spec.prefix, self.python_lib_dir)
module.python_include_dir = os.path.join(ext_spec.prefix, self.python_include_dir)