Commit 7d6147fd authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

working to make serviceregistry more relocatable (#32)


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 87c0cde1
......@@ -16,6 +16,7 @@ import sys
import os
import subprocess
import multiprocessing
import platform
try:
import pyxacc as xacc
......@@ -27,9 +28,11 @@ def parse_args(args):
parser = argparse.ArgumentParser(description="XACC Framework Utility.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
fromfile_prefix_chars='@')
parser.add_argument("-p", "--plugins", nargs='*', type=str, help="The XACC Plugins to install.", required=True)
parser.add_argument("-p", "--plugins", nargs='*', type=str, help="The XACC Plugins to install.", required=False)
parser.add_argument("-a", "--extra-cmake-args", nargs='*', type=str, help="Any extra CMake arguments to drive the install.", required=False)
parser.add_argument("-j", "--make-threads", default=4, type=int, help="Number of threads to use in make", required=False)
parser.add_argument("-i", "--initialize", action='store_true', help="Initialize the framework.", required=False)
opts = parser.parse_args(args)
return opts
......@@ -63,15 +66,63 @@ def mkdir_p(path):
print("")
exit()
def initialize():
try:
file = open(os.environ['HOME']+'/.xacc/.internal_plugins', 'r')
contents = file.read()
except IOError:
file = open(os.environ['HOME']+'/.xacc/.internal_plugins', 'w')
contents = ''
file.close()
file = open(os.environ['HOME']+'/.xacc/.internal_plugins', 'w')
xaccLocation = os.path.dirname(os.path.realpath(xacc.__file__))
if platform.system() == "Darwin":
libname1 = "libxacc-quantum-gate.dylib"
libname2 = "libxacc-quantum-aqc.dylib"
else:
libname1 = "libxacc-quantum-gate.so"
libname2 = "libxacc-quantum-aqc.so"
if xaccLocation+'/lib/'+libname1+'\n' not in contents:
file.write(xaccLocation+'/lib/'+libname1+'\n')
if xaccLocation+'/lib/'+libname2+'\n' not in contents:
file.write(xaccLocation+'/lib/'+libname2+'\n')
file.write(contents)
file.close()
def main(argv=None):
opts = parse_args(sys.argv[1:])
if opts.initialize:
initialize()
sys.exit(0)
# pyxacc can be installed in site-packages/xacc-...-egg or
# in ${XACC_DIR}/lib/python, we need to figure out which one
xaccLocation = os.path.dirname(os.path.realpath(xacc.__file__))
if os.path.basename(xaccLocation) == 'python':
xaccLocation = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
print('XACC Install Location = ', xaccLocation)
xacc_cwd = os.getcwd()
if xaccLocation+'/lib\n' not in contents:
file.write(xaccLocation+'/lib\n')
file.write(contents)
file.close()
def main(argv=None):
opts = parse_args(sys.argv[1:])
if opts.initialize:
initialize()
sys.exit(0)
# pyxacc can be installed in site-packages/xacc-...-egg or
# in ${XACC_DIR}/lib/python, we need to figure out which one
xaccLocation = os.path.dirname(os.path.realpath(xacc.__file__))
print('XACC Install Location = ', xaccLocation)
xacc_cwd = os.getcwd()
......
......@@ -19,60 +19,73 @@ void ServiceRegistry::initialize() {
"Invalid XACC Framework plugin context.");
}
// Get the Library path
std::string xaccLibDir = "";
#ifdef XACC_HAS_PYTHON
namespace py = pybind11;
auto getLibraryDir = []() -> std::string {
auto locals = py::dict();
py::exec(R"(
import os
imported = True
try:
import pyxacc as xacc
except ImportError:
imported = False
if imported:
xaccLocation = os.path.dirname(os.path.realpath(xacc.__file__))
)", py::globals(), locals);
bool foundPythonInstall = locals["imported"].cast<bool>();
if (foundPythonInstall) {
return locals["xaccLocation"].cast<std::string>() + std::string("/lib");
} else {
XACCLogger::instance()->warning("ServiceRegistry Warning, could not find pyxacc install");
return std::string(XACC_INSTALL_DIR) + std::string("/lib");;
}
};
if(!Py_IsInitialized()) {
py::scoped_interpreter guard{};
xaccLibDir = getLibraryDir();
} else {
xaccLibDir = getLibraryDir();
// Get the paths/files we'll be searching
const std::string xaccLibDir = std::string(XACC_INSTALL_DIR) + std::string("/lib");
const std::string xaccPluginPath = std::getenv("HOME")
+ std::string("/.xacc/plugins");
std::stringstream s;
s << std::getenv("HOME") << "/.xacc/.internal_plugins";
const std::string internalFileStr = s.str();
// Search in the .internal_plugins file first
if (boost::filesystem::exists(internalFileStr)) {
std::ifstream internalFile(internalFileStr);
std::string contents((std::istreambuf_iterator<char>(internalFile)), std::istreambuf_iterator<char>());
std::vector<std::string> split;
boost::split(split, contents, boost::is_any_of("\n"));
for (auto& s : split) {
if (!boost::contains(s, "#") && boost::filesystem::exists(s)) {
try {
context.InstallBundles(s);
boost::filesystem::path p(s);
auto name = p.filename().string();
boost::replace_all(name, "lib", "");
boost::replace_all(name, ".so", "");
boost::replace_all(name, ".dy", "");
installed.insert({name, p.string()});
XACCLogger::instance()->enqueueLog(
"Installed base plugin " + name);
} catch(std::exception& e) {
// do nothing if we failed
}
}
}
}
#else
xaccLibDir = std::string(XACC_INSTALL_DIR) + std::string("/lib");
#endif
XACCLogger::instance()->enqueueLog(
"XACC Library Directory: " + xaccLibDir);
for (auto &entry : boost::filesystem::directory_iterator(xaccLibDir)) {
// We want the gate and aqc bundles that come with XACC
if (boost::contains(entry.path().filename().string(),
"libxacc-quantum")) {
auto name = entry.path().filename().string();
boost::replace_all(name, "lib", "");
boost::replace_all(name, ".so", "");
boost::replace_all(name, ".dy", "");
// If not found, see if we have access to the installation directory
if (!installed.count("xacc-quantum-gate")
|| !installed.count("xacc-quantum-aqc")) {
if (boost::filesystem::exists(xaccLibDir)) {
XACCLogger::instance()->enqueueLog(
"Installing base plugin " + name);
context.InstallBundles(entry.path().string());
"Searching XACC Library Directory: " + xaccLibDir);
for (auto &entry : boost::filesystem::directory_iterator(
xaccLibDir)) {
// We want the gate and aqc bundles that come with XACC
if (boost::contains(entry.path().filename().string(),
"libxacc-quantum")) {
auto name = entry.path().filename().string();
boost::replace_all(name, "lib", "");
boost::replace_all(name, ".so", "");
boost::replace_all(name, ".dy", "");
if (installed.find(name) == installed.end()) {
context.InstallBundles(entry.path().string());
XACCLogger::instance()->enqueueLog(
"Installed base plugin " + name);
installed.insert({name, entry.path().string()});
}
}
}
}
}
const std::string xaccPluginPath = std::getenv("HOME")
+ std::string("/.xacc/plugins");
// If we still don't have them, throw an error
if (!installed.count("xacc-quantum-gate")
|| !installed.count("xacc-quantum-aqc")) {
throw std::runtime_error("Could not find required internal plugins.");
}
// Add external plugins...
boost::filesystem::directory_iterator end_itr;
......@@ -87,17 +100,18 @@ void ServiceRegistry::initialize() {
boost::replace_all(name, "lib", "");
boost::replace_all(name, ".so", "");
boost::replace_all(name, ".dy", "");
XACCLogger::instance()->enqueueLog(
"Installing Plugin " + name);
context.InstallBundles(subentry.path().string());
if (installed.find(name) == installed.end()) {
context.InstallBundles(subentry.path().string());
XACCLogger::instance()->enqueueLog(
"Installed Plugin " + name);
installed.insert(
{ name, subentry.path().string() });
}
}
}
}
} else {
XACCLogger::instance()->enqueueLog(
"There are no plugins. Install plugins to begin working with XACC.");
}
XACCLogger::instance()->enqueueLog(
......
......@@ -59,6 +59,8 @@ protected:
*/
BundleContext context;
std::map<std::string, std::string> installed;
/**
* The constructor
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment