Skip to content
Open
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
36 changes: 34 additions & 2 deletions kernelci/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import kernelci.storage


class KContext:

Check warning on line 25 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Too many public methods (24/20)

Check warning on line 25 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Too many instance attributes (8/7)
"""KernelCI Context class for managing configuration and secrets

This class provides a unified interface for accessing configuration
Expand All @@ -37,6 +37,7 @@
config_paths: List of paths to configuration files/directories
secrets_path: Path to the secrets TOML file
runtimes: List of runtimes from CLI arguments
runtime_types: List of runtime types from CLI arguments
cli_args: Parsed CLI arguments if parse_cli=True was used
"""

Expand All @@ -61,8 +62,9 @@
if parse_cli:
args = self._parse_cli_args()

# Initialize runtime list
# Initialize runtime list and runtime types
self.runtimes = []

Check failure on line 66 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Need type annotation for "runtimes" (hint: "runtimes: List[<type>] = ...") [var-annotated]
self.runtime_types = []

Check failure on line 67 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Need type annotation for "runtime_types" (hint: "runtime_types: List[<type>] = ...") [var-annotated]
self.cli_args = args if parse_cli else None

# Extract values from CLI arguments if provided
Expand All @@ -77,6 +79,8 @@
program_name = getattr(args, "name", None) or program_name
# Handle --runtimes parameter
self.runtimes = getattr(args, "runtimes", [])
# Handle --runtime-type parameter
self.runtime_types = getattr(args, "runtime_type", []) or []

# Set default paths if not provided
if not config_paths:
Expand Down Expand Up @@ -108,7 +112,7 @@
self.config = self._load_config()
self.secrets = self._load_secrets()

def _parse_cli_args(self) -> argparse.Namespace:

Check warning on line 115 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Too many statements (63/50)

Check warning on line 115 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Too many branches (22/12)
"""Parse command line arguments directly from sys.argv

Returns:
Expand All @@ -117,12 +121,12 @@
# Create a copy of sys.argv to avoid modifying the original
argv = sys.argv[1:].copy() # Skip program name and make a copy
runtimes = []
runtime_types = []

# Find --runtimes and collect its values without modifying argv
i = 0
while i < len(argv):
if argv[i] == "--runtimes":
runtime_idx = i
i += 1
# Collect values until we hit another option or end
while i < len(argv):
Expand All @@ -141,6 +145,25 @@
break
i += 1

# Find --runtime-type and collect its values
i = 0
while i < len(argv):
if argv[i] == "--runtime-type":

Check warning on line 151 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Unnecessary "elif" after "break", remove the leading "el" from "elif"
i += 1
# Collect values until we hit another option or end
while i < len(argv):
if argv[i].startswith("--"):
break
# Valid runtime types: lava, kubernetes, docker, shell, pull_labs
runtime_types.append(argv[i])
i += 1
break
elif argv[i].startswith("--runtime-type="):
runtime_types.append(argv[i].split("=", 1)[1])
i += 1
else:
i += 1

# Create a custom namespace to store our values without consuming arguments
args = argparse.Namespace()

Expand All @@ -151,6 +174,7 @@
args.config = None
args.name = None
args.runtimes = runtimes
args.runtime_type = runtime_types

# Look for our specific arguments in argv
i = 0
Expand Down Expand Up @@ -193,7 +217,7 @@
if not self.config_paths:
return {}

config = {}

Check failure on line 220 in kernelci/context/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Need type annotation for "config" (hint: "config: Dict[<type>, <type>] = ...") [var-annotated]

for path in self.config_paths:
if not path or not os.path.exists(path):
Expand Down Expand Up @@ -417,6 +441,14 @@
"""
return self.runtimes

def get_runtime_types(self) -> List[str]:
"""Get list of runtime types from CLI arguments

Returns:
List of runtime types (e.g., 'lava', 'kubernetes', 'docker', 'shell')
"""
return self.runtime_types

def get_cli_args(self) -> Optional[argparse.Namespace]:
"""Get parsed CLI arguments if parse_cli was used

Expand Down