Skip to content

yggdrasil.path._module_pack

_module_pack

Module → .zip archive packaging shared by :class:Path callers.

A single helper, :func:build_module_archive, mirrors what CommandExecution._zip_local_module has been doing for cluster command uploads: walk a local package directory, drop the standard junk (__pycache__, .dist-info, .egg-info, .DS_Store), and emit a deflated .zip whose top-level entry IS the package directory (so the archive can be added to sys.path directly).

Lives under :mod:yggdrasil.io.path rather than the legacy databricks.compute so the abstract :class:Path upload/import hooks — and the Spark Connect builder — can reuse it without pulling the cluster-command code in.

ModuleSpec module-attribute

ModuleSpec = Union[str, PathLike, Callable]

Anything :func:resolve_module_root accepts.

  • str — importable module name ("yggdrasil.io") OR a filesystem path string. Path-shaped strings (containing separators / pointing at a real file or directory) are taken as paths; the rest are resolved as module names.
  • :class:os.PathLike — concrete filesystem path.
  • callable — uses obj.__module__.

resolve_module_root

resolve_module_root(obj: ModuleSpec) -> _LocalPath

Resolve obj to a concrete local path (file or directory).

Strings that are valid filesystem paths win: "./mypkg" and "/abs/path/to/pkg" resolve as-is. Anything else is treated as an importable module name and routed through :meth:PyEnv.get_root_module_directory.

Raises :class:FileNotFoundError when the resolved path does not exist on disk, and :class:ValueError when obj has no usable module / path information.

build_module_archive

build_module_archive(
    obj: ModuleSpec, dest: Union[str, PathLike, None] = None
) -> _LocalPath

Zip a local module / package into a deflated .zip archive.

Resolves obj via :func:resolve_module_root, then walks the directory (skipping __pycache__ / .dist-info / .egg-info / .DS_Store) and emits a deflated zip. When the resolved root is already a single file (e.g. an existing .whl or .zip) the file is copied verbatim.

dest picks the output location:

  • None — write to a fresh LocalPath.staging_path() so the file gets unlinked when the caller closes its holder.
  • directory — write <dir>/<module>.zip inside it.
  • file ending in .zip / .whl — write there exactly.

Returns the local :class:pathlib.Path of the produced archive.