Skip to content

Commit e8842bf

Browse files
authored
Move Host.copy_to to filesystem module (#70)
Fixes #25
1 parent 0a9f96e commit e8842bf

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

README.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ related to files.
4242
h.fs.chmod("/path/to/file", "644")
4343
h.fs.unlink("/path/to/file")
4444
45+
In additional there are methods to fetch / put file from / to remote system
46+
to / from local system.
47+
48+
.. code:: python
49+
50+
h.fs.get("/path/to/remote/file", "/path/to/local/file/or/target/dir")
51+
h.fs.put("/path/to/local/file", "/path/to/remote/file/or/target/dir")
52+
53+
There is one special method which allows transfer file between hosts.
54+
55+
.. code:: python
56+
57+
h1.fs.transfer(
58+
"/path/to/file/on/h1",
59+
h2, "/path/to/file/on/h2/or/target/dir",
60+
)
61+
4562
Network
4663
~~~~~~~
4764

rrmngmnt/filesystem.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,67 @@ def chmod(self, path, mode):
132132
"""
133133
self._exec_command(['chmod', mode, path])
134134

135+
def get(self, path_src, path_dst):
136+
"""
137+
Fetch file from Host and store on local system
138+
139+
:param path_src: path to file on remote system
140+
:type path_src: str
141+
:param path_dst: path to file on local system or directory
142+
:type path_dst: str
143+
:return: path to destination file
144+
:rtype: str
145+
"""
146+
if os.path.isdir(path_dst):
147+
path_dst = os.path.join(path_dst, os.path.basename(path_src))
148+
with self.host.executor().session() as ss:
149+
with ss.open_file(path_src, 'rb') as rh:
150+
with open(path_dst, 'wb') as wh:
151+
wh.write(rh.read())
152+
return path_dst
153+
154+
def put(self, path_src, path_dst):
155+
"""
156+
Upload file from local system to Host
157+
158+
:param path_src: path to file on local system
159+
:type path_src: str
160+
:param path_dst: path to file on remote system or directory
161+
:type path_dst: str
162+
:return: path to destination file
163+
:rtype: str
164+
"""
165+
if self.isdir(path_dst):
166+
path_dst = os.path.join(path_dst, os.path.basename(path_src))
167+
with self.host.executor().session() as ss:
168+
with open(path_src, 'rb') as rh:
169+
with ss.open_file(path_dst, 'wb') as wh:
170+
wh.write(rh.read())
171+
return path_dst
172+
173+
def transfer(self, path_src, target_host, path_dst):
174+
"""
175+
Transfer file from one remote system (self) to other
176+
remote system (target_host).
177+
178+
:param path_src: path to file on local system
179+
:type path_src: str
180+
:param target_host: target system
181+
:type target_host: instance of Host
182+
:param path_dst: path to file on remote system or directory
183+
:type path_dst: str
184+
:return: path to destination file
185+
:rtype: str
186+
"""
187+
if target_host.fs.isdir(path_dst):
188+
path_dst = os.path.join(path_dst, os.path.basename(path_src))
189+
with self.host.executor().session() as h1s:
190+
with target_host.executor().session() as h2s:
191+
with h1s.open_file(path_src, 'rb') as rh:
192+
with h2s.open_file(path_dst, 'wb') as wh:
193+
wh.write(rh.read())
194+
return path_dst
195+
135196
def wget(self, url, output_file, progress_handler=None):
136197
"""
137198
Download file on the host from given url

rrmngmnt/host.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ def copy_to(self, resource, src, dst, mode=None, ownership=None):
261261
:param ownership: file ownership(ex. ('root', 'root'))
262262
:type ownership: tuple
263263
"""
264+
warnings.warn(
265+
"This method is deprecated and will be removed. "
266+
"Use Host.fs.transfer instead."
267+
)
264268
with resource.executor().session() as resource_session:
265269
with self.executor().session() as host_session:
266270
with resource_session.open_file(src, 'rb') as resource_file:

tests/test_filesystem.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,54 @@ def test_listdir_two(self):
130130
assert self.get_host().fs.listdir('/path/to/two') == [
131131
'first', 'second',
132132
]
133+
134+
135+
class TestFSGetPutFile(object):
136+
data = {
137+
"[ -d /path/to/put_dir ]": (0, "", ""),
138+
}
139+
files = {
140+
"/path/to/get_file": "data of get_file",
141+
}
142+
143+
@classmethod
144+
def setup_class(cls):
145+
fake_cmd_data(cls.data, cls.files)
146+
147+
def get_host(self, ip='1.1.1.1'):
148+
return Host(ip)
149+
150+
def test_get(self, tmpdir):
151+
self.get_host().fs.get("/path/to/get_file", str(tmpdir))
152+
assert tmpdir.join("get_file").read() == "data of get_file"
153+
154+
def test_put(self, tmpdir):
155+
p = tmpdir.join("put_file")
156+
p.write("data of put_file")
157+
self.get_host().fs.put(str(p), "/path/to/put_dir")
158+
assert self.files[
159+
'/path/to/put_dir/put_file'].data == "data of put_file"
160+
161+
162+
class TestTransfer(object):
163+
data = {
164+
"[ -d /path/to/dest_dir ]": (0, "", ""),
165+
}
166+
files = {
167+
"/path/to/file_to_transfer": "data to transfer",
168+
}
169+
170+
@classmethod
171+
def setup_class(cls):
172+
fake_cmd_data(cls.data, cls.files)
173+
174+
def get_host(self, ip='1.1.1.1'):
175+
return Host(ip)
176+
177+
def test_transfer(self):
178+
self.get_host().fs.transfer(
179+
"/path/to/file_to_transfer", self.get_host("1.1.1.2"),
180+
"/path/to/dest_dir",
181+
)
182+
assert self.files[
183+
'/path/to/dest_dir/file_to_transfer'].data == "data to transfer"

0 commit comments

Comments
 (0)