Skip to content
30 changes: 30 additions & 0 deletions colcon_core/verb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from colcon_core.plugin_system import order_extensions_by_name

logger = colcon_logger.getChild(__name__)
DEFAULT_START_PATH = Path(os.path.abspath(os.getcwd()))


class VerbExtensionPoint:
Expand Down Expand Up @@ -124,6 +125,35 @@ def check_and_mark_install_layout(install_base, *, merge_install):
marker_path.write_text(this_install_layout + '\n')


def check_and_mark_root_dir(start_path):
"""
Check the marker file for root workspace, otherwise create it.

The marker filename is `.root_dir`.

:param str start_path: The path where verb is invoked
:raises RuntimeError: if marker file is found in parent directory
"""
current_path = start_path
marker_name = '.root_dir'
Comment thread
claireyywang marked this conversation as resolved.
Outdated
home_path = Path(os.path.abspath(os.sep))
while current_path != home_path:
marker_path = current_path / marker_name
if marker_path.is_file():
if current_path != start_path:
raise RuntimeError(
"'{start_path}' is not marked as the root directory. "
"Please go to '{current_path}'. "
'If you want to mark current path as root directory, '
"please remove the '{marker_name}' file "
"in '{current_path}'.".format_map(locals()))
return
else:
current_path = current_path.parent
marker_path = start_path / marker_name
marker_path.write_text('colcon root directory\n')


def update_object(
object_, key, value, package_name, argument_type, value_source
):
Expand Down
10 changes: 9 additions & 1 deletion colcon_core/verb/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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 DEFAULT_START_PATH
from colcon_core.verb import logger
from colcon_core.verb import update_object
from colcon_core.verb import VerbExtensionPoint
Expand All @@ -49,7 +51,8 @@ def __init__(self, pkg, args, *, additional_destinations=None):
self.build_base = os.path.abspath(os.path.join(
os.getcwd(), args.build_base, pkg.name))
self.install_base = os.path.abspath(os.path.join(
os.getcwd(), args.install_base))
os.getcwd()))
self.start_path = DEFAULT_START_PATH
self.merge_install = args.merge_install
if not args.merge_install:
self.install_base = os.path.join(
Expand Down Expand Up @@ -89,6 +92,10 @@ def add_arguments(self, *, parser): # noqa: D102
'--install-base',
default='install',
help='The base path for all install prefixes (default: install)')
parser.add_argument(
'--start_path',
default=DEFAULT_START_PATH,
help='The directory where build verb is invoked in')
Comment thread
claireyywang marked this conversation as resolved.
Outdated
parser.add_argument(
'--merge-install',
action='store_true',
Expand Down Expand Up @@ -116,6 +123,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(context.args.start_path)
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.

I think the invocation here in the verb is too late since e.g. the log directory has already been created which isn't desired.

check_and_mark_build_tool(context.args.build_base)
check_and_mark_install_layout(
context.args.install_base,
Expand Down
8 changes: 8 additions & 0 deletions colcon_core/verb/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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 DEFAULT_START_PATH
from colcon_core.verb import update_object
from colcon_core.verb import VerbExtensionPoint

Expand All @@ -48,6 +50,7 @@ def __init__(self, pkg, args, *, additional_destinations=None):
os.getcwd(), args.build_base, pkg.name))
self.install_base = os.path.abspath(os.path.join(
os.getcwd(), args.install_base))
self.start_path = DEFAULT_START_PATH
if not args.merge_install:
self.install_base = os.path.join(
self.install_base, pkg.name)
Expand Down Expand Up @@ -95,6 +98,10 @@ def add_arguments(self, *, parser): # noqa: D102
'--merge-install',
action='store_true',
help='Merge all install prefixes into a single location')
parser.add_argument(
'--start_path',
default=DEFAULT_START_PATH,
help='The directory where test verb is invoked in')
parser.add_argument(
'--test-result-base',
help='The base path for all test results (default: --build-base)')
Expand Down Expand Up @@ -130,6 +137,7 @@ def add_arguments(self, *, parser): # noqa: D102
'retest_until_fail'

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