def restore_impl(the_clone): # Purge current environment pymod.mc.purge(load_after_purge=False) mp = the_clone.pop(pymod.names.modulepath, None) current_env = pymod.environ.copy(include_os=True) for (key, val) in current_env.items(): if key == pymod.names.modulepath: continue pymod.environ.unset(key) path = pymod.modulepath.Modulepath(split(mp, os.pathsep)) pymod.modulepath.set_path(path) # Make sure environment matches clone for (key, val) in the_clone.items(): pymod.environ.set(key, val) # Load modules to make sure aliases/functions are restored lm_cellar = pymod.environ._get_and_deserialize( the_clone, pymod.names.loaded_module_cellar) if lm_cellar: loaded_modules = [] for ar in lm_cellar: try: module = pymod.mc.unarchive_module(ar) except pymod.error.ModuleNotFoundError: raise pymod.error.CloneModuleNotFoundError( ar["fullname"], ar["filename"]) loaded_modules.append(module) pymod.mc.set_loaded_modules(loaded_modules) for module in loaded_modules: pymod.mc.load_partial(module)
def parse(self, filename): """Reads meta data for module in ``filename`` # pymod: [enable_if=<bool expr>] """ regex = re.compile(r"#\s*pymod\:") head = open(filename).readline() if not regex.search(head): return pymod_directive = split(regex.split(head, 1)[1], ",") kwds = dict([split(x, "=", 1) for x in pymod_directive]) for (key, default) in vars(self).items(): expr = kwds.pop(key, None) if expr is None: value = default else: value = eval_bool_expr(expr) if value is None: raise MetaDataValueError(expr, filename) setattr(self, key, value) if len(kwds): raise MetaDataUnknownFieldsError(list(kwds.keys()), filename)
def __init__(self, version_string=None): self.tuple = tuple() self.string = version_string or "" if version_string is not None: try: version_string, variant = version_string.split("-") except ValueError: variant = None parts = [try_int(part) for part in split(version_string, ".")] if variant is not None: parts.append(try_int(variant)) self.tuple = tuple(parts) for (i, attr) in enumerate(("major", "minor", "patch", "variant")): try: value = self.tuple[i] except IndexError: value = None setattr(self, attr, value)
def get_path(self, key, sep=os.pathsep): """Get the path given by `key` When paths are saved, the path value is saved and also some meta data. Currently, the saved meta data are the reference count and priority, though priority is not yet used. The meta data are saved in a dictionary in the environment variable `loaded_module_meta(key)` """ p = Namespace() p.key = self.fix_ld_library_path(key) p.meta_key = pymod.names.loaded_module_meta(key) p.sep = sep p.value = split(self.get(key), sep=sep) serialized = self.get(p.meta_key) p.meta = {} if serialized is None else deserialize(serialized) return p
def verify_config(self, data, scope): """Verify that the types match those of the default scope""" for (key, val) in data.items(): try: default = self.scopes["defaults"][key] except KeyError: msg = "Unknown user config var {0!r}".format(key) raise ValueError(msg) if scope == "environment": # Environment variables are always strings. if isinstance(default, list): val = split(val, sep=",") else: val = type(default)(val) data[key] = val elif type(default) != type(val): m = "User config var {0!r} must be of type {1!r}, not {2!r}" msg = m.format(key, type(default).__name__, type(val).__name__) raise ValueError(msg)
def get_path(key, sep=os.pathsep): return split(environ.get(key), sep=sep)
def init(parser, args): modulepath = split(args.modulepath, os.pathsep) pymod.mc.init(modulepath) pymod.mc.dump()
def factory(): # pragma: no cover path = split(os.getenv(pymod.names.modulepath), os.pathsep) return Modulepath(path)