Skip to content

Commit

Permalink
Fixes for fprime-cli (#60)
Browse files Browse the repository at this point in the history
* refactor: move search functions to utils.py

* fix: add dictionary search to fprime-cli

* fix: missing _ in variable name
  • Loading branch information
codeflight1 committed Jun 16, 2022
1 parent 9edae8d commit 97f5492
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/fprime_gds/common/gds_cli/base_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def print_upcoming_item(min_start_time="NOW"):
cls._log(cls._get_item_string(item_, json))
# Update time so we catch the next item since the last one
if item_:
min_start_time = predicates.greater_than(item.get_time())
min_start_time = predicates.greater_than(item_.get_time())
min_start_time = filtering_utils.time_to_data_predicate(
min_start_time
)
Expand Down
7 changes: 7 additions & 0 deletions src/fprime_gds/executables/fprime_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from copy import deepcopy
import os
import sys
import platform
from typing import Callable, List, Union

import argcomplete
Expand All @@ -22,6 +23,7 @@
# import fprime_gds.common.gds_cli.events as events
# from fprime_gds.common.pipeline.dictionaries import Dictionaries
from fprime_gds.executables.cli import GdsParser
from fprime_gds.executables.utils import get_artifacts_root, find_dict


def add_connection_arguments(parser: argparse.ArgumentParser):
Expand Down Expand Up @@ -402,6 +404,11 @@ def main():
# Remove function and argument validation args
del argument_dict["func"]
del argument_dict["validate"]

if argument_dict["dictionary"] is None:
root = get_artifacts_root() / platform.system()
argument_dict["dictionary"] = find_dict(root)

function(**argument_dict)


Expand Down
80 changes: 3 additions & 77 deletions src/fprime_gds/executables/run_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,11 @@
import sys
import platform
import webbrowser
from pathlib import Path
from fprime.fbuild.settings import (
IniSettings,
FprimeLocationUnknownException,
FprimeSettingsException,
)

import fprime_gds.executables.cli
import fprime_gds.executables.utils


def get_artifacts_root() -> Path:
try:
ini_settings = IniSettings.load(None)
except FprimeLocationUnknownException:
print(
"[ERROR] Not in fprime deployment and no artifacts root provided, unable to find dictionary and/or app"
)
sys.exit(-1)
except FprimeSettingsException as e:
print("[ERROR]", e)
sys.exit(-1)
assert "install_dest" in ini_settings, "install_dest not in settings.ini"
print(
f"""[INFO] Autodetected artifacts root '{ini_settings["install_dest"]}' from deployment settings.ini file."""
)
return ini_settings["install_dest"]


def find_app(root: Path) -> Path:
bin_dir = root / "bin"

if not bin_dir.exists():
print(f"[ERROR] binary location {bin_dir} does not exist")
sys.exit(-1)

files = []
for child in bin_dir.iterdir():
if child.is_file():
files.append(child)

if len(files) == 0:
print(f"[ERROR] App not found in {bin_dir}")
sys.exit(-1)

if len(files) > 1:
print(
f"[ERROR] Multiple app candidates in binary location {bin_dir}. Specify app manually with --app."
)
sys.exit(-1)

return files[0]


def find_dict(root: Path) -> Path:
dict_dir = root / "dict"

if not dict_dir.exists():
print(f"[ERROR] dictionary location {dict_dir} does not exist")
sys.exit(-1)

files = []
for child in dict_dir.iterdir():
if child.is_file() and child.suffix == ".xml":
files.append(child)

if len(files) == 0:
print(f"[ERROR] No xml dictionary found in dictionary location {dict_dir}")
sys.exit(-1)

if len(files) > 1:
print(
f"[ERROR] Multiple xml dictionaries found in dictionary location {dict_dir}. Specify dictionary manually with --dictionary."
)
sys.exit(-1)

return files[0]


def get_settings():
args = parse_args()

Expand All @@ -96,14 +22,14 @@ def get_settings():
if args.root_dir is not None:
root = Path(args.root_dir)
else:
root = get_artifacts_root()
root = fprime_gds.executables.utils.get_artifacts_root()
root = root / platform.system()

if not args.noapp and args.app is None:
args.app = find_app(root)
args.app = fprime_gds.executables.utils.find_app(root)

if args.dictionary is None:
args.dictionary = find_dict(root)
args.dictionary = fprime_gds.executables.utils.find_dict(root)

return args

Expand Down
74 changes: 74 additions & 0 deletions src/fprime_gds/executables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
import signal
import subprocess
import time
from pathlib import Path
from fprime.fbuild.settings import (
IniSettings,
FprimeLocationUnknownException,
FprimeSettingsException,
)

# Python 2.7 compatibility, adding in missing error type
try:
Expand Down Expand Up @@ -118,3 +124,71 @@ def run_wrapped_application(arguments, logfile=None, env=None, launch_time=None)
raise AppWrapperException(
f'Failed to run application: {" ".join(arguments)}. Error: {exc}'
)


def get_artifacts_root() -> Path:
try:
ini_settings = IniSettings.load(None)
except FprimeLocationUnknownException:
print(
"[ERROR] Not in fprime deployment and no artifacts root provided, unable to find dictionary and/or app"
)
sys.exit(-1)
except FprimeSettingsException as e:
print("[ERROR]", e)
sys.exit(-1)
assert "install_dest" in ini_settings, "install_dest not in settings.ini"
print(
f"""[INFO] Autodetected artifacts root '{ini_settings["install_dest"]}' from deployment settings.ini file."""
)
return ini_settings["install_dest"]


def find_app(root: Path) -> Path:
bin_dir = root / "bin"

if not bin_dir.exists():
print(f"[ERROR] binary location {bin_dir} does not exist")
sys.exit(-1)

files = []
for child in bin_dir.iterdir():
if child.is_file():
files.append(child)

if len(files) == 0:
print(f"[ERROR] App not found in {bin_dir}")
sys.exit(-1)

if len(files) > 1:
print(
f"[ERROR] Multiple app candidates in binary location {bin_dir}. Specify app manually with --app."
)
sys.exit(-1)

return files[0]


def find_dict(root: Path) -> Path:
dict_dir = root / "dict"

if not dict_dir.exists():
print(f"[ERROR] dictionary location {dict_dir} does not exist")
sys.exit(-1)

files = []
for child in dict_dir.iterdir():
if child.is_file() and child.suffix == ".xml":
files.append(child)

if len(files) == 0:
print(f"[ERROR] No xml dictionary found in dictionary location {dict_dir}")
sys.exit(-1)

if len(files) > 1:
print(
f"[ERROR] Multiple xml dictionaries found in dictionary location {dict_dir}. Specify dictionary manually with --dictionary."
)
sys.exit(-1)

return files[0]

0 comments on commit 97f5492

Please sign in to comment.