Skip to content

Commit

Permalink
spython: add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Dec 5, 2023
1 parent 8c8a15f commit 8a54f16
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 9 deletions.
14 changes: 6 additions & 8 deletions cwltool/singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import sys
from subprocess import check_call, check_output # nosec
from typing import Callable, Dict, List, MutableMapping, Optional, Tuple, cast
from spython.main.parse.parsers import DockerParser # type: ignore
from spython.main.parse.writers import get_writer # type: ignore
from spython.main import Client # type: ignore

from schema_salad.sourceline import SourceLine
from spython.main import Client
from spython.main.parse.parsers.docker import DockerParser
from spython.main.parse.writers.singularity import SingularityWriter

from .builder import Builder
from .context import RuntimeContext
Expand Down Expand Up @@ -180,12 +181,9 @@ def get_image(
with open(dockerfile_path, "w") as dfile:
dfile.write(dockerRequirement["dockerFile"])

Check warning on line 182 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L182

Added line #L182 was not covered by tests

parser = DockerParser(dockerfile_path)
SingularityWriter = get_writer("singularity")
writer = SingularityWriter(parser.recipe)
result = writer.convert()
singularityfile = SingularityWriter(DockerParser(dockerfile_path).parse()).convert()

Check warning on line 184 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L184

Added line #L184 was not covered by tests
with open(singularityfile_path, "w") as file:
file.write(result)
file.write(singularityfile)

Check warning on line 186 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L186

Added line #L186 was not covered by tests

os.environ["APPTAINER_TMPDIR"] = absolute_path
Client.build(recipe=singularityfile_path, build_folder=absolute_path, sudo=False)
Expand Down
9 changes: 9 additions & 0 deletions mypy-stubs/spython/main/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Iterator, Optional

from .base import Client as _BaseClient
from .build import build as base_build

class _Client(_BaseClient):
build = base_build

Client = _Client()
23 changes: 23 additions & 0 deletions mypy-stubs/spython/main/build.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Iterator, Optional

from .base import Client

def build(
self: Client,
recipe: Optional[str] = ...,
image: Optional[str] = ...,
isolated: Optional[bool] = ...,
sandbox: Optional[bool] = ...,
writable: Optional[bool] = ...,
build_folder: Optional[str] = ...,
robot_name: Optional[bool] = ...,
ext: Optional[str] = ...,
sudo: Optional[bool] = ...,
stream: Optional[bool] = ...,
force: Optional[bool] = ...,
options: Optional[list[str]] | None = ...,
quiet: Optional[bool] = ...,
return_result: Optional[bool] = ...,
sudo_options: Optional[str | list[str]] = ...,
singularity_options: Optional[list[str]] = ...,
) -> tuple[str, Iterator[str]]: ...
14 changes: 14 additions & 0 deletions mypy-stubs/spython/main/parse/parsers/base.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import abc

from ..recipe import Recipe

class ParserBase(metaclass=abc.ABCMeta):
filename: str
lines: list[str]
args: dict[str, str]
active_layer: str
active_layer_num: int
recipe: dict[str, Recipe]
def __init__(self, filename: str, load: bool = ...) -> None: ...
@abc.abstractmethod
def parse(self) -> dict[str, Recipe]: ...
7 changes: 7 additions & 0 deletions mypy-stubs/spython/main/parse/parsers/docker.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ..recipe import Recipe
from .base import ParserBase as ParserBase

class DockerParser(ParserBase):
name: str
def __init__(self, filename: str = ..., load: bool = ...) -> None: ...
def parse(self) -> dict[str, Recipe]: ...
19 changes: 19 additions & 0 deletions mypy-stubs/spython/main/parse/recipe.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Optional

class Recipe:
cmd: Optional[str]
comments: list[str]
entrypoint: Optional[str]
environ: list[str]
files: list[str]
layer_files: dict[str, str]
install: list[str]
labels: list[str]
ports: list[str]
test: Optional[str]
volumes: list[str]
workdir: Optional[str]
layer: int
fromHeader: Optional[str]
source: Optional[Recipe]
def __init__(self, recipe: Optional[Recipe] = ..., layer: int = ...) -> None: ...
6 changes: 6 additions & 0 deletions mypy-stubs/spython/main/parse/writers/base.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from ..recipe import Recipe

class WriterBase:
recipe: dict[str, Recipe]
def __init__(self, recipe: dict[str, Recipe] | None = ...) -> None: ...
def write(self, output_file: str | None = ..., force: bool = ...) -> None: ...
10 changes: 10 additions & 0 deletions mypy-stubs/spython/main/parse/writers/singularity.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Optional

from ..recipe import Recipe
from .base import WriterBase as WriterBase

class SingularityWriter(WriterBase):
name: str
def __init__(self, recipe: Optional[dict[str, Recipe]] = ...) -> None: ...
def validate(self) -> None: ...
def convert(self, runscript: str = ..., force: bool = ...) -> str: ...
2 changes: 1 addition & 1 deletion tests/test_tmpdir.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test that all temporary directories respect the --tmpdir-prefix and --tmp-outdir-prefix options."""
import os
import re
import shutil
import os
import subprocess
import sys
from pathlib import Path
Expand Down

0 comments on commit 8a54f16

Please sign in to comment.