-
Notifications
You must be signed in to change notification settings - Fork 9
/
load_ca.py
113 lines (99 loc) · 4.01 KB
/
load_ca.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This file is part of the Diamond cothread library.
#
# Copyright (C) 2007 James Rowland, 2007-2012 Michael Abbott,
# Diamond Light Source Ltd.
#
# The Diamond cothread library is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# The Diamond cothread library is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Contact:
# Dr. Michael Abbott,
# Diamond Light Source Ltd,
# Diamond House,
# Chilton,
# Didcot,
# Oxfordshire,
# OX11 0DE
# michael.abbott@diamond.ac.uk
# As discovering the location of libca (or whatever it's called in this
# particular system) is somewhat involved the work is gathered into this file.
# This file can also be run as a standalone script to discover the path to
# libca.
from __future__ import print_function
import ctypes
import platform
import os
from epicsarch import epics_host_arch
# Figure out the libraries that need to be loaded and the loading method.
load_library = ctypes.cdll.LoadLibrary
system = platform.system()
if system == 'Windows':
load_library = ctypes.windll.LoadLibrary
lib_files = ['Com.dll', 'ca.dll']
elif system == 'Darwin':
lib_files = ['libca.dylib']
else:
lib_files = ['libca.so']
def _libca_path(load_libca_path):
# We look for libca in a variety of different places, searched in order:
#
# 1. Firstly if CATOOLS_LIBCA_PATH is set in the environment we take that as
# gospel. This allows the remaining search to be overridden.
# 2 If the libca_path module is present we accept the value it defines.
# 3. Check for local copies of the libca file(s).
# 4. Finally check for EPICS_BASE and compute appropriate architecture
# First allow a forced override
libca_path = os.environ.get('CATOOLS_LIBCA_PATH')
if libca_path:
return libca_path
# Next import from configuration file if present, unless this has been
# disabled.
if load_libca_path:
try:
# If libca_path has been defined go with that
from .libca_path import libca_path
return libca_path
except ImportError:
pass
# If no libca_path, how about local copies of the files?
libca_path = os.path.abspath(os.path.dirname(__file__))
if os.path.isfile(os.path.join(libca_path, lib_files[-1])):
# Yes, there seems to be something locally installed.
return libca_path
# No local install, no local configuration, no override. Try for standard
# environment variable configuration instead.
epics_base = os.environ['EPICS_BASE']
return os.path.join(epics_base, 'lib', epics_host_arch)
if __name__ == '__main__':
# If run standalone we are a helper script. Write out the relevant
# definitions for the use of our caller.
libca_path = _libca_path(False)
print('CATOOLS_LIBCA_PATH=\'%s\'' % libca_path)
print('LIB_FILES=\'%s\'' % ' '.join(lib_files))
else:
# Load the library (or libraries).
try:
# First try loading the libraries directly without searching anywhere.
# In this case we'll pick up from the path or anything already loaded
# into the interpreter.
for lib in lib_files:
libca = load_library(lib)
except OSError:
# Ask _libca_path() where to find things.
libca_path = _libca_path(True)
if os.path.isfile(libca_path):
libca = load_library(libca_path)
else:
for lib in lib_files:
libca = load_library(os.path.join(libca_path, lib))