Skip to content

Commit

Permalink
Add force option to generate command for purging before re-generating (
Browse files Browse the repository at this point in the history
…#217)

* add force option to generate that will call purge before generating, still need to check edgecases

* formatting

* Use build.purge() instead of purge() utility

* Fix build_dir doesn't exist + spelling

---------

Co-authored-by: crsmith <[email protected]>
Co-authored-by: thomas-bc <[email protected]>
  • Loading branch information
3 people authored Oct 14, 2024
1 parent 439c38c commit d1704ae
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ openpty
optarg
optionxform
oran
OSAL
Packetizer
Paetz
params
Expand Down
5 changes: 3 additions & 2 deletions src/fprime/fbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, build_type: BuildType, project: Path, verbose: bool = False):
self.cmake = CMakeHandler()
self.cmake.set_verbose(verbose)

def invent(self, platform: str = None, build_dir: Path = None):
def invent(self, platform: str = None, build_dir: Path = None, force=False):
"""Invents a build path from a given platform
Sets this build up as a new build that would be used as as part of a generate step. This directory must not
Expand All @@ -91,7 +91,8 @@ def invent(self, platform: str = None, build_dir: Path = None):
self.__setup_default(platform, build_dir)
if self.build_dir.exists():
msg = f"{self.build_dir} already exists."
raise InvalidBuildCacheException(msg)
if not force:
raise InvalidBuildCacheException(msg)

def load(self, platform: str = None, build_dir: Path = None, skip_validation=False):
"""Load an existing build cache
Expand Down
92 changes: 53 additions & 39 deletions src/fprime/fbuild/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def run_fbuild_cli(
make_args: arguments to supply to the build tool (make or ninja)
"""
if parsed.command == "generate":
if parsed.force and build.build_dir.exists():
print(f"[INFO] Purging build directory at: {build.build_dir}")
build.purge()

toolchain = build.find_toolchain()
print(f"[INFO] Generating build directory at: {build.build_dir}")
print(f"[INFO] Using toolchain file {toolchain} for platform {parsed.platform}")
Expand All @@ -65,45 +69,7 @@ def run_fbuild_cli(
build.generate(cmake_args)
elif parsed.command == "purge":
# Since purge does not load its "base", we need to overload the platform
build.platform = parsed.platform
for purge_build in Build.get_build_list(
build, parsed.build_cache, ignore_invalid=parsed.force
):
print(
f"[INFO] {parsed.command.title()} build directory at: {purge_build.build_dir}"
)
try:
perform_purge = False
if parsed.force:
if Path(purge_build.build_dir).is_dir():
perform_purge = True
else:
print(
f"[INFO] Skipping purge. The following directory does not exist: {purge_build.build_dir}"
)
else:
perform_purge = confirm("Purge this directory?")

if perform_purge:
purge_build.purge()

install_dir = purge_build.install_dest_exists()
if (
purge_build.build_type != BuildType.BUILD_CUSTOM
and install_dir
and install_dir.exists()
):
print(
f"[INFO] {parsed.command.title()} install directory at: {install_dir}"
)
if parsed.force or confirm(
f"Purge installation directory at {install_dir} ?"
):
purge_build.purge_install()
except PermissionError as e:
print(
f"Error: Permission denied while purging {purge_build.build_dir}: {e}"
)
purge(build, parsed)

else:
target = get_target(parsed)
Expand All @@ -118,6 +84,47 @@ def run_fbuild_cli(
)


def purge(build: Build, parsed: argparse.Namespace):
for purge_build in Build.get_build_list(
build, parsed.build_cache, ignore_invalid=parsed.force
):
print(
f"[INFO] {parsed.command.title()} build directory at: {purge_build.build_dir}"
)
try:
perform_purge = False
if parsed.force:
if Path(purge_build.build_dir).is_dir():
perform_purge = True
else:
print(
f"[INFO] Skipping purge. The following directory does not exist: {purge_build.build_dir}"
)
else:
perform_purge = confirm("Purge this directory?")

if perform_purge:
purge_build.purge()

install_dir = purge_build.install_dest_exists()
if (
purge_build.build_type != BuildType.BUILD_CUSTOM
and install_dir
and install_dir.exists()
):
print(
f"[INFO] {parsed.command.title()} install directory at: {install_dir}"
)
if parsed.force or confirm(
f"Purge installation directory at {install_dir} ?"
):
purge_build.purge_install()
except PermissionError as e:
print(
f"Error: Permission denied while purging {purge_build.build_dir}: {e}"
)


def add_target_parser(
target: Target,
subparsers,
Expand Down Expand Up @@ -241,6 +248,13 @@ def add_special_targets(
action="store_true",
help="Pass -D flags through to CMake. Can be used multiple times.",
)
generate_parser.add_argument(
"-f",
"--force",
default=False,
action="store_true",
help="Before generating, purges the build directory by force if it already exists. No confirmation will be requested.",
)
purge_parser = subparsers.add_parser(
"purge",
help=help_text.short("purge", "Remove build caches for specified project"),
Expand Down
2 changes: 1 addition & 1 deletion src/fprime/util/build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def load_build(parsed, skip_validation=False):
# However, the base directory must be setup here. Errors in this load are ignored to allow the command to find
# build caches related to that set.
if parsed.command == "generate":
build.invent(parsed.platform, build_dir=parsed.build_cache)
build.invent(parsed.platform, build_dir=parsed.build_cache, force=parsed.force)
else:
build.load(
parsed.platform,
Expand Down

0 comments on commit d1704ae

Please sign in to comment.