-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve path arguments when specified on command line (#575)
This change enhances the behavior of command line arguments which specify a path which might be relative to the current directory. The new functionality captures the current working directory when the parser is constructed, and captures a given relative path on the command line by resolving it from that captured working directory. If an argument is left to the default value, it is not resolved by this new functionality and is left for resolution in the consuming code later. The main use case for this capture-then-resolve behavior is to support changing the working directory of the colcon process. If that happens, the context of where a user intended a relative path to be based is lost, so this change captures the directory early in the colcon startup sequence.
- Loading branch information
Showing
4 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2023 Open Source Robotics Foundation, Inc. | ||
# Licensed under the Apache License, Version 2.0 | ||
|
||
import functools | ||
import os | ||
|
||
from colcon_core.argument_default import is_default_value | ||
|
||
|
||
def resolve_path(value, base=os.getcwd()): | ||
""" | ||
Resolve a path argument from the current directory. | ||
If the given value is an argument default, the value is returned | ||
unmodified. | ||
:param value: The value to resolve to an absolute path | ||
:returns: The unmodified value, or resolved path | ||
""" | ||
if value is None or is_default_value(value): | ||
return value | ||
res = os.path.abspath(os.path.join(base, str(value))) | ||
return res | ||
|
||
|
||
def get_cwd_path_resolver(): | ||
""" | ||
Create a function which resolves paths from the current directory. | ||
If the current directory changes between calling this function and calling | ||
the function returned by this function, the directory at the time of this | ||
function call is used. | ||
:returns: A function which takes a single string and returns a string | ||
""" | ||
return functools.partial(resolve_path, base=os.getcwd()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters