Skip to content

Commit 18646f7

Browse files
committed
util/managedfile: replace ssh usage for local case
In case we operate on the local machine, there is no need to force the user to have a setup where they can connect via SSH to localhost. Instead implement the symlink functionality via pure python os module functionality. Signed-off-by: Rouven Czerwinski <[email protected]>
1 parent bc6de2e commit 18646f7

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

labgrid/util/managedfile.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,23 @@ def sync_to_resource(self, symlink=None):
6868
f"{self.rpath}{os.path.basename(self.local_path)}"
6969
)
7070
else:
71-
conn = sshmanager.open("localhost")
7271
self.rpath = os.path.dirname(self.local_path) + "/"
7372

7473
if symlink is not None:
7574
self.logger.info("Linking")
76-
try:
77-
conn.run_check(f"test ! -e {symlink} -o -L {symlink}")
78-
except ExecutionError:
79-
raise ManagedFileError(f"Path {symlink} exists but is not a symlink.")
80-
# use short options to be compatible with busybox
81-
# --symbolic --force --no-dereference
82-
conn.run_check(f"ln -sfn {self.rpath}{os.path.basename(self.local_path)} {symlink}")
75+
if isinstance(self.resource, NetworkResource):
76+
try:
77+
conn.run_check(f"test ! -e {symlink} -o -L {symlink}")
78+
except ExecutionError:
79+
raise ManagedFileError(f"Path {symlink} exists but is not a symlink.")
80+
# use short options to be compatible with busybox
81+
# --symbolic --force --no-dereference
82+
conn.run_check(f"ln -sfn {self.rpath}{os.path.basename(self.local_path)} {symlink}")
83+
else:
84+
if os.path.exists(symlink) and not os.path.islink(symlink):
85+
raise ManagedFileError(f"Path {symlink} exists but is not a symlink.")
86+
os.symlink(f"{self.rpath}{os.path.basename(self.local_path)}", symlink)
87+
8388

8489
def _on_nfs(self, conn):
8590
if self._on_nfs_cached is not None:

tests/test_util.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,7 @@ def test_local_managedfile(target, tmpdir):
354354
assert str(t) == mf.get_remote_path()
355355

356356
def test_network_managedfile_no_sync(target, tmpdir):
357-
import hashlib
358-
359-
res = NetworkResource(target, "localhost", "test")
357+
res = NetworkResource(target, "test", "localhost")
360358
t = tmpdir.join("test")
361359
t.write(
362360
"""
@@ -369,6 +367,33 @@ def test_network_managedfile_no_sync(target, tmpdir):
369367
with pytest.raises(ManagedFileError, match=expected) as e:
370368
mf.get_remote_path()
371369

370+
def test_local_managedfile_symlink(target, tmpdir):
371+
res = Resource(target, "test")
372+
t = tmpdir.join("test")
373+
t.write(
374+
"""
375+
Test
376+
"""
377+
)
378+
mf = ManagedFile(t, res, detect_nfs=False)
379+
mf.sync_to_resource(symlink=tmpdir.join("link"))
380+
381+
assert str(t) == mf.get_remote_path()
382+
assert os.path.islink(tmpdir.join("link"))
383+
384+
@pytest.mark.localsshmanager
385+
def test_remote_managedfile_symlink(target, tmpdir):
386+
res = NetworkResource(target, "test", "localhost")
387+
t = tmpdir.join("test")
388+
t.write(
389+
"""
390+
Test
391+
"""
392+
)
393+
mf = ManagedFile(t, res, detect_nfs=False)
394+
mf.sync_to_resource(symlink=tmpdir.join("link"))
395+
396+
assert os.path.islink(tmpdir.join("link"))
372397

373398
def test_find_dict():
374399
dict_a = {"a": {"a.a": {"a.a.a": "a.a.a_val"}}, "b": "b_val"}

0 commit comments

Comments
 (0)