Skip to content

Commit

Permalink
Move Host.copy_to to filesystem module (#70)
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
lukas-bednar authored Aug 22, 2016
1 parent 0a9f96e commit e8842bf
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ related to files.
h.fs.chmod("/path/to/file", "644")
h.fs.unlink("/path/to/file")
In additional there are methods to fetch / put file from / to remote system
to / from local system.

.. code:: python
h.fs.get("/path/to/remote/file", "/path/to/local/file/or/target/dir")
h.fs.put("/path/to/local/file", "/path/to/remote/file/or/target/dir")
There is one special method which allows transfer file between hosts.

.. code:: python
h1.fs.transfer(
"/path/to/file/on/h1",
h2, "/path/to/file/on/h2/or/target/dir",
)
Network
~~~~~~~

Expand Down
61 changes: 61 additions & 0 deletions rrmngmnt/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,67 @@ def chmod(self, path, mode):
"""
self._exec_command(['chmod', mode, path])

def get(self, path_src, path_dst):
"""
Fetch file from Host and store on local system
:param path_src: path to file on remote system
:type path_src: str
:param path_dst: path to file on local system or directory
:type path_dst: str
:return: path to destination file
:rtype: str
"""
if os.path.isdir(path_dst):
path_dst = os.path.join(path_dst, os.path.basename(path_src))
with self.host.executor().session() as ss:
with ss.open_file(path_src, 'rb') as rh:
with open(path_dst, 'wb') as wh:
wh.write(rh.read())
return path_dst

def put(self, path_src, path_dst):
"""
Upload file from local system to Host
:param path_src: path to file on local system
:type path_src: str
:param path_dst: path to file on remote system or directory
:type path_dst: str
:return: path to destination file
:rtype: str
"""
if self.isdir(path_dst):
path_dst = os.path.join(path_dst, os.path.basename(path_src))
with self.host.executor().session() as ss:
with open(path_src, 'rb') as rh:
with ss.open_file(path_dst, 'wb') as wh:
wh.write(rh.read())
return path_dst

def transfer(self, path_src, target_host, path_dst):
"""
Transfer file from one remote system (self) to other
remote system (target_host).
:param path_src: path to file on local system
:type path_src: str
:param target_host: target system
:type target_host: instance of Host
:param path_dst: path to file on remote system or directory
:type path_dst: str
:return: path to destination file
:rtype: str
"""
if target_host.fs.isdir(path_dst):
path_dst = os.path.join(path_dst, os.path.basename(path_src))
with self.host.executor().session() as h1s:
with target_host.executor().session() as h2s:
with h1s.open_file(path_src, 'rb') as rh:
with h2s.open_file(path_dst, 'wb') as wh:
wh.write(rh.read())
return path_dst

def wget(self, url, output_file, progress_handler=None):
"""
Download file on the host from given url
Expand Down
4 changes: 4 additions & 0 deletions rrmngmnt/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ def copy_to(self, resource, src, dst, mode=None, ownership=None):
:param ownership: file ownership(ex. ('root', 'root'))
:type ownership: tuple
"""
warnings.warn(
"This method is deprecated and will be removed. "
"Use Host.fs.transfer instead."
)
with resource.executor().session() as resource_session:
with self.executor().session() as host_session:
with resource_session.open_file(src, 'rb') as resource_file:
Expand Down
51 changes: 51 additions & 0 deletions tests/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,54 @@ def test_listdir_two(self):
assert self.get_host().fs.listdir('/path/to/two') == [
'first', 'second',
]


class TestFSGetPutFile(object):
data = {
"[ -d /path/to/put_dir ]": (0, "", ""),
}
files = {
"/path/to/get_file": "data of get_file",
}

@classmethod
def setup_class(cls):
fake_cmd_data(cls.data, cls.files)

def get_host(self, ip='1.1.1.1'):
return Host(ip)

def test_get(self, tmpdir):
self.get_host().fs.get("/path/to/get_file", str(tmpdir))
assert tmpdir.join("get_file").read() == "data of get_file"

def test_put(self, tmpdir):
p = tmpdir.join("put_file")
p.write("data of put_file")
self.get_host().fs.put(str(p), "/path/to/put_dir")
assert self.files[
'/path/to/put_dir/put_file'].data == "data of put_file"


class TestTransfer(object):
data = {
"[ -d /path/to/dest_dir ]": (0, "", ""),
}
files = {
"/path/to/file_to_transfer": "data to transfer",
}

@classmethod
def setup_class(cls):
fake_cmd_data(cls.data, cls.files)

def get_host(self, ip='1.1.1.1'):
return Host(ip)

def test_transfer(self):
self.get_host().fs.transfer(
"/path/to/file_to_transfer", self.get_host("1.1.1.2"),
"/path/to/dest_dir",
)
assert self.files[
'/path/to/dest_dir/file_to_transfer'].data == "data to transfer"

0 comments on commit e8842bf

Please sign in to comment.