-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d005275
Showing
8 changed files
with
531 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Publish to PyPI | ||
|
||
"on": | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
environment: release | ||
permissions: | ||
id-token: write # Required for PyPI trusted publishing | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Install UV | ||
run: | | ||
curl -LsSf https://astral.sh/uv/install.sh | sh | ||
echo "$HOME/.cargo/bin" >> $GITHUB_PATH | ||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
|
||
- name: Install build dependencies | ||
run: | | ||
uv pip install build | ||
- name: Build package | ||
run: python -m build | ||
|
||
- name: Publish to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Test | ||
|
||
on: | ||
pull_request: | ||
branches: [ main ] | ||
push: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.9", "3.10", "3.11"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Install the latest version of uv | ||
uses: astral-sh/setup-uv@v5 | ||
with: | ||
version: "0.5.x" | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
run: | | ||
uv sync --all-extras --dev | ||
- name: Lint with ruff | ||
run: | | ||
uv run ruff check . | ||
- name: Run tests | ||
run: | | ||
uv run pytest tests/ -v --cov=gitwalk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# UV | ||
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
#uv.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control | ||
.pdm.toml | ||
.pdm-python | ||
.pdm-build/ | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ | ||
|
||
# PyPI configuration file | ||
.pypirc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# gitwalk | ||
|
||
A Python library that provides an os.walk()-compatible interface that respects .gitignore rules. Walk through your directories while automatically excluding paths matched by .gitignore patterns. | ||
|
||
## Installation | ||
|
||
Using uv: | ||
```bash | ||
uv pip install gitwalk | ||
``` | ||
|
||
## Usage | ||
|
||
```python | ||
from gitwalk import gitwalk as walk | ||
|
||
# Walk through directory respecting .gitignore rules | ||
for dirpath, dirnames, filenames in walk("./my_project"): | ||
print(f"Directory: {dirpath}") | ||
print(f"Subdirectories: {dirnames}") | ||
print(f"Files: {filenames}") | ||
``` | ||
|
||
## Features | ||
|
||
- Same interface as `os.walk()` | ||
- Respects `.gitignore` patterns | ||
- Supports both topdown and bottom-up traversal | ||
- Handles error callbacks | ||
- Follows symbolic links (optional) | ||
|
||
## Tests | ||
``` | ||
uv pip install -e ".[test]" | ||
pytest | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[project] | ||
name = "gitwalk" | ||
version = "0.1.0" | ||
description = "A directory walker that respects .gitignore rules" | ||
authors = [ | ||
{ name = "Avi Yeger", email = "yeger00@gmail.com" } | ||
] | ||
dependencies = [ | ||
"pathspec>=0.11.0", | ||
] | ||
requires-python = ">=3.7" | ||
readme = "README.md" | ||
license = { text = "MIT" } | ||
|
||
[project.optional-dependencies] | ||
test = [ | ||
"pytest>=7.0.0", | ||
"pytest-cov>=4.0.0", | ||
"ruff>=0.2.0", | ||
] | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .walker import gitwalk | ||
|
||
__version__ = "0.1.0" | ||
__all__ = ["gitwalk"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import Iterator, List, Literal, Tuple | ||
from pathspec import PathSpec | ||
from pathspec.patterns import GitWildMatchPattern | ||
|
||
def gitwalk(top: str, topdown: Literal[True] = True, onerror=None, followlinks: bool = False) -> Iterator[Tuple[str, List[str], List[str]]]: | ||
""" | ||
Walk a directory tree similar to os.walk(), but respecting .gitignore rules. | ||
Args: | ||
top: Starting directory path | ||
topdown: If True, do a topdown traversal; if False, do a bottom-up traversal | ||
onerror: Optional function to handle errors | ||
followlinks: If True, follow symbolic links | ||
Yields: | ||
A 3-tuple: (dirpath, dirnames, filenames) - same format as os.walk() | ||
""" | ||
if not topdown: | ||
raise ValueError("topdown must be True") | ||
|
||
def load_gitignore(directory: str) -> PathSpec: | ||
"""Load .gitignore patterns from the given directory and its parents.""" | ||
patterns = [] | ||
current_dir = Path(directory) | ||
|
||
while True: | ||
gitignore_path = current_dir / '.gitignore' | ||
if gitignore_path.is_file(): | ||
with open(gitignore_path, 'r') as f: | ||
patterns.extend(f.readlines()) | ||
|
||
if current_dir.parent == current_dir: | ||
break | ||
current_dir = current_dir.parent | ||
|
||
return PathSpec.from_lines(GitWildMatchPattern, patterns) | ||
|
||
try: | ||
walk_generator = os.walk(top, topdown, onerror, followlinks) | ||
|
||
for dirpath, dirnames, filenames in walk_generator: | ||
gitignore = load_gitignore(dirpath) | ||
rel_dirpath = os.path.relpath(dirpath, top) | ||
if rel_dirpath == '.': | ||
rel_dirpath = '' | ||
|
||
filtered_dirs = [] | ||
for dirname in dirnames: | ||
rel_path = os.path.join(rel_dirpath, dirname) if rel_dirpath else dirname | ||
if not gitignore.match_file(rel_path + '/'): | ||
filtered_dirs.append(dirname) | ||
|
||
filtered_files = [] | ||
for filename in filenames: | ||
rel_path = os.path.join(rel_dirpath, filename) if rel_dirpath else filename | ||
if not gitignore.match_file(rel_path): | ||
filtered_files.append(filename) | ||
|
||
dirnames[:] = filtered_dirs | ||
yield dirpath, filtered_dirs, filtered_files | ||
|
||
except Exception as e: | ||
if onerror is not None: | ||
onerror(e) | ||
else: | ||
raise |
Oops, something went wrong.