[lxc-devel] [pylxd/master] Implemented recursive file transfer

mrtc0 on Github lxc-bot at linuxcontainers.org
Wed Jan 10 15:06:43 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1340 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180110/e1ca9be5/attachment.bin>
-------------- next part --------------
From e2d467b551a61212299d7bce6b0eaf271809b68f Mon Sep 17 00:00:00 2001
From: mrtc0 <mrtc0.py at gmail.com>
Date: Wed, 10 Jan 2018 23:51:47 +0900
Subject: [PATCH] Implemented recursive file transfer

Refer to https://github.com/lxc/pylxd/issues/246 .

Container.files.recursive_put(src, dst), allowd recursive transfer of
files.
This means that folder transfer is possible.
---
 pylxd/models/container.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/pylxd/models/container.py b/pylxd/models/container.py
index 84a0b19..5cbb054 100644
--- a/pylxd/models/container.py
+++ b/pylxd/models/container.py
@@ -13,6 +13,7 @@
 #    under the License.
 import collections
 import time
+import os
 
 import six
 from six.moves.urllib import parse
@@ -90,6 +91,27 @@ def get(self, filepath):
                 params={'path': filepath})
             return response.content
 
+        def recursive_put(self, src, dst):
+            if os.path.isdir(src):
+                idx = len(os.path.dirname(src))
+                dst_dirs = []
+                for path, dirname, files in os.walk(src):
+                    dst_dir = os.path.join(dst, path[idx:].lstrip(os.path.sep))
+                    dst_dirs.append(dst_dir)
+
+                self._container.execute(['mkdir', '-p'] + dst_dirs)
+                # Copy files
+                responses = []
+                for path, dirname, files in os.walk(src):
+                    dst_dir = os.path.join(dst, path[idx:].lstrip(os.path.sep))
+                    for name in files:
+                        src_name = os.path.join(path, name)
+                        dst_name = os.path.join(dst_dir, name)
+                        response = self._container.files.put(
+                            dst_name, data=open(src_name, 'rb').read())
+                        responses.append(response)
+                return all(responses)
+
     @classmethod
     def exists(cls, client, name):
         """Determine whether a container exists."""


More information about the lxc-devel mailing list