def load_library(self): # Note that the library was located in __init__ and converted to # an absolute path (including checking the CWD). However, we # previously tested that changing the environment (i.e., # changing directories) between defining the ExternalFunction # and loading it would cause the library to still be correctly # loaded. We will re-search for the library here. If it was # previously found, we will be searching for an absolute path # (and the same path will be found again). _abs_lib = find_library(self._library) if _abs_lib is not None: self._library = _abs_lib self._so = cdll.LoadLibrary(self._library) self._known_functions = {} AE = _AMPLEXPORTS() AE.ASLdate = 20160307 def addfunc(name, f, _type, nargs, funcinfo, ae): # trap for Python 3, where the name comes in as bytes() and # not a string if not isinstance(name, str): name = name.decode() self._known_functions[str(name)] = (f, _type, nargs, funcinfo, ae) AE.Addfunc = _AMPLEXPORTS.ADDFUNC(addfunc) def addrandinit(ae, rss, v): # TODO: This should support the randinit ASL option rss(v, 1) AE.Addrandinit = _AMPLEXPORTS.ADDRANDINIT(addrandinit) def atreset(ae, a, b): logger.warning( "AMPL External function: ignoring AtReset call in external " "library. This may result in a memory leak or other " "undesirable behavior.") AE.AtReset = _AMPLEXPORTS.ATRESET(atreset) FUNCADD = CFUNCTYPE(None, POINTER(_AMPLEXPORTS)) FUNCADD(('funcadd_ASL', self._so))(byref(AE))
def __init__(self, *args, **kwargs): if args: raise ValueError( "AMPLExternalFunction constructor does not support " "positional arguments") self._library = kwargs.pop('library', None) self._function = kwargs.pop('function', None) self._known_functions = None self._so = None # Convert the specified library name to an absolute path, and # warn the user if we couldn't find it if self._library is not None: _lib = find_library(self._library) if _lib is not None: self._library = _lib else: logger.warning( 'Defining AMPL external function, but cannot locate ' f'specified library "{self._library}"') ExternalFunction.__init__(self, *args, **kwargs)
def test_find_library_system(self): # Find a system library (before we muck with the PATH) _args = {'cwd': False, 'include_PATH': False, 'pathlist': []} if FileDownloader.get_sysinfo()[0] == 'windows': a = find_library('ntdll', **_args) b = find_library('ntdll.dll', **_args) c = find_library('foo\\bar\\ntdll.dll', **_args) else: a = find_library('c', **_args) b = find_library('libc.so', **_args) c = find_library('foo/bar/libc.so', **_args) self.assertIsNotNone(a) self.assertIsNotNone(b) self.assertIsNotNone(c) self.assertEqual(a, b) # find_library could have found libc.so.6 self.assertTrue(c.startswith(a)) # Verify that the library is loadable (they are all the same # file, so only check one) _lib = ctypes.cdll.LoadLibrary(a) self.assertIsNotNone(_lib)
def test_find_library(self): self.tmpdir = os.path.abspath(tempfile.mkdtemp()) os.chdir(self.tmpdir) config.PYOMO_CONFIG_DIR = self.tmpdir config_libdir = os.path.join(self.tmpdir, 'lib') os.mkdir(config_libdir) config_bindir = os.path.join(self.tmpdir, 'bin') os.mkdir(config_bindir) ldlibdir_name = 'in_ld_lib' ldlibdir = os.path.join(self.tmpdir, ldlibdir_name) os.mkdir(ldlibdir) os.environ['LD_LIBRARY_PATH'] = os.pathsep + ldlibdir + os.pathsep pathdir_name = 'in_path' pathdir = os.path.join(self.tmpdir, pathdir_name) os.mkdir(pathdir) os.environ['PATH'] = os.pathsep + pathdir + os.pathsep libExt = _libExt[_system()][0] f_in_cwd_ldlib_path = 'f_in_cwd_ldlib_path' open(os.path.join(self.tmpdir, f_in_cwd_ldlib_path), 'w').close() open(os.path.join(ldlibdir, f_in_cwd_ldlib_path), 'w').close() open(os.path.join(pathdir, f_in_cwd_ldlib_path), 'w').close() f_in_ldlib_extension = 'f_in_ldlib_extension' open(os.path.join(ldlibdir, f_in_ldlib_extension + libExt), 'w').close() f_in_path = 'f_in_path' open(os.path.join(pathdir, f_in_path), 'w').close() f_in_configlib = 'f_in_configlib' open(os.path.join(config_libdir, f_in_configlib), 'w').close() f_in_configbin = 'f_in_configbin' open(os.path.join(config_bindir, f_in_ldlib_extension), 'w').close() open(os.path.join(config_bindir, f_in_configbin), 'w').close() self.assertEqual(os.path.join(self.tmpdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path)) self.assertEqual(os.path.join(ldlibdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False)) self.assertEqual( os.path.join(ldlibdir, f_in_ldlib_extension) + libExt, find_library(f_in_ldlib_extension)) self.assertEqual(os.path.join(pathdir, f_in_path), find_library(f_in_path)) self.assertEqual(None, find_library(f_in_path, include_PATH=False)) self.assertEqual( os.path.join(pathdir, f_in_path), find_library(f_in_path, pathlist=os.pathsep + pathdir + os.pathsep)) # test an explicit pathlist overrides LD_LIBRARY_PATH self.assertEqual( os.path.join(pathdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False, pathlist=[pathdir])) # test that the PYOMO_CONFIG_DIR 'lib' dir is included self.assertEqual(os.path.join(config_libdir, f_in_configlib), find_library(f_in_configlib)) # and the Bin dir self.assertEqual(os.path.join(config_bindir, f_in_configbin), find_library(f_in_configbin)) # ... but only if include_PATH is true self.assertEqual(None, find_library(f_in_configbin, include_PATH=False)) # And none of them if the pathlist is specified self.assertEqual(None, find_library(f_in_configlib, pathlist=pathdir)) self.assertEqual(None, find_library(f_in_configbin, pathlist=pathdir))
import pyomo.common.unittest as unittest import sys import os import subprocess from itertools import product import pyomo.contrib.parmest.parmest as parmest import pyomo.contrib.parmest.graphics as graphics import pyomo.contrib.parmest as parmestbase import pyomo.environ as pyo from pyomo.opt import SolverFactory ipopt_available = SolverFactory('ipopt').available() from pyomo.common.fileutils import find_library pynumero_ASL_available = False if find_library( 'pynumero_ASL') is None else True testdir = os.path.dirname(os.path.abspath(__file__)) @unittest.skipIf(not parmest.parmest_available, "Cannot test parmest: required dependencies are missing") @unittest.skipIf(not ipopt_available, "The 'ipopt' command is not available") class TestRooneyBiegler(unittest.TestCase): def setUp(self): from pyomo.contrib.parmest.examples.rooney_biegler.rooney_biegler import rooney_biegler_model # Note, the data used in this test has been corrected to use data.loc[5,'hour'] = 7 (instead of 6) data = pd.DataFrame(data=[[1, 8.3], [2, 10.3], [3, 19.0], [4, 16.0], [5, 15.6], [7, 19.8]], columns=['hour', 'y'])
def available(cls): if cls.libname is _NotSet: cls.libname = find_library('pynumero_ASL') if cls.libname is None: return False return os.path.exists(cls.libname)
def test_find_library(self): self.tmpdir = os.path.abspath(tempfile.mkdtemp()) os.chdir(self.tmpdir) # Find a system library (before we muck with the PATH) _args = {'cwd': False, 'include_PATH': False, 'pathlist': []} if FileDownloader.get_sysinfo()[0] == 'windows': a = find_library('ntdll', **_args) b = find_library('ntdll.dll', **_args) c = find_library('foo\\bar\\ntdll.dll', **_args) else: a = find_library('c', **_args) b = find_library('libc.so', **_args) c = find_library('foo/bar/libc.so', **_args) self.assertIsNotNone(a) self.assertIsNotNone(b) self.assertIsNotNone(c) self.assertEqual(a, b) # find_library could have found libc.so.6 self.assertTrue(c.startswith(a)) # Verify that the library is loadable (they are all the same # file, so only check one) _lib = ctypes.cdll.LoadLibrary(a) self.assertIsNotNone(_lib) envvar.PYOMO_CONFIG_DIR = self.tmpdir config_libdir = os.path.join(self.tmpdir, 'lib') os.mkdir(config_libdir) config_bindir = os.path.join(self.tmpdir, 'bin') os.mkdir(config_bindir) ldlibdir_name = 'in_ld_lib' ldlibdir = os.path.join(self.tmpdir, ldlibdir_name) os.mkdir(ldlibdir) os.environ['LD_LIBRARY_PATH'] = os.pathsep + ldlibdir + os.pathsep pathdir_name = 'in_path' pathdir = os.path.join(self.tmpdir, pathdir_name) os.mkdir(pathdir) os.environ['PATH'] = os.pathsep + pathdir + os.pathsep libExt = _libExt[_system()][0] f_in_cwd_ldlib_path = 'f_in_cwd_ldlib_path' open(os.path.join(self.tmpdir, f_in_cwd_ldlib_path), 'w').close() open(os.path.join(ldlibdir, f_in_cwd_ldlib_path), 'w').close() open(os.path.join(pathdir, f_in_cwd_ldlib_path), 'w').close() f_in_ldlib_extension = 'f_in_ldlib_extension' open(os.path.join(ldlibdir, f_in_ldlib_extension + libExt), 'w').close() f_in_path = 'f_in_path' open(os.path.join(pathdir, f_in_path), 'w').close() f_in_configlib = 'f_in_configlib' open(os.path.join(config_libdir, f_in_configlib), 'w').close() f_in_configbin = 'f_in_configbin' open(os.path.join(config_bindir, f_in_ldlib_extension), 'w').close() open(os.path.join(config_bindir, f_in_configbin), 'w').close() self._check_file(find_library(f_in_cwd_ldlib_path), os.path.join(self.tmpdir, f_in_cwd_ldlib_path)) self._check_file(os.path.join(ldlibdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False)) self._check_file( os.path.join(ldlibdir, f_in_ldlib_extension) + libExt, find_library(f_in_ldlib_extension)) self._check_file(os.path.join(pathdir, f_in_path), find_library(f_in_path)) if _system() == 'windows': self._check_file(os.path.join(pathdir, f_in_path), find_library(f_in_path, include_PATH=False)) else: # Note that on Windows, ctypes.util.find_library *always* # searches the PATH self.assertIsNone(find_library(f_in_path, include_PATH=False)) self._check_file( os.path.join(pathdir, f_in_path), find_library(f_in_path, pathlist=os.pathsep + pathdir + os.pathsep)) # test an explicit pathlist overrides LD_LIBRARY_PATH self._check_file( os.path.join(pathdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False, pathlist=[pathdir])) # test that the PYOMO_CONFIG_DIR 'lib' dir is included self._check_file(os.path.join(config_libdir, f_in_configlib), find_library(f_in_configlib)) # and the Bin dir self._check_file(os.path.join(config_bindir, f_in_configbin), find_library(f_in_configbin)) # ... but only if include_PATH is true self.assertIsNone(find_library(f_in_configbin, include_PATH=False)) # And none of them if the pathlist is specified self.assertIsNone(find_library(f_in_configlib, pathlist=pathdir)) self.assertIsNone(find_library(f_in_configbin, pathlist=pathdir))
def test_find_library(self): self.tmpdir = os.path.abspath(tempfile.mkdtemp()) os.chdir(self.tmpdir) config.PYOMO_CONFIG_DIR = self.tmpdir config_libdir = os.path.join(self.tmpdir, 'lib') os.mkdir(config_libdir) config_bindir = os.path.join(self.tmpdir, 'bin') os.mkdir(config_bindir) ldlibdir_name = 'in_ld_lib' ldlibdir = os.path.join(self.tmpdir, ldlibdir_name) os.mkdir(ldlibdir) os.environ['LD_LIBRARY_PATH'] = os.pathsep + ldlibdir + os.pathsep pathdir_name = 'in_path' pathdir = os.path.join(self.tmpdir, pathdir_name) os.mkdir(pathdir) os.environ['PATH'] = os.pathsep + pathdir + os.pathsep libExt = _libExt[_system()][0] f_in_cwd_ldlib_path = 'f_in_cwd_ldlib_path' open(os.path.join(self.tmpdir,f_in_cwd_ldlib_path),'w').close() open(os.path.join(ldlibdir,f_in_cwd_ldlib_path),'w').close() open(os.path.join(pathdir,f_in_cwd_ldlib_path),'w').close() f_in_ldlib_extension = 'f_in_ldlib_extension' open(os.path.join(ldlibdir,f_in_ldlib_extension + libExt),'w').close() f_in_path = 'f_in_path' open(os.path.join(pathdir,f_in_path),'w').close() f_in_configlib = 'f_in_configlib' open(os.path.join(config_libdir, f_in_configlib),'w').close() f_in_configbin = 'f_in_configbin' open(os.path.join(config_bindir, f_in_ldlib_extension),'w').close() open(os.path.join(config_bindir, f_in_configbin),'w').close() self.assertEqual( os.path.join(self.tmpdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path) ) self.assertEqual( os.path.join(ldlibdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False) ) self.assertEqual( os.path.join(ldlibdir, f_in_ldlib_extension) + libExt, find_library(f_in_ldlib_extension) ) self.assertEqual( os.path.join(pathdir, f_in_path), find_library(f_in_path) ) self.assertEqual( None, find_library(f_in_path, include_PATH=False) ) self.assertEqual( os.path.join(pathdir, f_in_path), find_library(f_in_path, pathlist=os.pathsep+pathdir+os.pathsep) ) # test an explicit pathlist overrides LD_LIBRARY_PATH self.assertEqual( os.path.join(pathdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False, pathlist=[pathdir]) ) # test that the PYOMO_CONFIG_DIR 'lib' dir is included self.assertEqual( os.path.join(config_libdir, f_in_configlib), find_library(f_in_configlib) ) # and the Bin dir self.assertEqual( os.path.join(config_bindir, f_in_configbin), find_library(f_in_configbin) ) # ... but only if include_PATH is true self.assertEqual( None, find_library(f_in_configbin, include_PATH=False) ) # And none of them if the pathlist is specified self.assertEqual( None, find_library(f_in_configlib, pathlist=pathdir) ) self.assertEqual( None, find_library(f_in_configbin, pathlist=pathdir) )
from setuptools import setup, find_packages import pybind11.setup_helpers from pybind11.setup_helpers import Pybind11Extension, build_ext import os from pyomo.common.fileutils import find_library original_pybind11_setup_helpers_macos = pybind11.setup_helpers.MACOS pybind11.setup_helpers.MACOS = False try: highs_lib = find_library('highs', include_PATH=True) if highs_lib is None: raise RuntimeError( 'Could not find HiGHS library; Please make sure it is in the LD_LIBRARY_PATH environment variable' ) highs_lib_dir = os.path.dirname(highs_lib) highs_build_dir = os.path.dirname(highs_lib_dir) highs_include_dir = os.path.join(highs_build_dir, 'include', 'highs') if not os.path.exists(os.path.join(highs_include_dir, 'Highs.h')): raise RuntimeError('Could not find HiGHS include directory') extensions = list() extensions.append( Pybind11Extension('highspy.highs_bindings', sources=['highspy/highs_bindings.cpp'], language='c++', include_dirs=[highs_include_dir], library_dirs=[highs_lib_dir], libraries=['highs'])) setup(name='highspy',
def test_find_library_user(self): self.tmpdir = os.path.abspath(tempfile.mkdtemp()) # CWD restored in tearDown os.chdir(self.tmpdir) envvar.PYOMO_CONFIG_DIR = self.tmpdir config_libdir = os.path.join(self.tmpdir, 'lib') os.mkdir(config_libdir) config_bindir = os.path.join(self.tmpdir, 'bin') os.mkdir(config_bindir) ldlibdir_name = 'in_ld_lib' ldlibdir = os.path.join(self.tmpdir, ldlibdir_name) os.mkdir(ldlibdir) os.environ['LD_LIBRARY_PATH'] = os.pathsep + ldlibdir + os.pathsep pathdir_name = 'in_path' pathdir = os.path.join(self.tmpdir, pathdir_name) os.mkdir(pathdir) os.environ['PATH'] = os.pathsep + pathdir + os.pathsep libExt = _libExt[_system()][0] f_in_cwd_ldlib_path = 'f_in_cwd_ldlib_path' open(os.path.join(self.tmpdir, f_in_cwd_ldlib_path), 'w').close() open(os.path.join(ldlibdir, f_in_cwd_ldlib_path), 'w').close() open(os.path.join(pathdir, f_in_cwd_ldlib_path), 'w').close() f_in_ldlib_extension = 'f_in_ldlib_extension' open(os.path.join(ldlibdir, f_in_ldlib_extension + libExt), 'w').close() f_in_path = 'f_in_path' open(os.path.join(pathdir, f_in_path), 'w').close() f_in_configlib = 'f_in_configlib' open(os.path.join(config_libdir, f_in_configlib), 'w').close() f_in_configbin = 'f_in_configbin' open(os.path.join(config_bindir, f_in_ldlib_extension), 'w').close() open(os.path.join(config_bindir, f_in_configbin), 'w').close() self._check_file(find_library(f_in_cwd_ldlib_path), os.path.join(self.tmpdir, f_in_cwd_ldlib_path)) self._check_file(os.path.join(ldlibdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False)) self._check_file( os.path.join(ldlibdir, f_in_ldlib_extension) + libExt, find_library(f_in_ldlib_extension)) self._check_file(os.path.join(pathdir, f_in_path), find_library(f_in_path)) if _system() == 'windows': self._check_file(os.path.join(pathdir, f_in_path), find_library(f_in_path, include_PATH=False)) else: # Note that on Windows, ctypes.util.find_library *always* # searches the PATH self.assertIsNone(find_library(f_in_path, include_PATH=False)) self._check_file( os.path.join(pathdir, f_in_path), find_library(f_in_path, pathlist=os.pathsep + pathdir + os.pathsep)) # test an explicit pathlist overrides LD_LIBRARY_PATH self._check_file( os.path.join(pathdir, f_in_cwd_ldlib_path), find_library(f_in_cwd_ldlib_path, cwd=False, pathlist=[pathdir])) # test that the PYOMO_CONFIG_DIR 'lib' dir is included self._check_file(os.path.join(config_libdir, f_in_configlib), find_library(f_in_configlib)) # and the Bin dir self._check_file(os.path.join(config_bindir, f_in_configbin), find_library(f_in_configbin)) # ... but only if include_PATH is true self.assertIsNone(find_library(f_in_configbin, include_PATH=False)) # And none of them if the pathlist is specified self.assertIsNone(find_library(f_in_configlib, pathlist=pathdir)) self.assertIsNone(find_library(f_in_configbin, pathlist=pathdir))
def find_GSL(): # FIXME: the GSL interface is currently broken in PyPy: if platform.python_implementation().lower().startswith('pypy'): return None return find_library('amplgsl.dll')