$ show linkedin/shiv
shiv is a command-line utility for building fully self-contained Python zipapps (PEP 441) that include all dependencies. It uses pip to install packages into a temporary directory, then packages the site-packages directory along with a bootstrap script into a single executable .pyz file. At runtime, the bootstrap extracts the dependencies to a cache directory (default ~/.shiv) and adds them to sys.path before executing the specified entry point or dropping into an interactive interpreter. The architecture is a CLI tool built with Click, orchestrated by a main module that delegates to pip and a custom zip archive builder. Key components include a robust environment configuration system that supports build-time settings and runtime overrides via environment variables, a cross-platform file lock for safe extraction, and a clean bootstrap pipeline. The codebase is well-structured, with comprehensive tests and support for Python 3.8+. It is maintained by LinkedIn and is actively used for distributing Python applications.
The LLM identified 3 files worth highlighting for reuse:
The Environment class encapsulates build-time and runtime configuration for a shiv zipapp. It stores settings like entry point, build ID, and flags, and allows overriding most settings via environment variables (e.g., SHIV_ENTRY_POINT, SHIV_ROOT). It is serializable to/from JSON, enabling metadata to be embedded in the zipapp. This module is self-contained, demonstrates a clean pattern for layered configuration, and can be reused in any project that needs to manage settings with environment overrides.
A minimal, portable file lock implementation using OS-specific locking primitives (msvcrt on Windows, fcntl on Unix). It provides a context manager for acquiring and releasing a lock on a file, with a polling loop. It is a standalone module with no external dependencies and can be extracted and reused in any Python project requiring inter-process synchronization.
This module handles executing Python code in the bootstrapped environment, supporting execution from a file, stdin, or a module. It is used to launch an interactive interpreter or run scripts passed as arguments. It is a self-contained utility that demonstrates clean handling of different execution modes (compile, run_module, code.interact) and can be reused in other projects that need to execute Python code programmatically.