Skip to content

Commit

Permalink
pip install .-able libecl
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkwah committed Feb 20, 2020
1 parent 97dc442 commit 78aebe3
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 142 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ scratch.sparsebundle
*.DS_Store
__ecl_lib_path.py
__ecl_lib_info.py

/venv/
/_skbuild/
/python/ecl/version.py
.*
*.egg-info/
89 changes: 50 additions & 39 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,55 @@ set( ECL_VERSION_MICRO "not-available" )
execute_process(COMMAND date "+%Y-%m-%d %H:%M:%S" OUTPUT_VARIABLE ECL_BUILD_TIME )
string(STRIP "${ECL_BUILD_TIME}" ECL_BUILD_TIME)

find_package(Git)

if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" status
RESULT_VARIABLE RESULT
OUTPUT_QUIET
ERROR_QUIET
)

if(NOT "${RESULT}" STREQUAL 0)
set(GIT_FOUND OFF)
if(ECL_VERSION)
# Have we been provided with an explicitly-set version?
string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\1" ECL_VERSION_MAJOR "${ECL_VERSION}")
string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\2" ECL_VERSION_MINOR "${ECL_VERSION}")
string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\3" ECL_VERSION_MICRO "${ECL_VERSION}")
else()
# Otherwise try to discover it via git
find_package(Git)

if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" status
RESULT_VARIABLE RESULT
OUTPUT_QUIET
ERROR_QUIET
)

if(NOT "${RESULT}" STREQUAL 0)
set(GIT_FOUND OFF)
endif()
endif()
endif()

if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" rev-parse HEAD
OUTPUT_VARIABLE GIT_COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" rev-parse --short HEAD
OUTPUT_VARIABLE GIT_COMMIT_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" describe --tags
OUTPUT_VARIABLE GIT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
)

string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\1" ECL_VERSION_MAJOR "${GIT_TAG}")
string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\2" ECL_VERSION_MINOR "${GIT_TAG}")
string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\3" ECL_VERSION_MICRO "${GIT_TAG}")
else()
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" rev-parse HEAD
OUTPUT_VARIABLE GIT_COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" rev-parse --short HEAD
OUTPUT_VARIABLE GIT_COMMIT_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND ${GIT_EXECUTABLE} "--git-dir=${CMAKE_SOURCE_DIR}/.git" describe --tags
OUTPUT_VARIABLE GIT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
)

string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\1" ECL_VERSION_MAJOR "${GIT_TAG}")
string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\2" ECL_VERSION_MINOR "${GIT_TAG}")
string( REGEX REPLACE "^([^.]+)\\.([^.]+)\\.(.+)$" "\\3" ECL_VERSION_MICRO "${GIT_TAG}")
else()
set( GIT_COMMIT "unknown (git not found!)")
set( GIT_COMMIT_SHORT "unknown (git not found!)")
message( WARNING "Git not found. Build will not contain correct version info." )
endif()
endif()

message( STATUS "libecl version: ${ECL_VERSION_MAJOR}.${ECL_VERSION_MINOR}.${ECL_VERSION_MICRO}" )
Expand Down Expand Up @@ -375,6 +383,9 @@ if (ENABLE_PYTHON)
endif()
endif()
install(EXPORT ecl-config DESTINATION share/cmake/ecl)
export(TARGETS ecl FILE eclConfig.cmake)
export(PACKAGE ecl)
if (NOT SKBUILD)
# Avoid installing when calling from python setup.py
install(EXPORT ecl-config DESTINATION share/cmake/ecl)
export(TARGETS ecl FILE eclConfig.cmake)
export(PACKAGE ecl)
endif()
4 changes: 2 additions & 2 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ install(TARGETS ecl
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY include/
DESTINATION include
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
DESTINATION include
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

if (NOT BUILD_TESTS)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools", "setuptools_scm", "wheel", "scikit-build", "cmake", "ninja"]
139 changes: 88 additions & 51 deletions python/ecl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,74 +64,112 @@
module=r'ecl|ert',
)

from cwrap import load as cwrapload
from cwrap import Prototype

try:
import ert_site_init
except ImportError:
pass
ECL_LEGACY_INSTALLED = not os.path.isdir(os.path.join(os.path.dirname(__file__), ".libs"))
if not ECL_LEGACY_INSTALLED:
from .version import version as __version__


required_version_hex = 0x02070000
def get_include():
return os.path.join(os.path.dirname(__file__), ".include")

ecl_lib_path = None
ert_so_version = ""
__version__ = "0.0.0"

def dlopen_libecl():
import ctypes
import platform

# 1. Try to load the __ecl_lib_info module; this module has been
# configured by cmake during the build configuration process. The
# module should contain the variable lib_path pointing to the
# directory with shared object files.
try:
from .__ecl_lib_info import EclLibInfo
ecl_lib_path = EclLibInfo.lib_path
ert_so_version = EclLibInfo.so_version
__version__ = EclLibInfo.__version__
except ImportError:
pass
except AttributeError:
pass
path = os.path.join(os.path.dirname(__file__), ".libs")
if platform.system() == "Linux":
path = os.path.join(path, "libecl.so")
elif platform.system() == "Darwin":
path = os.path.join(path, "libecl.dylib")
else:
raise NotImplementedError("Invalid platform")

return ctypes.CDLL(path, ctypes.RTLD_GLOBAL)

# 2. Using the environment variable ERT_LIBRARY_PATH it is possible to
# override the default algorithms. If the ERT_LIBRARY_PATH is set
# to a non existing directory a warning will go to stderr and the
# setting will be ignored.
env_lib_path = os.getenv("ERT_LIBRARY_PATH")
if env_lib_path:
if os.path.isdir( env_lib_path ):
ert_lib_path = os.getenv("ERT_LIBRARY_PATH")
else:
sys.stderr.write("Warning: Environment variable ERT_LIBRARY_PATH points to nonexisting directory:%s - ignored" % env_lib_path)

class EclPrototype(Prototype):
lib = dlopen_libecl()

# Check that the final ert_lib_path setting corresponds to an existing
# directory.
if ecl_lib_path:
if not os.path.isabs(ecl_lib_path):
ecl_lib_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ecl_lib_path))
def __init__(self, prototype, bind=True):
super(EclPrototype, self).__init__(EclPrototype.lib, prototype, bind=bind)
else:
#
# If installed via CMake directly (legacy)
#
from cwrap import load as cwrapload

if not os.path.isdir( ecl_lib_path ):
ecl_lib_path = None
try:
import ert_site_init
except ImportError:
pass


required_version_hex = 0x02070000

if sys.hexversion < required_version_hex:
raise Exception("ERT Python requires Python 2.7.")
ecl_lib_path = None
ert_so_version = ""
__version__ = "0.0.0"

def load(name):

# 1. Try to load the __ecl_lib_info module; this module has been
# configured by cmake during the build configuration process. The
# module should contain the variable lib_path pointing to the
# directory with shared object files.
try:
return cwrapload(name, path=ecl_lib_path, so_version=ert_so_version)
from .__ecl_lib_info import EclLibInfo
ecl_lib_path = EclLibInfo.lib_path
ert_so_version = EclLibInfo.so_version
__version__ = EclLibInfo.__version__
except ImportError:
# For pip installs, setup.py puts the shared lib in this directory
own_dir=os.path.dirname(os.path.abspath(__file__))
return cwrapload(name, path=own_dir, so_version=ert_so_version)
pass
except AttributeError:
pass


# 2. Using the environment variable ERT_LIBRARY_PATH it is possible to
# override the default algorithms. If the ERT_LIBRARY_PATH is set
# to a non existing directory a warning will go to stderr and the
# setting will be ignored.
env_lib_path = os.getenv("ERT_LIBRARY_PATH")
if env_lib_path:
if os.path.isdir( env_lib_path ):
ert_lib_path = os.getenv("ERT_LIBRARY_PATH")
else:
sys.stderr.write("Warning: Environment variable ERT_LIBRARY_PATH points to nonexisting directory:%s - ignored" % env_lib_path)


class EclPrototype(Prototype):
lib = load("libecl")
# Check that the final ert_lib_path setting corresponds to an existing
# directory.
if ecl_lib_path:
if not os.path.isabs(ecl_lib_path):
ecl_lib_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ecl_lib_path))

def __init__(self, prototype, bind=True):
super(EclPrototype, self).__init__(EclPrototype.lib, prototype, bind=bind)
if not os.path.isdir( ecl_lib_path ):
ecl_lib_path = None

if sys.hexversion < required_version_hex:
raise Exception("ERT Python requires Python 2.7.")

def load(name):
try:
return cwrapload(name, path=ecl_lib_path, so_version=ert_so_version)
except ImportError:
# For pip installs, setup.py puts the shared lib in this directory
own_dir=os.path.dirname(os.path.abspath(__file__))
return cwrapload(name, path=own_dir, so_version=ert_so_version)

class EclPrototype(Prototype):
lib = load("libecl")

def __init__(self, prototype, bind=True):
super(EclPrototype, self).__init__(EclPrototype.lib, prototype, bind=bind)

#
# Common
#

from .ecl_type import EclTypeEnum, EclDataType
from .ecl_util import EclFileEnum, EclFileFlagEnum, EclPhaseEnum, EclUnitTypeEnum , EclUtil
Expand All @@ -147,4 +185,3 @@ def root():
Will print the filesystem root of the current ert package.
"""
return os.path.abspath( os.path.join( os.path.dirname( __file__ ) , "../"))

4 changes: 0 additions & 4 deletions python/tests/global_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
set(TEST_SOURCES
__init__.py
test_import.py
test_pylint.py
)

add_python_package("python.tests.global_tests" "${PYTHON_INSTALL_PREFIX}/tests/global_tests" "${TEST_SOURCES}" False)

addPythonTest(tests.global_tests.test_import.ImportEcl)
addPythonTest(tests.global_tests.test_pylint.LintErt)


15 changes: 11 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
cwrap
numpy
pandas
sphinx
functools32;python_version=='2.7'
future
ninja
numpy;python_version>='3.0'
numpy<=1.16.6;python_version=='2.7'
pandas;python_version>='3.0'
pandas<=0.25.3;python_version=='2.7'
scikit-build
setuptools
setuptools_scm
six
functools32;python_version=='2.7'
sphinx
wheel
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[aliases]
test = pytest
Loading

0 comments on commit 78aebe3

Please sign in to comment.