hashin: Requirements.txt Hashing Tool
hashin is a command-line tool that automates the process of adding hashes (e.g., SHA256) to entries in a `requirements.txt` file, improving supply chain security by ensuring integrity. It is currently at version 1.0.5 and maintains an active release cadence, with recent updates focusing on Python version compatibility and internal dependency management.
Common errors
-
hashin: command not found
cause `hashin` is not installed or its installation directory is not in your system's PATH.fixEnsure `hashin` is installed by running `pip install hashin`. If it is installed, verify your system's PATH configuration or try running it via `python -m hashin`. -
ERROR: Command "hashin" requires Python >=3.9
cause You are attempting to run `hashin` using a Python interpreter version older than 3.9.fixSwitch to a Python 3.9+ environment (e.g., using `pyenv` or `conda`), or upgrade your default Python interpreter. For example, `python3.9 -m pip install hashin`. -
ERROR: No requirements file found at 'requirements.txt'
cause `hashin` was run without specifying a requirements file, and no file named `requirements.txt` exists in the current working directory.fixEither create a `requirements.txt` file in your current directory, or specify the path to your requirements file using the `-r` option: `hashin -r path/to/my_requirements.txt`.
Warnings
- breaking hashin 1.0.1 and later versions require Python 3.9 or newer. Older Python versions (e.g., 3.8) are no longer supported due to internal dependency updates.
- gotcha `hashin` modifies your `requirements.txt` file in-place. This is its intended behavior, but users should be aware of it and ideally run `hashin` within a version-controlled repository to easily revert changes if necessary.
- gotcha hashin requires a valid requirements file format. If package versions are malformed or unresolvable by pip, `hashin` may fail or produce unexpected results.
Install
-
pip install hashin
Quickstart
import subprocess
import os
# Create a dummy requirements.txt for demonstration
requirements_content = """
requests==2.31.0
click==8.1.7
"""
with open("requirements.txt", "w") as f:
f.write(requirements_content)
print("Original requirements.txt:")
with open("requirements.txt", "r") as f:
print(f.read())
try:
# Run hashin to add hashes to requirements.txt in place
# Using -r requirements.txt explicitly for clarity, though it's the default.
print("\nRunning: hashin -r requirements.txt\n")
result = subprocess.run(["hashin", "-r", "requirements.txt"], capture_output=True, text=True, check=True)
print("hashin output:")
print(result.stdout)
if result.stderr:
print("hashin stderr (if any):\n" + result.stderr)
print("\nUpdated requirements.txt:")
with open("requirements.txt", "r") as f:
print(f.read())
except subprocess.CalledProcessError as e:
print(f"Error running hashin: {e.stderr}")
except FileNotFoundError:
print("Error: 'hashin' command not found. Please ensure it's installed and in your PATH.")
finally:
# Clean up the dummy file
if os.path.exists("requirements.txt"):
os.remove("requirements.txt")