Codeowners Parser
A Python library for parsing and working with CODEOWNERS files, typically used in Git repositories to define ownership for different parts of the codebase. It provides methods to identify the owners of specific file paths based on the rules defined in a CODEOWNERS file. The current version is 0.8.0. Releases are infrequent, usually for minor bug fixes or small feature additions.
Warnings
- breaking Version 0.2.0 significantly changed the library's implementation by removing the underlying Rust bindings and switching to a pure Python parser.
- gotcha The `Codeowners` constructor searches for CODEOWNERS files in specific default locations (`.github/CODEOWNERS`, `CODEOWNERS` in root, `docs/CODEOWNERS`) if no explicit path is provided.
- gotcha The library strictly follows the CODEOWNERS specification's rule that the last matching pattern in the file takes precedence.
Install
-
pip install codeowners
Imports
- Codeowners
from codeowners import Codeowners
- CodeownersError
from codeowners.exceptions import CodeownersError
Quickstart
from codeowners import Codeowners
# Create a dummy CODEOWNERS file for demonstration
with open('.github/CODEOWNERS', 'w') as f:
f.write("*.py @python-devs\n/src/docs/* @docs-team\n/src/app/core/ @core-maintainers")
# Initialize the parser with the path to the CODEOWNERS file
# Note: The library searches default paths if not explicitly given,
# but providing the path is best practice.
co = Codeowners('.github/CODEOWNERS')
# Get owners for a specific file path
owners_py = co.of('src/app/utils.py')
print(f"Owners for src/app/utils.py: {owners_py}")
owners_doc = co.of('src/docs/guide.md')
print(f"Owners for src/docs/guide.md: {owners_doc}")
owners_core = co.of('src/app/core/models.py')
print(f"Owners for src/app/core/models.py: {owners_core}")
# Clean up the dummy file
import os
os.remove('.github/CODEOWNERS')