Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(path): change windows style path to unix #633

Merged
merged 1 commit into from
Sep 10, 2018

Conversation

sriram-mv
Copy link
Contributor

@sriram-mv sriram-mv commented Aug 28, 2018

  • On supplying path for docker in windows environments, change path to unix style.
  • This allows sam-cli to work with docker toolkit.

Issue #, if available:

Description of changes:

  • change windows style paths to unix style paths, to allow operability of the sam cli under windows 8.
  • Did testing on windows environment with docker toolbox and docker for windows.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


def path_modifier(func):
"""
This method decorator allows conversions of windows style path to unix style path
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update this to use numpy doc strings instead? We are moving off this format onto the numpy format with new docs.


os_mock.name = "nt"
with mock.patch("os.path", ntpath):
ntpath.isabs = lambda path: False
Copy link
Contributor

Choose a reason for hiding this comment

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

ooo... this is interesting, never considered this a way to mock functions out before!

def test_must_resolve_windows_path_to_unix_paths(self, os_mock):

os_mock.name = "nt"
with mock.patch("os.path", ntpath):
Copy link
Contributor

Choose a reason for hiding this comment

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

This could just be a style difference but why would you do this over mocking out os.path through os_mock.path = mock()?

@@ -53,7 +54,7 @@ def __init__(self,
self._image = image
self._cmd = cmd
self._working_dir = working_dir
self._host_dir = host_dir
self._host_dir = posix_path(host_dir)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we set the posix_path on all the volumes that get mounted? Apply to the kwargs on the create() method.

When we mount other volumes in the future, this will just transparently work.

code_path : str
Directory in the host operating system that should be mounted within the container.
"""
return posixpath.sep + re.sub("^([A-Za-z])+:",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a pretty complex expression. Not easy to follow for someone without context. Can you add a few examples to docstring of input and output of this method? It will help set context

import pathlib2 as pathlib


def posix_path(code_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: posix_path or to_posix_path?

Parameters
----------
code_path : str
Directory in the host operating system that should be mounted within the container.
Copy link
Contributor

Choose a reason for hiding this comment

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

Returns?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added Returns and Examples.

@@ -46,6 +46,32 @@ def test_init_must_store_all_values(self):
self.assertEquals(None, container.id)
self.assertEquals(self.mock_docker_client, container.docker_client)

@patch("samcli.local.docker.utils.os")
def test_init_change_host_dir_unix_style(self, os_mock):
os_mock.name = "nt"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you have a test for the non-nt case?

- On supplying path for docker, change path to unix style.
- This allows sam-cli to work with docker toolbox on windows.
@sriram-mv
Copy link
Contributor Author

Addressed the comments.

--------
>>> to_posix_path('/Users/UserName/sam-app')
/Users/UserName/sam-app
>>> to_posix_path('C:\\\\Users\\\\UserName\\\\AppData\\\\Local\\\\Temp\\\\mydir')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to do multiple escapes, because of quotes within docstrings, thats still a valid path and resolves.

In [332]: to_posix_path('C:\\Users\\UserName\\AppData\\Local\\Temp\\mydir')
Out[332]: '/c/Users/UserName/AppData/Local/Temp/mydir'

In [333]: to_posix_path('C:\\\\Users\\\\UserName\\\\AppData\\\\Local\\\\Temp\\\\mydir')
Out[333]: '/c/Users/UserName/AppData/Local/Temp/mydir'

self.mock_docker_client.containers.create.assert_called_with(self.image,
command=self.cmd,
working_dir=self.working_dir,
volumes=translated_volumes,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Additional volumes with windows style paths, also resolve to unix style paths.

Copy link
Contributor

@sanathkr sanathkr left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks for making the changes 🎉

@@ -104,6 +105,9 @@ def create(self):
if self._additional_volumes:
kwargs["volumes"].update(self._additional_volumes)

# Make sure all mounts are of posix path style.
Copy link
Contributor

Choose a reason for hiding this comment

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

nice! Thakns

@jfuss jfuss merged commit 7dd6d25 into aws:develop Sep 10, 2018
@sriram-mv sriram-mv mentioned this pull request Oct 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants