Skip to content

Commit

Permalink
Escape filename spaces on remotes, fixes #322
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed May 25, 2017
1 parent 2d8843b commit 4e55437
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.6.4 (in progress)
-------------------

* Fixes for globbing with spaces in filename on a remote server (`#322 <https://github.com/tomerfiliba/plumbum/issues/322>`_)

1.6.3
-----
* Python 3.6 is now supported, critical bug fixed (`#302 <https://github.com/tomerfiliba/plumbum/issues/302>`_)
Expand Down
5 changes: 4 additions & 1 deletion plumbum/machines/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ def _path_listdir(self, fn):
files.remove("..")
return files
def _path_glob(self, fn, pattern):
matches = self._session.run("for fn in %s/%s; do echo $fn; done" % (fn, pattern))[1].splitlines()
# shquote does not work here due to the way bash loops use space as a seperator
pattern = pattern.replace(" ", r"\ ")
fn = fn.replace(" ", r"\ ")
matches = self._session.run(r'for fn in {0}/{1}; do echo $fn; done'.format(fn,pattern))[1].splitlines()
if len(matches) == 1 and not self._path_stat(matches[0]):
return [] # pattern expansion failed
return matches
Expand Down
Empty file added tests/file with space.txt
Empty file.
8 changes: 8 additions & 0 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ def test_path(self):
found = True
assert found

@skip_on_windows
def test_glob_spaces(self):
fileloc = local.cwd / 'file with space.txt'
assert fileloc.exists()

assert local.cwd // "*space.txt"
assert local.cwd // "file with*"

@skip_on_windows
def test_env(self):
assert "PATH" in local.env
Expand Down
9 changes: 9 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ def test_glob(self):
assert "test_remote.py" in filenames
assert "slow_process.bash" in filenames

def test_glob_spaces(self):
with self._connect() as rem:
with rem.cwd(os.path.dirname(os.path.abspath(__file__))):
filenames = [f.name for f in rem.cwd // ("*space.txt")]
assert "file with space.txt" in filenames

filenames = [f.name for f in rem.cwd // ("*with space.txt")]
assert "file with space.txt" in filenames

@pytest.mark.usefixtures("testdir")
def test_download_upload(self):
with self._connect() as rem:
Expand Down

0 comments on commit 4e55437

Please sign in to comment.