def create_source_or_dep(modname, mod, dependencies, sources, experiment_path): if modname in MODULE_BLACKLIST or modname in dependencies: return filename = os.path.abspath(mod.__file__) if mod is not None else '' if filename and filename not in sources and \ is_subdir(filename, experiment_path): s = Source.create(filename) sources.add(s) elif mod is not None: pdep = PackageDependency.create(mod) if pdep.name.find('.') == -1 or pdep.version is not None: dependencies.add(pdep)
def is_local_source(filename, modname, experiment_path): if not is_subdir(filename, experiment_path): return False rel_path = os.path.relpath(filename, experiment_path) path_parts = get_relevant_path_parts(rel_path) mod_parts = modname.split('.') if path_parts == mod_parts: return True if len(path_parts) > len(mod_parts): return False abs_path_parts = get_relevant_path_parts(os.path.abspath(filename)) return all([p == m for p, m in zip(reversed(abs_path_parts), reversed(mod_parts))])
def is_local_source(filename, modname, experiment_path): """Check if a module comes from the given experiment path. Check if a module, given by name and filename, is from (a subdirectory of ) the given experiment path. This is used to determine if the module is a local source file, or rather a package dependency. Parameters ---------- filename: str The absolute filename of the module in question. (Usually module.__file__) modname: str The full name of the module including parent namespaces. experiment_path: str The base path of the experiment. Returns ------- bool: True if the module was imported locally from (a subdir of) the experiment_path, and False otherwise. """ if not is_subdir(filename, experiment_path): return False rel_path = os.path.relpath(filename, experiment_path) path_parts = convert_path_to_module_parts(rel_path) mod_parts = modname.split('.') if path_parts == mod_parts: return True if len(path_parts) > len(mod_parts): return False abs_path_parts = convert_path_to_module_parts(os.path.abspath(filename)) return all([ p == m for p, m in zip(reversed(abs_path_parts), reversed(mod_parts)) ])
def is_local_source(filename, modname, experiment_path): """Check if a module comes from the given experiment path. Check if a module, given by name and filename, is from (a subdirectory of ) the given experiment path. This is used to determine if the module is a local source file, or rather a package dependency. Parameters ---------- filename: str The absolute filename of the module in question. (Usually module.__file__) modname: str The full name of the module including parent namespaces. experiment_path: str The base path of the experiment. Returns ------- bool: True if the module was imported locally from (a subdir of) the experiment_path, and False otherwise. """ if not is_subdir(filename, experiment_path): return False rel_path = os.path.relpath(filename, experiment_path) path_parts = convert_path_to_module_parts(rel_path) mod_parts = modname.split('.') if path_parts == mod_parts: return True if len(path_parts) > len(mod_parts): return False abs_path_parts = convert_path_to_module_parts(os.path.abspath(filename)) return all([p == m for p, m in zip(reversed(abs_path_parts), reversed(mod_parts))])
def test_is_subdirectory(path, parent, expected): assert is_subdir(path, parent) == expected