Skip to content

Commit

Permalink
reduce complexity of finding andinitializing dll
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Piotr Przeradowski committed Aug 10, 2013
1 parent 6b7de8a commit 2fe71b4
Showing 1 changed file with 101 additions and 81 deletions.
182 changes: 101 additions & 81 deletions pystacia/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,69 @@ def dll_template(osname, abi):
return None


def find_library(name, abis, environ=None, osname=None, factory=None):
logger.debug('Trying to find ImageMagick...')
def process_depends(depends_path, path, osname, factory):
depends = open(depends_path)

for line in depends:
depname, depabi = line.split()
template = formattable(dll_template(osname, depabi))
dll_path = join(path, template.format(name=depname,
abi=depabi))
try:
factory(dll_path)
except:
pass

paths = []
depends.close()

if not environ:
environ = os.environ

def find_in_path(path, name, abis, osname, factory):
depends_path = join(path, 'depends.txt')
if exists(depends_path):
process_depends(depends_path, path, osname, factory)

for abi in abis:
template = dll_template(osname, abi)
if not template:
continue

template = formattable(template)

dll_path = join(path, template.format(name=name, abi=abi))

logger.debug('Trying: ' + dll_path)

if exists(dll_path):
logger.debug('Found: ' + dll_path)

if osname == 'windows':
old_path = getcwd()
chdir(path)

try:
factory(dll_path)
except:
from sys import exc_info
msg = formattable('Caught exception while loading '
'{0}: {1}. Rolling back')
logger.debug(msg.format(dll_path, exc_info()[1]))

if osname == 'windows':
chdir(old_path)
else:
if osname == 'windows':
chdir(old_path)

return dll_path


def gather_paths(environ):
paths = []

path = registry.get('library_path', environ.get('PYSTACIA_LIBRARY_PATH'))
if path:
paths.append(path)

if not osname:
osname = get_osname()

if not registry.get('skip_package', environ.get('PYSTACIA_SKIP_PACKAGE')):
import pystacia
path = dirname(pystacia.__file__)
Expand All @@ -64,65 +112,33 @@ def find_library(name, abis, environ=None, osname=None, factory=None):
if not registry.get('skip_cwd', environ.get('PYSTACIA_SKIP_CWD')):
paths.append(getcwd())

return paths


def find_library(name, abis, environ=None, osname=None, factory=None):
logger.debug('Trying to find ImageMagick...')

if not environ:
environ = os.environ

if not factory:
factory = CDLL

if not osname:
osname = get_osname()

paths = gather_paths(environ)

logger.debug('Following paths will be searched: ' + ';'.join(paths))

for path in paths:
if not exists(path):
logger.debug('Path does not exist: ' + path)
continue

depends_path = join(path, 'depends.txt')
if exists(depends_path):
depends = open(depends_path)

for line in depends:
depname, depabi = line.split()
template = formattable(dll_template(osname, depabi))
dll_path = join(path, template.format(name=depname,
abi=depabi))
try:
factory(dll_path)
except:
pass

depends.close()

for abi in abis:
template = dll_template(osname, abi)
if not template:
continue

template = formattable(template)

dll_path = join(path, template.format(name=name, abi=abi))

logger.debug('Trying: ' + dll_path)

if exists(dll_path):
logger.debug('Found: ' + dll_path)

if osname == 'windows':
old_path = getcwd()
chdir(path)

try:
factory(dll_path)
except:
from sys import exc_info
msg = formattable('Caught exception while loading '
'{0}: {1}. Rolling back')
logger.debug(msg.format(dll_path, exc_info()[1]))

if osname == 'windows':
chdir(old_path)
else:
if osname == 'windows':
chdir(old_path)

return dll_path
dll_path = find_in_path(path, name, abis, osname, factory)
if dll_path:
return dll_path

# still nothing? let ctypes figure it out
if not registry.get('skip_system', environ.get('PYSTACIA_SKIP_SYSTEM')):
Expand Down Expand Up @@ -152,6 +168,33 @@ def rollback(self):
__lock = Lock()


def init_dll(dll):
def shutdown():
logger.debug('Cleaning up traced instances')
_cleanup()

c_call(None, 'terminus')

if jython:
from java.lang import System # @UnresolvedImport
System.exit(0)

logger.debug('Critical section - init MagickWand')
with __lock:
if not dll.__inited:
c_call(None, 'genesis', __init=False)

logger.debug('Registering atexit handler')
atexit.register(shutdown)

dll.__inited = True

version = magick.get_version()
if version < min_version:
msg = formattable('Unsupported version of MagickWand {0}')
warn(msg.format(version))


def get_dll(init=True, environ=None, isolated=False):
"""Find ImageMagick DLL and initialize it.
Expand Down Expand Up @@ -184,30 +227,7 @@ def get_dll(init=True, environ=None, isolated=False):
dll = get_dll.__dll

if init and not dll.__inited:
def shutdown():
logger.debug('Cleaning up traced instances')
_cleanup()

c_call(None, 'terminus')

if jython:
from java.lang import System # @UnresolvedImport
System.exit(0)

logger.debug('Critical section - init MagickWand')
with __lock:
if not dll.__inited:
c_call(None, 'genesis', __init=False)

logger.debug('Registering atexit handler')
atexit.register(shutdown)

dll.__inited = True

version = magick.get_version()
if version < min_version:
msg = formattable('Unsupported version of MagickWand {0}')
warn(msg.format(version))
init_dll(dll)

return dll

Expand Down

0 comments on commit 2fe71b4

Please sign in to comment.