From 7c6de9108667feb014071f22ebbbd09c03b967bb Mon Sep 17 00:00:00 2001 From: feldschlachtiv Date: Wed, 14 Aug 2019 02:50:02 -0400 Subject: [PATCH 1/3] use abspath instead of relpath For compatibility with Windows. relpath() fails when the specified path is on a different drive. --- samcli/commands/build/command.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samcli/commands/build/command.py b/samcli/commands/build/command.py index 9c331d2163..ed0c79e7ee 100644 --- a/samcli/commands/build/command.py +++ b/samcli/commands/build/command.py @@ -158,8 +158,8 @@ def do_cli(function_identifier, # pylint: disable=too-many-locals click.secho("\nBuild Succeeded", fg="green") - msg = gen_success_msg(os.path.relpath(ctx.build_dir), - os.path.relpath(ctx.output_template_path), + msg = gen_success_msg(os.path.abspath(ctx.build_dir), + os.path.abspath(ctx.output_template_path), os.path.abspath(ctx.build_dir) == os.path.abspath(DEFAULT_BUILD_DIR)) click.secho(msg, fg="yellow") From 0b6d761452acc488f0f5b670c06125aae1ce92a7 Mon Sep 17 00:00:00 2001 From: feldschlachtiv Date: Fri, 16 Aug 2019 16:37:57 -0400 Subject: [PATCH 2/3] Try to relpath first, then abspath --- samcli/commands/build/command.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/samcli/commands/build/command.py b/samcli/commands/build/command.py index ed0c79e7ee..cd838dc00e 100644 --- a/samcli/commands/build/command.py +++ b/samcli/commands/build/command.py @@ -158,8 +158,19 @@ def do_cli(function_identifier, # pylint: disable=too-many-locals click.secho("\nBuild Succeeded", fg="green") - msg = gen_success_msg(os.path.abspath(ctx.build_dir), - os.path.abspath(ctx.output_template_path), + # try to use relpath so the command is easier to understand, however, + # under Windows, when SAM and (build_dir or output_template_path) are + # on different drive, relpath() fails. + try: + build_dir = os.path.relpath(ctx.build_dir) + output_template_path = os.path.relpath(ctx.output_template_path) + except ValueError: + LOG.debug("Failed to retrieve relpath - using abspath instead") + build_dir = os.path.abspath(ctx.build_dir) + output_template_path = os.path.abspath(ctx.output_template_path) + + msg = gen_success_msg(build_dir, + output_template_path, os.path.abspath(ctx.build_dir) == os.path.abspath(DEFAULT_BUILD_DIR)) click.secho(msg, fg="yellow") From d58a558da5a126e058b448a73fd132150eff9a4e Mon Sep 17 00:00:00 2001 From: feldschlachtiv Date: Wed, 21 Aug 2019 21:28:30 -0400 Subject: [PATCH 3/3] use the user specified path when relpath fails Also rename a couple of variables to be a little more descriptive. --- samcli/commands/build/command.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/samcli/commands/build/command.py b/samcli/commands/build/command.py index cd838dc00e..a84cf804de 100644 --- a/samcli/commands/build/command.py +++ b/samcli/commands/build/command.py @@ -162,15 +162,15 @@ def do_cli(function_identifier, # pylint: disable=too-many-locals # under Windows, when SAM and (build_dir or output_template_path) are # on different drive, relpath() fails. try: - build_dir = os.path.relpath(ctx.build_dir) - output_template_path = os.path.relpath(ctx.output_template_path) + build_dir_in_success_message = os.path.relpath(ctx.build_dir) + output_template_path_in_success_message = os.path.relpath(ctx.output_template_path) except ValueError: - LOG.debug("Failed to retrieve relpath - using abspath instead") - build_dir = os.path.abspath(ctx.build_dir) - output_template_path = os.path.abspath(ctx.output_template_path) + LOG.debug("Failed to retrieve relpath - using the specified path as-is instead") + build_dir_in_success_message = ctx.build_dir + output_template_path_in_success_message = ctx.output_template_path - msg = gen_success_msg(build_dir, - output_template_path, + msg = gen_success_msg(build_dir_in_success_message, + output_template_path_in_success_message, os.path.abspath(ctx.build_dir) == os.path.abspath(DEFAULT_BUILD_DIR)) click.secho(msg, fg="yellow")