Skip to content
35 changes: 35 additions & 0 deletions colcon_core/verb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,41 @@ def check_and_mark_install_layout(install_base, *, merge_install):
marker_path.write_text(this_install_layout + '\n')


def check_and_mark_root_dir(this_build_tool='colcon'):
"""
Check the marker file for root workspace.

If marker file is found in parent directory, raise error;
if it is found in current direcotry, continue to build.
If no marker file found, create marker file.

The marker filename is `.colcon`.

:param str this_build_tool: The name of this build tool
:raises RuntimeError: if marker file is found in parent directory
"""
original_path = Path(os.getcwd())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be clearer (easier to test, and more reusable) if the function would have the start path as an argument rather then getting it internally from global state?

current_path = original_path
parent_path = current_path.parent
while current_path.name != parent_path.name:
Comment thread
claireyywang marked this conversation as resolved.
Outdated
marker_path = current_path / '.colcon'
if marker_path.parent.is_dir() and marker_path.is_file():
Comment thread
claireyywang marked this conversation as resolved.
Outdated
if current_path.name == original_path.name:
Comment thread
claireyywang marked this conversation as resolved.
Outdated
return
raise RuntimeError(
"'{original_path}' is not marked as the root directory. "
"Please go to '{current_path}' for `colcon build`. "
'If the current directory is intended to be the root '
"workspace, please remove the '.colcon' file "
"in '{current_path}'.".format_map(locals()))
else:
current_path = parent_path
parent_path = current_path.parent
Comment thread
claireyywang marked this conversation as resolved.
Outdated

marker_path = original_path / '.colcon'
Comment thread
claireyywang marked this conversation as resolved.
Outdated
marker_path.write_text(this_build_tool + '\n')
Comment thread
claireyywang marked this conversation as resolved.
Outdated


def update_object(
object_, key, value, package_name, argument_type, value_source
):
Expand Down
2 changes: 2 additions & 0 deletions colcon_core/verb/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from colcon_core.task import TaskContext
from colcon_core.verb import check_and_mark_build_tool
from colcon_core.verb import check_and_mark_install_layout
from colcon_core.verb import check_and_mark_root_dir
from colcon_core.verb import logger
from colcon_core.verb import update_object
from colcon_core.verb import VerbExtensionPoint
Expand Down Expand Up @@ -116,6 +117,7 @@ def add_arguments(self, *, parser): # noqa: D102
self.task_argument_destinations = decorated_parser.get_destinations()

def main(self, *, context): # noqa: D102
check_and_mark_root_dir()
check_and_mark_build_tool(context.args.build_base)
check_and_mark_install_layout(
context.args.install_base,
Expand Down
2 changes: 2 additions & 0 deletions colcon_core/verb/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from colcon_core.task import TaskContext
from colcon_core.verb import check_and_mark_build_tool
from colcon_core.verb import check_and_mark_install_layout
from colcon_core.verb import check_and_mark_root_dir
from colcon_core.verb import update_object
from colcon_core.verb import VerbExtensionPoint

Expand Down Expand Up @@ -130,6 +131,7 @@ def add_arguments(self, *, parser): # noqa: D102
'retest_until_fail'

def main(self, *, context): # noqa: D102
check_and_mark_root_dir()
check_and_mark_build_tool(context.args.build_base)
check_and_mark_install_layout(
context.args.install_base,
Expand Down