# -*- coding: utf-8 -*-
import sys
import time
import uuid
import subprocess


def cmd_async(command: str, cwd: str = None) -> subprocess.Popen:
    """异步执行cmd命令，不阻塞等待返回结果，直接返回ps对象
    """

    # 如需以新进程组启动软件，可用参数creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
    ps = subprocess.Popen(command, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)
    return ps


if __name__ == '__main__':
    rclone = '"C:\\Program Files (x86)\\信创应用推送系统\\main\\rclone.exe"'
    ip = '172.16.100.18'
    mac = ''.join([uuid.UUID(int=uuid.getnode()).hex[-12:][e:e + 2] for e in range(0, 11, 2)])
    try:
        num = int(mac)
        cmd = '{} mount "本地磁盘:" Y: --webdav-url="http://{}:3000{}" --vfs-cache-mode writes --dir-cache-time 1s --vfs-write-back 2s --no-console'.format(rclone, ip, num)
        cmd_async(cmd)
        print('挂载微信分区(1/2): {}'.format(cmd))
        time.sleep(5)
        cmd = 'mklink /D "C:\\Users\\Administrator\\Documents\\WeChat Files" "Y:\\WeChat Files"'
        cmd_async(cmd)
        print('挂载微信分区(2/2): {}'.format(cmd))

    except Exception:
        print('主虚机不挂载微信分区')
