Esempio n. 1
0
    def get_response_and_status_impl(self, request):
        file_path = request.file_path
        response = FileViewerRPCResponse()
        if FileUtil.is_file(path_name=file_path):
            file_name = FileUtil.die_if_file_not_exist(file_name=file_path)
            file_info = response.files_info.add()
            file_info.file_path = file_name
            file_info.file_size = FileUtil.get_file_size(file_name=file_name)
            file_info.modified_time = str(
                FileUtil.get_file_modified_time(file_name=file_name))
        else:
            dir_name = FileUtil.die_if_dir_not_exist(dir_name=file_path)
            sub_files = FileUtil.list_files_in_dir(dir_name=dir_name)
            for sub_file in sub_files:
                file_info = response.files_info.add()
                file_info.file_path = sub_file
                file_info.file_size = FileUtil.get_file_size(
                    file_name=sub_file)
                file_info.modified_time = str(
                    FileUtil.get_file_modified_time(file_name=sub_file))
            sub_dirs = FileUtil.list_dirs_in_dir(dir_name=dir_name)
            for sub_dir in sub_dirs:
                dirs_info = response.directories_info.add()
                dirs_info.file_path = sub_dir

        return response, Status.SUCCEEDED
Esempio n. 2
0
    def _delete_file(self, cur_time, path_name):
        num_file_removed, num_file_failed = 0, 0
        if FileUtil.is_file(path_name=path_name):
            ttl = FileUtil.get_ttl_from_path(path_name=path_name)
            if ttl and cur_time - FileUtil.get_file_modified_time(
                    file_name=path_name) > ttl:
                self._logger.info("Removing file " + path_name + '...')
                try:
                    with FileLockTool(protected_file_path=path_name,
                                      read_mode=True,
                                      timeout=TimeSleepObj.ONE_TENTH_SECOND):
                        FileUtil.remove_file(file_name=path_name)
                    num_file_removed += 1
                    self.counter_increment("num_file_removed")
                except Exception as err:
                    num_file_failed += 1
                    self.counter_increment("num_file_failed_to_be_removed")
                    self._logger.error("Removing file " + path_name +
                                       ' failed with err ' + str(err) + '.')
        else:
            for file_name in FileUtil.list_files_in_dir(dir_name=path_name):
                stats = self._delete_file(cur_time=cur_time,
                                          path_name=file_name)
                num_file_removed += stats[0]
                num_file_failed += stats[1]
            for dir_name in FileUtil.list_dirs_in_dir(dir_name=path_name):
                stats = self._delete_file(cur_time=cur_time,
                                          path_name=dir_name)
                num_file_removed += stats[0]
                num_file_failed += stats[1]

        return num_file_removed, num_file_failed
Esempio n. 3
0
 def get_response_and_status_impl(self, request):
     proto_file = FileUtil.die_if_file_not_exist(
         file_name=request.proto_file_path)
     message_type = ProtoUtil.infer_message_type_from_str(
         message_type_str=request.message_type,
         modules=request.proto_module if request.proto_module else None)
     response = ProtoViewerRPCResponse()
     proto_message = FileUtil.read_proto_from_file(proto_type=message_type,
                                                   file_name=proto_file)
     response.proto_content = ProtoUtil.message_to_text(
         proto_message=proto_message)
     file_info = FileInfo()
     file_info.file_path = request.proto_file_path
     file_info.file_size = FileUtil.get_file_size(file_name=proto_file)
     file_info.modified_time = str(
         FileUtil.get_file_modified_time(file_name=proto_file))
     response.file_info.CopyFrom(file_info)
     return response, Status.SUCCEEDED