Beispiel #1
0
def test_save_bytes_to_file(tmp_path, path_type, data):
    path = path_type(tmp_path / "lala")
    store.save_bytes_to_file(path, data)
    assert store.load_bytes_from_file(path) == data
def _load_history_dump() -> LicenseUsageHistoryDump:
    raw_history_dump = store.load_bytes_from_file(
        history_filepath,
        default=b'{}',
    )
    return LicenseUsageHistoryDump.deserialize(raw_history_dump)
Beispiel #3
0
def get_piggyback_raw_data(
    piggybacked_hostname: Optional[HostName],
    time_settings: PiggybackTimeSettings,
) -> Sequence[PiggybackRawDataInfo]:
    """Returns the usable piggyback data for the given host

    A list of two element tuples where the first element is
    the source host name and the second element is the raw
    piggyback data (byte string)
    """
    if not piggybacked_hostname:
        return []

    piggyback_file_infos = _get_piggyback_processed_file_infos(
        piggybacked_hostname, time_settings)
    if not piggyback_file_infos:
        logger.log(
            VERBOSE,
            "No piggyback files for '%s'. Skip processing.",
            piggybacked_hostname,
        )
        return []

    piggyback_data = []
    for file_info in piggyback_file_infos:
        try:
            # Raw data is always stored as bytes. Later the content is
            # converted to unicode in abstact.py:_parse_info which respects
            # 'encoding' in section options.
            raw_data = AgentRawData(
                store.load_bytes_from_file(file_info.file_path))

        except IOError as e:
            reason = "Cannot read piggyback raw data from source '%s'" % file_info.source_hostname
            piggyback_raw_data = PiggybackRawDataInfo(
                source_hostname=file_info.source_hostname,
                file_path=str(file_info.file_path),
                successfully_processed=False,
                reason=reason,
                reason_status=0,
                raw_data=AgentRawData(b''),
            )
            logger.log(
                VERBOSE,
                "Piggyback file '%s': %s, %s",
                file_info.file_path,
                reason,
                e,
            )

        else:
            piggyback_raw_data = PiggybackRawDataInfo(
                file_info.source_hostname,
                str(file_info.file_path),
                file_info.successfully_processed,
                file_info.reason,
                file_info.reason_status,
                raw_data,
            )
            if file_info.successfully_processed:
                logger.log(
                    VERBOSE,
                    "Piggyback file '%s': %s",
                    file_info.file_path,
                    file_info.reason,
                )
            else:
                logger.log(
                    VERBOSE,
                    "Piggyback file '%s' is outdated (%s). Skip processing.",
                    file_info.file_path,
                    file_info.reason,
                )
        piggyback_data.append(piggyback_raw_data)
    return piggyback_data
Beispiel #4
0
 def _load_data(self, filepath) -> Dict:
     return pickle.loads(store.load_bytes_from_file(filepath))