Example #1
0
    def sync(self):

        local_file_dir_info = LocalFileDirInfo(self.config.get_local_path())

        del_file_dict = self.db.file_stat_dict
        del_dir_dict = self.db.dir_stat_dict

        create_file_dict = {}
        create_dir_dict = {}

        for local_file_stat in local_file_dir_info.file_stat_list:
            local_file_path = local_file_stat.file_path

            if self.db.file_stat_dict.has_key(local_file_path):

                if FileStat.compare(local_file_stat, self.db.file_stat_dict[local_file_path]) == 0:
                    del self.db.file_stat_dict[local_file_path]
                else:
                    create_file_dict[local_file_path] = local_file_stat
            else:
                create_file_dict[local_file_path] = local_file_stat
        
        for local_dir_stat in local_file_dir_info.dir_stat_list:
            local_dir_path = local_dir_stat.file_path
            if self.db.dir_stat_dict.has_key(local_dir_path):
                del self.db.dir_stat_dict[local_dir_path]
            else:
                create_dir_dict[local_dir_path] = local_dir_stat

        appid = int(self.config.get_appid())
        secret_id = self.config.get_secret_id()
        secret_key = self.config.get_secret_key()
        bucket = self.config.get_bucket()
        timeout = self.config.get_timeout()
        cos_client = CosClient(appid, secret_id, secret_key)
        cos_config = CosConfig()
        cos_config.set_timeout(timeout)
        if self.config.get_enable_https() == 1:
            cos_config.enable_https()
        cos_client.set_config(cos_config)

        self._create_cos_target_dir(cos_client, bucket)
        self._del_cos_sync_file(cos_client, bucket, del_file_dict)
        self._del_cos_sync_dir(cos_client, bucket, del_dir_dict)
        self._create_cos_sync_dir(cos_client, bucket, create_dir_dict)
        self._create_cos_sync_file(cos_client, bucket, create_file_dict)
Example #2
0
    def sync(self):
        # 本地文件目录列表
        local_file_dir_info = LocalFileDirInfo(self.config.get_local_path())

        # 记录的要删除的文件和目录列表
        del_file_dict = self.db.file_stat_dict
        del_dir_dict = self.db.dir_stat_dict

        # 记录的要创建的文件和目录列表
        create_file_dict = {}
        create_dir_dict = {}

        # 文件同步状态校验
        for local_file_stat in local_file_dir_info.file_stat_list:
            local_file_path = local_file_stat.file_path
            # 如果数据库中记录cos中也存在, 则判断下文件是否发生了改变
            if (self.db.file_stat_dict.has_key(local_file_path)):
                # 如果文件内容未发生变化, 则不用同步, 否则进行同步
                if (FileStat.compare(
                        local_file_stat,
                        self.db.file_stat_dict[local_file_path]) == 0):
                    del self.db.file_stat_dict[local_file_path]
                else:
                    create_file_dict[local_file_path] = local_file_stat
            # 如果数据库中不存在相关记录, 则插入到要创建的文件列表
            else:
                create_file_dict[local_file_path] = local_file_stat

        # 目录同步状态校验
        for local_dir_stat in local_file_dir_info.dir_stat_list:
            local_dir_path = local_dir_stat.file_path
            # 如果数据库中记录cos中也存在, 则判断下目录是否发生了改变
            if (self.db.dir_stat_dict.has_key(local_dir_path)):
                del self.db.dir_stat_dict[local_dir_path]
            # 如果数据库中不存在相关记录, 则插入到要创建的文件列表
            else:
                create_dir_dict[local_dir_path] = local_dir_stat

        # 生成cos客户端
        appid = int(self.config.get_appid())
        secret_id = self.config.get_secret_id()
        secret_key = self.config.get_secret_key()
        bucket = self.config.get_bucket()
        timeout = self.config.get_timeout()
        cos_client = CosClient(appid, secret_id, secret_key)
        cos_config = CosConfig()
        cos_config.set_timeout(timeout)
        if self.config.get_enable_https() == 1:
            cos_config.enable_https()
        cos_client.set_config(cos_config)

        # 创建要同步的cos 目录
        self._create_cos_target_dir(cos_client, bucket)

        # 进行同步, 同步顺序如下
        # 1 删除文件(之前本地有,现在没有的文件, 或者发生修改的文件)
        self._del_cos_sync_file(cos_client, bucket, del_file_dict)

        # 2 删除目录(之前本地有,现在没有的目录)
        self._del_cos_sync_dir(cos_client, bucket, del_dir_dict)

        # 3 创建目录(新建的目录)
        self._create_cos_sync_dir(cos_client, bucket, create_dir_dict)

        # 4 创建文件(新建的文件)
        self._create_cos_sync_file(cos_client, bucket, create_file_dict)