Ejemplo n.º 1
0
def tele_ls():
    try:
        path = sys.argv[2]
        sub_path = __sub_path(path, False)
    except IndexError:
        sub_path = __sub_path('./', False)
    server_target = _choose_server_target()
    if server_target:
        from . import SshProtocol
        res = SshProtocol.command(server_target['user'], server_target['host'], server_target['path'], server_target['port'], f'ls -lah {sub_path}').strip().split('\n')[3:]
        res = [i.strip().split() for i in res]

        from rich.table import Table
        from rich.box import SIMPLE_HEAVY
        from rich.color import Color
        from rich.text import Text
        from rich.style import Style

        table = Table(title=f'{sub_path}', box=SIMPLE_HEAVY, show_header=True, header_style='bold magenta')
        table.add_column('permission' if user_lang != 'zh' else '权限码', justify='center')
        table.add_column('size' if user_lang != 'zh' else '尺寸', justify='center')
        table.add_column('owner' if user_lang != 'zh' else '拥有者', justify='center')
        table.add_column('time' if user_lang != 'zh' else '修改时间', justify='center')
        table.add_column('name' if user_lang != 'zh' else '文件名', justify='right')
        for i in res:
            is_dir = True if i[0][0] == 'd' else False
            color = Color.from_rgb(112, 87, 250) if is_dir else None
            p_color = Color.from_rgb(171, 157, 242)
            permission = ''.join(['1' if j != '-' else '0' for j in i[0][1:]])
            permission = '%d%d%d' % (int(permission[:3], 2), int(permission[3: 6], 2), int(permission[6:], 2))
            table.add_row(*[Text(permission, style=Style(color=p_color)), f'[green]{i[4]}', '[bold yellow]' + i[2], '[blue]' +  ' '.join(i[5:8]), Text(" ".join(i[8:]), style=Style(color=color, bold=is_dir))])
        QproDefaultConsole.print(table, justify='center')
Ejemplo n.º 2
0
def delete():
    try:
        path = os.path.abspath(sys.argv[2])
        sub_path = __sub_path(path)
    except IndexError:
        return QproDefaultConsole.print(
            QproWarnString, 'usage: Qpro del <path>' if user_lang != 'zh' else '使用: Qpro del <路径>'
        )
    else:
        if not sub_path:
            return \
                QproDefaultConsole.print(
                    QproErrorString,
                    (f"{path} is not in this Qpro project!" if os.path.exists(path) else f'No such file named: {path}')
                    if user_lang != 'zh' else
                    (f"{path} 不在当前 Qpro 项目中!" if os.path.exists(path) else f'该路径不存在: {path}')
                )
        server_targets = get_server_targets()
        st = 0
        for server_target in server_targets:
            _st = SshProtocol.command(server_target['user'], server_target['host'], server_target['path'],
                                      server_target['port'], f'rm -rf {sub_path}')
            st |= 1 if _st else 0
            if _st:
                QproDefaultConsole.print(QproErrorString, f'{server_target}: delete {sub_path} failed with error: {_st}')
        if not st or _ask({
            'type': 'confirm',
            'name': 'confirm',
            'message': f'{path} is not in this Qpro project! Do you want to delete it?' if user_lang != 'zh' else f'{path} 不在当前 Qpro 项目中! 是否删除?',
        }):
            remove(sub_path)
Ejemplo n.º 3
0
def scp(smv_flag: bool = False):
    kw = 'scp' if not smv_flag else 'smv'
    try:
        path = sys.argv[2]
        sub_path = __sub_path(path)
    except IndexError:
        return QproDefaultConsole.print(
            QproWarnString, f'usage: Qpro {kw} <path>' if user_lang != 'zh' else f'使用: Qpro {kw} <路径>'
        )
    else:
        if not sub_path:
            return \
                QproDefaultConsole.print(
                    QproErrorString,
                    (f"{path} is not in this Qpro project!" if os.path.exists(path) else f'No such file named: {path}')
                    if user_lang != 'zh' else
                    (f"{path} 不在当前 Qpro 项目中!" if os.path.exists(path) else f'该路径不存在: {path}')
                )
        server_targets = get_server_targets()
        status = 0
        for server_target in server_targets:
            if os.path.isdir(path):
                status |= SshProtocol.post_folder(server_target['user'], server_target['host'], server_target['path'], server_target['port'], path, sub_path)
            else:
                status |= SshProtocol.post_file(server_target['user'], server_target['host'], server_target['path'], server_target['port'], path, sub_path)
        if smv_flag and not status:
            remove(path)
Ejemplo n.º 4
0
def delete():
    try:
        path = os.path.abspath(sys.argv[sys.argv.index('-del') + 1])
        sub_path = __sub_path(path)
    except IndexError:
        return QproDefaultConsole.print(
            QproWarnString, 'usage: Qpro -del <path>'
            if user_lang != 'zh' else '使用: Qpro -del <路径>')
    else:
        if not sub_path:
            return \
                QproDefaultConsole.print(
                    QproErrorString,
                    (f"{path} is not in this Qpro project!" if os.path.exists(path) else f'No such file named: {path}')
                    if user_lang != 'zh' else
                    (f"{path} 不在当前 Qpro 项目中!" if os.path.exists(path) else f'该路径不存在: {path}')
                )
        config = get_config()
        if ':' in config['server_target'][0]:
            server, target, port = get_server_target()
            user, ip = server.split('@') if '@' in server else [None, server]
            st = SshProtocol.command(user, ip, target, port,
                                     f'rm -rf {sub_path}')
            # st = os.system("ssh -p %s %s 'rm -rf %s'" % (port, server, target + sub_path))
            if st:
                return
        remove(path)
Ejemplo n.º 5
0
def scp(smv_flag: bool = False):
    kw = '-scp' if not smv_flag else '-smv'
    try:
        path = sys.argv[sys.argv.index(kw) + 1]
        sub_path = __sub_path(path)
    except IndexError:
        return QproDefaultConsole.print(
            QproWarnString, f'usage: Qpro {kw} <path>'
            if user_lang != 'zh' else f'使用: Qpro {kw} <路径>')
    else:
        if not sub_path:
            return \
                QproDefaultConsole.print(
                    QproErrorString,
                    (f"{path} is not in this Qpro project!" if os.path.exists(path) else f'No such file named: {path}')
                    if user_lang != 'zh' else
                    (f"{path} 不在当前 Qpro 项目中!" if os.path.exists(path) else f'该路径不存在: {path}')
                )
        server, target, port = get_server_target()
        user, ip = server.split('@') if '@' in server else [None, server]
        if os.path.isdir(path):
            status = SshProtocol.post_folder(user, ip, target, port, path,
                                             sub_path)
        else:
            status = SshProtocol.post_file(user, ip, target, port, path,
                                           sub_path)
        if smv_flag and not status:
            remove(path)
Ejemplo n.º 6
0
def tele_ls():
    try:
        path = sys.argv[sys.argv.index('-ls') + 1]
        sub_path = __sub_path(path, False)
    except IndexError:
        sub_path = ''
    config = get_config()
    if ':' in config['server_target'][0]:
        from . import SshProtocol
        server, target, port = get_server_target()
        user, ip = server.split('@') if '@' in server else [None, server]
        SshProtocol.command(user, ip, target, port, f'ls {sub_path}')
Ejemplo n.º 7
0
def get():
    try:
        path = sys.argv[2]
        sub_path = __sub_path(path, isExist=False)
    except IndexError:
        return QproDefaultConsole.print(
            QproWarnString, 'usage: Qpro get <path>' if user_lang != 'zh' else '使用: Qpro get <路径>'
        )
    else:
        if not sub_path:
            return QproDefaultConsole.print(QproErrorString,
                                            f"{path} is not in this Qpro project!" if user_lang != 'zh' else f'{path} 不在当前 Qpro 项目中!')
        server_target = get_server_targets()[0]
        SshProtocol.get_file_or_folder(server_target['user'], server_target['host'], server_target['port'], server_target['path'], sub_path, path)
Ejemplo n.º 8
0
def get():
    try:
        path = sys.argv[sys.argv.index('-get') + 1]
        sub_path = __sub_path(path, isExist=False)
    except IndexError:
        return QproDefaultConsole.print(
            QproWarnString, 'usage: Qpro -get <path>'
            if user_lang != 'zh' else '使用: Qpro -get <路径>')
    else:
        if not sub_path:
            return QproDefaultConsole.print(
                QproErrorString, f"{path} is not in this Qpro project!"
                if user_lang != 'zh' else f'{path} 不在当前 Qpro 项目中!')
        server, target, port = get_server_target()
        user, ip = server.split('@') if '@' in server else [None, server]
        SshProtocol.get_file_or_folder(user, ip, target, port, sub_path, path)
Ejemplo n.º 9
0
def ssh():
    server_target = _choose_server_target()
    if not server_target:
        return
    SshProtocol.ssh(server_target['user'], server_target['host'], server_target['path'], server_target['port'], __sub_path('.'))