Skip to content
Snippets Groups Projects
update.py 1.97 KiB
import paramiko
import os

import sys
import pathlib

sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.confi_reader import read_ssh


class VersionManager:

    def __init__(self):
        self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve())+"/config.json")

    def update(self):
        # TODO: rm previous files
        for client in self.clients:
            transport = paramiko.Transport((client.hostname, 22))
            transport.connect(username=client.user, password=client.password)
            sftp = MySFTPClient.from_transport(transport)
            sftp.mkdir("./Desktop/mqtt_goyon/iotamak-core", ignore_existing=True)
            sftp.put_dir("/mnt/d/work/stage m1/iotamak-core", "./Desktop/mqtt_goyon/iotamak-core")
            sftp.close()


class MySFTPClient(paramiko.SFTPClient):
    def put_dir(self, source, target):
        ''' Uploads the contents of the source directory to the target path. The
            target directory needs to exists. All subdirectories in source are
            created under target.
        '''
        for item in os.listdir(source):
            if os.path.isfile(os.path.join(source, item)):
                self.put(os.path.join(source, item), '%s/%s' % (target, item))
                print(os.path.join(source, item))
            else:
                if any([i in item for i in [".git", "__pycache__", ".idea", ".vscode"]]):
                    pass
                else:
                    self.mkdir('%s/%s' % (target, item), ignore_existing=True)
                    self.put_dir(os.path.join(source, item), '%s/%s' % (target, item))
                    print(os.path.join(source, item))

    def mkdir(self, path, mode=511, ignore_existing=False):
        ''' Augments mkdir by adding an option to not fail if the folder exists  '''
        try:
            super(MySFTPClient, self).mkdir(path, mode)
        except IOError:
            if ignore_existing:
                pass
            else:
                raise