Exemplo n.º 1
0
    def _syncFile(self, srcFile, destFile, destRootPath, remote, trashbin):
        """比较并同步单个文件"""
        # 大小不同直接复制
        if os.stat(srcFile).st_size != os.stat(destFile).st_size:
            print(FC.r("[Diff] ") + srcFile.split('/')[-1])
            trashbin.moveToTrashbin(destFile, destRootPath)
            shutil.copy(srcFile, destFile)
            for r in remote:
                r.put(srcFile, destFile[len(destRootPath) + 1:])
            return

        with open(srcFile, 'rb') as f1, open(destFile, 'rb') as f2:
            while True:
                data1 = f1.read(4096)
                data2 = f2.read(4096)
                if data1 != data2:
                    trashbin.moveToTrashbin(destFile, destRootPath)
                    shutil.copy(srcFile, destFile)
                    for r in remote:
                        r.put(srcFile, destFile[len(destRootPath) + 1:])
                    print(FC.r("[Diff] ") + srcFile.split('/')[-1])
                    return
                elif data1 == b'':
                    print(FC.g("[Same] ") + srcFile.split('/')[-1])
                    return
Exemplo n.º 2
0
    def _syncFiles(self, curSrcPath, srcFiles, curDestPath, destFiles,
                   lastSyncTime, destRootPath, remote, trashbin):
        """同步curSrcPath文件夹的所有文件到目标文件夹同级的位置"""
        uselessFiles = destFiles.difference(srcFiles)
        newFiles = srcFiles.difference(destFiles)

        # 目标文件夹无用文件
        for f in uselessFiles:
            destPath = os.path.join(curDestPath, f)
            print(FC.r("[Del ] ") + f)
            trashbin.moveToTrashbin(destPath, destRootPath)
            for r in remote:
                r.rm(destPath[len(destRootPath) + 1:])

        # 新增文件
        for f in newFiles:
            srcPath = os.path.join(curSrcPath, f)
            destPath = os.path.join(curDestPath, f)
            print(FC.y("[New ] ") + f)
            shutil.copy(srcPath, destPath)
            for r in remote:
                r.put(srcPath, destPath[len(destRootPath) + 1:])

        # 都有的文件
        for f in destFiles.intersection(srcFiles):
            _src_f = os.path.join(curSrcPath, f)
            _dest_f = os.path.join(curDestPath, f)

            if lastSyncTime is not None and fileLastModifyTime(
                    _src_f) <= lastSyncTime:
                continue

            self._syncFile(_src_f, _dest_f, destRootPath, remote, trashbin)
Exemplo n.º 3
0
    def delRenference(self):
        self.display()

        print(FC.r('请输入要删除的序号:'))
        idx = input()

        try:
            _idx = int(idx)
            assert 0 <= _idx < len(self.folderRefernece), '序号超出范围'

            self._checkAndDeleteFormerPath(self.folderRefernece[_idx]['src'])
            for dest in self.folderRefernece[_idx]['dest']:
                self._checkAndDeleteFormerPath(dest['destPath'])
                self._checkAndDeleteFormerPath(
                    TrashManager.getDestRootPathInTrash(
                        self.trashPath, dest['destPath']))
                for remote in dest['remote']:
                    i = input("确认删除{}吗?[Y/n]".format(
                        remote['username'] + '@' + remote['host'] + ':' +
                        remote['rootPath'])).strip().upper()
                    if i != 'N':
                        try:
                            server = ServerTransporter(remote)
                            server.rm(remote['rootPath'])
                            print("remote文件夹已删除")
                        except:
                            print("删除失败")

            self.folderRefernece.pop(_idx)
            self._updateFolderReference()
            print("文件夹映射更新成功")
        except:
            print('序号不合规范')
Exemplo n.º 4
0
    def _checkAndDeleteFormerPath(self, path):
        """删除修改前(被弃用)的文件夹路径"""
        if not os.path.isdir(path):
            return
        while True:

            print(("是否删除被弃置的文件夹[" + FC.g("{}") + "]?[y/N]").format(path))
            check = input().strip()

            if check == '' or check.upper() == 'N':
                return
            elif check.upper() == 'Y':
                shutil.rmtree(path)

                print(FC.r("文件夹已删除"))
                return
Exemplo n.º 5
0
    def moveToTrashbin(self, srcPath, srcRootPath):
        assert os.path.exists(srcPath), "被移动文件不存在"

        relativePath = srcPath[len(srcRootPath) + 1:]
        path = os.path.join(self.rootPath, relativePath)
        l = path.split('/')
        folderPath = '/'.join(l[:-1])
        filename = l[-1]

        if os.path.exists(path):
            shutil.move(
                srcPath, "{}/{}_{}".format(folderPath,
                                           getStrfTime(self.DATETIME_PATTERN),
                                           filename))
        else:
            if not os.path.exists(folderPath):
                os.makedirs(folderPath)
            shutil.move(srcPath, path)

        print(FC.r("<Trash> ") + path)
Exemplo n.º 6
0
    def modifyRenference(self):
        self.display()

        idx = input(FC.r("请输入要修改的序号:"))
        try:
            _idx = int(idx)
            assert _idx < len(self.folderRefernece), '序号超出范围'

            print("\n" + FC.g("[1]") + "修改原路径")
            print(FC.g("[2]") + "修改目标路径")
            modifyField = int(input(FC.c("请输入修改项:")))

            # modifyField = int(input())
            if modifyField == 1:
                # 修改原路径
                print("请输入备份原文件夹路径:")
                srcPath = input()

                while not os.path.isdir(srcPath):
                    print("\n该地址不是有效的文件夹,请输入备份源文件夹路径:")
                    srcPath = input()

                if srcPath == self.folderRefernece[_idx]['src']:
                    print("文件夹未变动")

                assert self._checkSameInReference(srcPath,
                                                  'src') < 0, "路径已存在在当前备份中"
                assert self._checkSameInReference(
                    srcPath, 'dest') < 0, "还不支持映射中存在相同的原路径和目标路径"

                self._checkAndDeleteFormerPath(
                    self.folderRefernece[_idx]['src'])
                self.folderRefernece[_idx]['src'] = srcPath
                self._updateFolderReference()
                print("更新完毕")
            else:

                for idx, dest in enumerate(self.folderRefernece[_idx]['dest']):
                    print("\n" + FC.g("[{}]".format(idx)) + dest['destPath'])
                    for remote in dest['remote']:
                        print("     " + remote['username'] + '@' +
                              remote['host'] + ':' + remote['rootPath'])

                _destIdx = int(input("请输入要修改的目标路径编号:"))
                assert 0 <= _destIdx <= idx, "编号有误"

                idx = 0
                dest = self.folderRefernece[_idx]['dest'][_destIdx]
                print("\n" + FC.g("[{}]".format(idx)) + dest['destPath'])
                for remote in dest['remote']:
                    idx += 1
                    print(
                        FC.g("[{}]".format(idx)) + remote['username'] + '@' +
                        remote['host'] + ':' + remote['rootPath'])

                _destIdx = int(input("请输入要修改的目标路径编号:"))
                assert 0 <= _destIdx <= idx, "编号有误"

                if _destIdx == 0:
                    # 修改目标路径
                    _t = True
                    while _t:
                        print("\n请输入备份目标文件夹绝对路径:")
                        destPath = input()
                        if not os.path.isdir(destPath):
                            os.makedirs(destPath)
                            _t = False
                        else:
                            _i = input("该文件夹非空,使用则清空文件夹,确认使用吗?[y/N]")
                            if _i.strip() == '' or _i.strip().upper() == 'N':
                                continue
                            elif _i.upper() == 'Y':
                                _t = False
                            else:
                                print("输入有误")

                    if destPath != dest['destPath']:
                        assert self._checkSameInReference(
                            destPath, 'dest') < 0, "路径已存在在当前备份中"
                        assert self._checkSameInReference(
                            destPath, 'src') < 0, "还不支持映射中存在相同的原路径和目标路径"

                        trashPath = TrashManager.getDestRootPathInTrash(
                            self.trashPath, dest['destPath'])
                        self._checkAndDeleteFormerPath(dest['destPath'])
                        self._checkAndDeleteFormerPath(trashPath)
                        dest['destPath'] = destPath
                        self._updateFolderReference()
                        print("更新完毕")
                    else:
                        print("文件夹未变动")
                        return
                else:
                    # remote
                    print("修改远程配置尚未完成")

        except AssertionError as e:
            print(e)
        except:
            print('序号不合规范')