Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type checking #90

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/typing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Typing

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_call:

env:
UV_SYSTEM_PYTHON: 1

jobs:
type-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install dependencies
run: |
pip install -U pip
pip install .
pip install mypy
- name: Run mypy
run: |
mypy komodoenv
123 changes: 67 additions & 56 deletions komodoenv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@
from komodoenv.statfs import is_nfs


def get_release_maturity_text(release_path):
class KomodoenvNamespace(argparse.Namespace):
"""
Komodoenv argument parser namespace
"""

force: bool
release: str
track: str | None
no_update: bool
root: str
force_color: bool
destination: str


def get_release_maturity_text(release_path: Path) -> str:
"""Returns a comment informing the user about the maturity of the release that
they've chosen. Eg, warn users if they want bleeding, pat them on the back
if they want stable, etc.
Expand All @@ -40,7 +54,7 @@ def get_release_maturity_text(release_path):
)


def distro_suffix():
def distro_suffix() -> str:
# Workaround to make tests pass on Github Actions
if (
"GITHUB_ACTIONS" in os.environ
Expand All @@ -56,12 +70,10 @@ def distro_suffix():
def resolve_release( # noqa: C901
*,
root: Path,
name: str,
release_path: Path,
no_update: bool = False,
) -> tuple[Path, Path]:
"""Autodetect komodo release heuristically"""
if not (root / name / "enable").is_file():
sys.exit(f"'{root / name}' is not a valid komodo release")

env = os.environ.copy()
if "BASH_ENV" in env:
Expand All @@ -71,7 +83,7 @@ def resolve_release( # noqa: C901
[
"/bin/bash",
"-c",
f"source {root / name / 'enable'};which python;python --version",
f"source {release_path / 'enable'};which python;python --version",
],
env=env,
)
Expand All @@ -92,7 +104,7 @@ def resolve_release( # noqa: C901
major, minor = match.groups()
pyver = "-py" + major + minor

base_name = re.match("^(.*?)(?:-py[0-9]+)?(?:-rhel[0-9]+)?$", name)
base_name = re.match("^(.*?)(?:-py[0-9]+)?(?:-rhel[0-9]+)?$", release_path.name)
if base_name is None:
msg = "Could not find the release."
raise ValueError(msg)
Expand All @@ -105,7 +117,7 @@ def resolve_release( # noqa: C901
dir_ = root / (mode + pyver + rhver)
track = root / (mode + pyver)
if not (dir_ / "root").is_dir():
# stable-rhel7 isn't a thing. Try resolving and appending 'rhver'
# stable-rhel8 isn't a thing. Try resolving and appending 'rhver'
dir_ = (root / (mode + pyver)).resolve()
dir_ = dir_.parent / (dir_.name + distro_suffix())
if not (dir_ / "root").is_dir():
Expand All @@ -120,7 +132,7 @@ def resolve_release( # noqa: C901
)


def parse_args(args):
def parse_args(args: list[str]) -> KomodoenvNamespace:
ap = argparse.ArgumentParser()
ap.add_argument(
"-f",
Expand Down Expand Up @@ -163,42 +175,10 @@ def parse_args(args):
)
ap.add_argument("destination", type=str, help="Where to create komodoenv")

args = ap.parse_args(args)

args.root = Path(args.root)
if not args.root.is_dir():
msg = "The given root is not a directory."
raise ValueError(msg)

if "/" in args.release:
args.release = Path(args.release)
elif isinstance(args.release, str):
args.release = Path(args.root) / args.release

if not args.release or not args.track:
args.release, args.track = resolve_release(
root=args.root,
name=str(args.release),
no_update=args.no_update,
)
args.track = Path(args.track)
args.destination = Path(args.destination).absolute()

if args.release is None or not args.release.is_dir():
print(
"Could not automatically detect active Komodo release. "
"Either enable a Komodo release that supports komodoenv "
"or specify release manually with the "
"`--release' argument. ",
sys.stderr,
)
ap.print_help()
sys.exit(1)
return ap.parse_args(args, namespace=KomodoenvNamespace())

return args


def main(args=None):
def main() -> None:
texts = {
"info": blue(
"Info: "
Expand All @@ -212,32 +192,63 @@ def main(args=None):
),
}

if args is None:
args = sys.argv[1:]
args = parse_args(args)
args = parse_args(sys.argv[1:])

root = Path(args.root).resolve()
if not root.is_dir():
msg = f"The given root '{args.root}' is not a directory."
raise ValueError(msg)

release = root / args.release

if not (release / "enable").is_file():
msg = f"'{release !s}' is not a valid komodo release!"
raise ValueError(msg)

if not args.track:
release, track = resolve_release(
root=root,
release_path=release,
no_update=args.no_update,
)
else:
track = root / args.track
if not (release / "enable").is_file():
sys.exit(f"'{track !s}' is not a valid komodo release!")
destination = Path(args.destination).absolute()

if not release.is_dir():
print(
"Could not automatically detect active Komodo release. "
"Either enable a Komodo release that supports komodoenv "
"or specify release manually with the "
"`--release' argument. ",
sys.stderr,
)
sys.exit(1)

if args.destination.is_dir() and args.force:
rmtree(str(args.destination), ignore_errors=True)
elif args.destination.is_dir():
sys.exit(f"Destination directory already exists: {args.destination}")
if destination.is_dir() and args.force:
rmtree(str(destination), ignore_errors=True)
elif destination.is_dir():
sys.exit(f"Destination directory already exists: {destination !s}")

use_color = args.force_color or (sys.stdout.isatty() and sys.stderr.isatty())
release_text = get_release_maturity_text(args.track)
release_text = get_release_maturity_text(track)
if not use_color:
texts = {key: strip_color(val) for key, val in texts.items()}
release_text = strip_color(release_text)

print(texts["info"], file=sys.stderr)
print(release_text, file=sys.stderr)

if not is_nfs(args.destination):
if not is_nfs(destination):
print(texts["nfs"], file=sys.stderr)

creator = Creator(
komodo_root=args.root,
srcpath=args.release,
trackpath=args.track,
dstpath=args.destination,
komodo_root=root,
src_path=release,
track_path=track,
dst_path=destination,
use_color=use_color,
)
creator.create()
Expand Down
Loading
Loading