diff options
author | Mariano Lopez <mariano.lopez@linux.intel.com> | 2016-05-02 11:27:10 +0000 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2016-05-11 10:33:41 +0100 |
commit | eb0ab04149d4d2f3966bce7dd2060d68c6963131 (patch) | |
tree | 2d5527c4f3f982e84cc89716691b9d3dd4a49303 | |
parent | e81d7a8afe4355b2fcb28bf69ad527fa041441ab (diff) | |
download | poky-eb0ab04149d4d2f3966bce7dd2060d68c6963131.tar.gz |
sshcontrol.py: Add methods to copy dirs and delete files
This patch add new methods to SSHControl class. These methods
include:
- Copy a dir to DUT
- Delete a file in the DUT
- Delete a directory in the DUT (if empty)
- Delete a directory structure in the DUT
[YOCTO #9565]
(From OE-Core rev: f22afb09fefdcb568d79ccd81277a43dacee2a82)
Signed-off-by: Mariano Lopez <mariano.lopez@linux.intel.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | meta/lib/oeqa/utils/sshcontrol.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/meta/lib/oeqa/utils/sshcontrol.py b/meta/lib/oeqa/utils/sshcontrol.py index 1658744165..ff88d37bd9 100644 --- a/meta/lib/oeqa/utils/sshcontrol.py +++ b/meta/lib/oeqa/utils/sshcontrol.py | |||
@@ -1,3 +1,4 @@ | |||
1 | # -*- coding: utf-8 -*- | ||
1 | # Copyright (C) 2013 Intel Corporation | 2 | # Copyright (C) 2013 Intel Corporation |
2 | # | 3 | # |
3 | # Released under the MIT license (see COPYING.MIT) | 4 | # Released under the MIT license (see COPYING.MIT) |
@@ -151,3 +152,86 @@ class SSHControl(object): | |||
151 | def copy_from(self, remotepath, localpath): | 152 | def copy_from(self, remotepath, localpath): |
152 | command = self.scp + ['%s@%s:%s' % (self.user, self.ip, remotepath), localpath] | 153 | command = self.scp + ['%s@%s:%s' % (self.user, self.ip, remotepath), localpath] |
153 | return self._internal_run(command, ignore_status=False) | 154 | return self._internal_run(command, ignore_status=False) |
155 | |||
156 | def copy_dir_to(self, localpath, remotepath): | ||
157 | """ | ||
158 | Copy recursively localpath directory to remotepath in target. | ||
159 | """ | ||
160 | |||
161 | for root, dirs, files in os.walk(localpath): | ||
162 | # Create directories in the target as needed | ||
163 | for d in dirs: | ||
164 | tmp_dir = os.path.join(root, d).replace(localpath, "") | ||
165 | new_dir = os.path.join(remotepath, tmp_dir.lstrip("/")) | ||
166 | cmd = "mkdir -p %s" % new_dir | ||
167 | self.run(cmd) | ||
168 | |||
169 | # Copy files into the target | ||
170 | for f in files: | ||
171 | tmp_file = os.path.join(root, f).replace(localpath, "") | ||
172 | dst_file = os.path.join(remotepath, tmp_file.lstrip("/")) | ||
173 | src_file = os.path.join(root, f) | ||
174 | self.copy_to(src_file, dst_file) | ||
175 | |||
176 | |||
177 | def delete_files(self, remotepath, files): | ||
178 | """ | ||
179 | Delete files in target's remote path. | ||
180 | """ | ||
181 | |||
182 | cmd = "rm" | ||
183 | if not isinstance(files, list): | ||
184 | files = [files] | ||
185 | |||
186 | for f in files: | ||
187 | cmd = "%s %s" % (cmd, os.path.join(remotepath, f)) | ||
188 | |||
189 | self.run(cmd) | ||
190 | |||
191 | |||
192 | def delete_dir(self, remotepath): | ||
193 | """ | ||
194 | Delete remotepath directory in target. | ||
195 | """ | ||
196 | |||
197 | cmd = "rmdir %s" % remotepath | ||
198 | self.run(cmd) | ||
199 | |||
200 | |||
201 | def delete_dir_structure(self, localpath, remotepath): | ||
202 | """ | ||
203 | Delete recursively localpath structure directory in target's remotepath. | ||
204 | |||
205 | This function is very usefult to delete a package that is installed in | ||
206 | the DUT and the host running the test has such package extracted in tmp | ||
207 | directory. | ||
208 | |||
209 | Example: | ||
210 | pwd: /home/user/tmp | ||
211 | tree: . | ||
212 | └── work | ||
213 | ├── dir1 | ||
214 | │ └── file1 | ||
215 | └── dir2 | ||
216 | |||
217 | localpath = "/home/user/tmp" and remotepath = "/home/user" | ||
218 | |||
219 | With the above variables this function will try to delete the | ||
220 | directory in the DUT in this order: | ||
221 | /home/user/work/dir1/file1 | ||
222 | /home/user/work/dir1 (if dir is empty) | ||
223 | /home/user/work/dir2 (if dir is empty) | ||
224 | /home/user/work (if dir is empty) | ||
225 | """ | ||
226 | |||
227 | for root, dirs, files in os.walk(localpath, topdown=False): | ||
228 | # Delete files first | ||
229 | tmpdir = os.path.join(root).replace(localpath, "") | ||
230 | remotedir = os.path.join(remotepath, tmpdir.lstrip("/")) | ||
231 | self.delete_files(remotedir, files) | ||
232 | |||
233 | # Remove dirs if empty | ||
234 | for d in dirs: | ||
235 | tmpdir = os.path.join(root, d).replace(localpath, "") | ||
236 | remotedir = os.path.join(remotepath, tmpdir.lstrip("/")) | ||
237 | self.delete_dir(remotepath) | ||