Skip to content

Instantly share code, notes, and snippets.

@ndevenish
Created August 1, 2017 22:24
Show Gist options
  • Save ndevenish/f98535eb55c9d910d0d905a73598c289 to your computer and use it in GitHub Desktop.
Save ndevenish/f98535eb55c9d910d0d905a73598c289 to your computer and use it in GitHub Desktop.
Read a libtbx env_config file into a dictionary for serialization
#!/usr/bin/env python
import pickle
from pprint import pprint
import yaml
from libtbx.env_config import environment, module, build_options
from libtbx.path import relocatable_path, absolute_path
def dict_from_module(m, include_name=True):
attrs = {"extra_command_line_locations", "mate_suffix", "required_for_build",
"exclude_from_binary_bundle", "names", "optional", "required_for_use"
}
if include_name:
attrs.add("name")
d = {key: getattr(m, key) for key in attrs if hasattr(m, key)}
# We don't know what form this takes
assert not m.python_paths, "Python_paths, but no example of what this is: Check this"
dist_paths = []
for x in m.dist_paths:
# Some entries are None - no idea how important this is
if x is not None:
dist_paths.append(x.relocatable)
else:
dist_paths.append(None)
if dist_paths:
d["dist_paths"] = dist_paths
return d
def dict_from_build_options(b):
attrs = {"compiler", "mode", "warning_level", "precompile_headers",
"static_libraries", "static_exe", "scan_boost", "write_full_flex_fwd_h",
"build_boost_python_extensions", "boost_python_no_py_signatures",
"boost_python_bool_int_strict", "enable_openmp_if_possible",
"enable_boost_threads", "enable_cuda", "opt_resources", "use_environment_flags",
"enable_cxx11", "old_division", "env_cxxflags", "env_cflags", "env_cppflags",
"env_ldflags"}
return {key: getattr(b, key) for key in attrs if hasattr(b, key)}
def dict_from_env(env):
d = {
"build_path": abs(env.build_path),
"python_exe": env.python_exe.relocatable,
"explicitly_requested_modules": list(env.explicitly_requested_modules),
"repository_paths": [x.relocatable for x in env.repository_paths],
"scons_dist_path": env.scons_dist_path.relocatable,
"build_options": dict_from_build_options(env.build_options),
"missing_optional": list(env.missing_optional),
"missing_for_use": list(env.missing_for_use),
"missing_for_build": list(env.missing_for_build),
"modules": {x.name: dict_from_module(x, include_name=False) for x in env.module_list},
}
return d
def load_from_dict(d):
env = environment(d["build_path"])
env.python_exe = relocatable_path(env.build_path, d["python_exe"])
env.explicitly_requested_modules = set(d["explicitly_requested_modules"])
env.repository_paths = [relocatable_path(env.build_path, x) for x in d["repository_paths"]]
env.scons_dist_path = relocatable_path(env.build_path, d["scons_dist_path"])
env.build_options = load_build_options_from_dict(d["build_options"])
env.missing_optional = set(d.get("missing_optional", []))
env.missing_for_use = set(d.get("missing_for_use", []))
env.missing_for_build = set(d.get("missing_for_build", []))
modules = [load_module_from_dict(name, d, env) for (name, d) in d.get("modules", []).items()]
env.module_list = modules
env.module_dict = {m.name: m for m in modules}
env.module_dist_paths = {m.name: _get_dist_path(m) for m in modules}
def load_build_options_from_dict(d):
b = build_options(
compiler=d.get("compiler", "default"),
mode=d.get("mode", "release"),
warning_level=d.get("warning_level", 0),
static_exe=d.get("static_exe", False),
scan_boost=d.get("scan_boost", False))
for key, val in d.items():
setattr(b, key, value)
return b
def _get_dist_path(mod):
for p in mod.dist_paths:
if p is not None:
return p
def load_module_from_dict(name, d, env):
m = module(env, name)
attrs = {"extra_command_line_locations", "mate_suffix", "required_for_build",
"exclude_from_binary_bundle", "names", "optional", "required_for_use"
}
for name in attrs:
setattr(m, name, d.get(val))
if "name" in attrs:
assert attrs["name"] == name, "name mismatch"
# Since we don't know what this is used for...
env.python_paths = []
dist_paths = []
for path in d["dist"]:
if path is None:
dist_paths.append(None)
else:
dist_paths.append(relocatable_path(env.build_path, path))
m.dist_paths = dist_paths
env = pickle.load(open("libtbx_env"))
print(yaml.dump(dict_from_env(env)))
# pprint(dict_from_env(env))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment