Initial commit
This commit is contained in:
21
skills/detect-python-version/SKILL.md
Normal file
21
skills/detect-python-version/SKILL.md
Normal file
@@ -0,0 +1,21 @@
|
||||
---
|
||||
name: detect-python-version
|
||||
description: Detects the Python major and minor version of the current repository
|
||||
---
|
||||
|
||||
# detect-python-version
|
||||
|
||||
Runs a script that outputs the Python version in `major.minor` format (e.g., `3.11`, `3.13`).
|
||||
|
||||
## Instructions
|
||||
|
||||
Run `scripts/detect_version.py` to detect the Python version.
|
||||
|
||||
The script outputs just the version string in `major.minor` format, nothing else.
|
||||
|
||||
## Examples
|
||||
|
||||
**Output:** `3.11`
|
||||
|
||||
**Output:** `3.13`
|
||||
|
||||
67
skills/detect-python-version/scripts/detect_version.py
Executable file
67
skills/detect-python-version/scripts/detect_version.py
Executable file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def locate_git_repo(dir_cwd: Path) -> Path | None:
|
||||
"""
|
||||
Locate the git repository root directory by searching for the .git folder.
|
||||
"""
|
||||
for _ in range(10):
|
||||
if dir_cwd.joinpath(".git").exists():
|
||||
return dir_cwd
|
||||
dir_cwd = dir_cwd.parent
|
||||
return None
|
||||
|
||||
|
||||
def locate_pyproject_toml(dir_cwd: Path) -> Path | None:
|
||||
"""
|
||||
Locate the pyproject.toml file by searching upwards in the directory tree.
|
||||
"""
|
||||
for _ in range(10):
|
||||
if dir_cwd.joinpath("pyproject.toml").exists():
|
||||
return dir_cwd.joinpath("pyproject.toml")
|
||||
dir_cwd = dir_cwd.parent
|
||||
return None
|
||||
|
||||
|
||||
def locate_venv_bin_python(dir_cwd: Path) -> Path | None:
|
||||
"""
|
||||
Locate the virtual environment directory by searching for the .venv folder.
|
||||
"""
|
||||
for _ in range(10):
|
||||
path_venv_bin_python = dir_cwd.joinpath(".venv", "bin", "python")
|
||||
if path_venv_bin_python.exists():
|
||||
return path_venv_bin_python
|
||||
dir_cwd = dir_cwd.parent
|
||||
return None
|
||||
|
||||
|
||||
def get_python_version(path_venv_bin_python: Path) -> tuple[int, int, int]:
|
||||
"""
|
||||
Get the Python version of the specified python executable.
|
||||
"""
|
||||
args = [
|
||||
f"{path_venv_bin_python}",
|
||||
"--version",
|
||||
]
|
||||
res = subprocess.run(args, capture_output=True)
|
||||
text = res.stdout.decode("utf-8").strip()
|
||||
parts = text.split()[1].split(".")
|
||||
return int(parts[0]), int(parts[1]), int(parts[2])
|
||||
|
||||
|
||||
def main():
|
||||
dir_here = Path.cwd()
|
||||
path_venv_bin_python = locate_venv_bin_python(dir_here)
|
||||
if path_venv_bin_python is not None:
|
||||
major, minor, micro = get_python_version(path_venv_bin_python)
|
||||
py_ver = f"{major}.{minor}"
|
||||
print(py_ver)
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user