diff --git a/labgrid/driver/usbstoragedriver.py b/labgrid/driver/usbstoragedriver.py index 5ceec293d..1b7fd009a 100644 --- a/labgrid/driver/usbstoragedriver.py +++ b/labgrid/driver/usbstoragedriver.py @@ -99,9 +99,9 @@ def write_files(self, sources, target, partition, target_is_directory=True): target_path = str(pathlib.PurePath(mount_path) / target_rel) copied_sources = [] + files = [ManagedFile(f, self.storage) for f in sources] - for f in sources: - mf = ManagedFile(f, self.storage) + for mf in files: mf.sync_to_resource() copied_sources.append(mf.get_remote_path()) @@ -114,6 +114,10 @@ def write_files(self, sources, target, partition, target_is_directory=True): args = ["cp", "-T", copied_sources[0], target_path] processwrapper.check_output(self.storage.command_prefix + args) + + for mf in files: + mf.cleanup_resource() + self.proxy.unmount(self.devpath) except: # We are going to die with an exception anyway, so no point in waiting @@ -201,6 +205,8 @@ def write_image(self, filename=None, mode=Mode.DD, partition=None, skip=0, seek= print_on_silent_log=True ) + mf.cleanup_resource() + def _get_devpath(self, partition): partition = "" if partition is None else partition # simple concatenation is sufficient for USB mass storage diff --git a/labgrid/util/managedfile.py b/labgrid/util/managedfile.py index c3b19ed27..db5a7b885 100644 --- a/labgrid/util/managedfile.py +++ b/labgrid/util/managedfile.py @@ -78,6 +78,21 @@ def sync_to_resource(self, symlink=None): # --symbolic --force --no-dereference conn.run_check(f"ln -sfn {self.rpath}{os.path.basename(self.local_path)} {symlink}") + def cleanup_resource(self, symlink=None): + if isinstance(self.resource, NetworkResource): + host = self.resource.host + conn = sshmanager.open(host) + + if self._on_nfs(conn): + pass + else: + self.rpath = f"{self.get_user_cache_path()}/{self.get_hash()}/" + self.logger.info("Removing %s on %s", self.local_path, host) + conn.run_check(f"rm -rf {self.rpath}") + + if symlink is not None: + self.logger.info("Removing symlink {symlink}") + conn.run_check(f"rm -f {symlink}") def _on_nfs(self, conn): if self._on_nfs_cached is not None: