def store_piggyback_raw_data(source_hostname, piggybacked_raw_data): # type: (str, Dict[str, List[bytes]]) -> None piggyback_file_paths = [] for piggybacked_hostname, lines in piggybacked_raw_data.items(): piggyback_file_path = _get_piggybacked_file_path( source_hostname, piggybacked_hostname) logger.log( VERBOSE, "Storing piggyback data for: %s", piggybacked_hostname, ) # 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. store.save_bytes_to_file(piggyback_file_path, b"%s\n" % b"\n".join(lines)) piggyback_file_paths.append(piggyback_file_path) # Store the last contact with this piggyback source to be able to filter outdated data later # We use the mtime of this file later for comparison. # Only do this for hosts that sent piggyback data this turn, cleanup the status file when no # piggyback data was sent this turn. if piggybacked_raw_data: status_file_path = _get_source_status_file_path(source_hostname) _store_status_file_of(status_file_path, piggyback_file_paths) else: remove_source_status_file(source_hostname)
def save(stored_passwords: Mapping[str, str]) -> None: """Save the passwords to the pre-activation path""" content = "" for ident, pw in stored_passwords.items(): content += "%s:%s\n" % (ident, pw) store.save_bytes_to_file(password_store_path(), _obfuscate(content))
def save_extensions(extensions: LicenseUsageExtensions) -> None: license_usage_dir.mkdir(parents=True, exist_ok=True) extensions_filepath = _get_extensions_filepath() with store.locked(extensions_filepath): store.save_bytes_to_file(extensions_filepath, _serialize_dump(extensions.for_report()))
def recreate_openapi_spec(mocker): from cmk.gui.plugins.openapi import specgen spec_path = paths.omd_root + "/share/checkmk/web/htdocs/openapi" openapi_spec_dir = mocker.patch('cmk.gui.wsgi.routing.openapi_spec_dir') openapi_spec_dir.return_value = spec_path store.save_bytes_to_file(spec_path + "/checkmk.yaml", specgen.generate()) yield
def _update_trusted_cas(self, current_config): trusted_cas: List[bytes] = [] errors: List[str] = [] if current_config["use_system_wide_cas"]: trusted, errors = self._get_system_wide_trusted_ca_certificates() trusted_cas += trusted trusted_cas += [ensure_binary(e) for e in current_config["trusted_cas"]] store.save_bytes_to_file(self.trusted_cas_file, b"\n".join(trusted_cas)) return errors
def save_to(self, path: str, filename: str, pretty: bool = False) -> None: filepath = "%s/%s" % (path, filename) output = self.get_raw_tree() store.save_object_to_file(filepath, output, pretty=pretty) buf = io.BytesIO() with gzip.GzipFile(fileobj=buf, mode="wb") as f: f.write((repr(output) + "\n").encode("utf-8")) store.save_bytes_to_file(filepath + ".gz", buf.getvalue()) # Inform Livestatus about the latest inventory update store.save_text_to_file("%s/.last" % path, u"")
def recreate_openapi_spec(mocker, _cache=[]): # pylint: disable=dangerous-default-value from cmk.gui.plugins.openapi.specgen import generate spec_path = paths.omd_root + "/share/checkmk/web/htdocs/openapi" openapi_spec_dir = mocker.patch('cmk.gui.wsgi.applications.rest_api') openapi_spec_dir.return_value = spec_path if not _cache: with SPEC_LOCK: if not _cache: _cache.append(generate()) spec_data = _cache[0] store.save_bytes_to_file(spec_path + "/checkmk.yaml", spec_data)
def write(self, raw_data: TRawData, mode: Mode) -> None: if not self._do_cache(mode): return path = self.make_path(mode) try: path.parent.mkdir(parents=True, exist_ok=True) except Exception as e: raise MKGeneralException("Cannot create directory %r: %s" % (path.parent, e)) self._logger.debug("Write data to cache file %s", path) try: _store.save_bytes_to_file(path, self._to_cache_file(raw_data)) except Exception as e: raise MKGeneralException("Cannot write cache file %s: %s" % (path, e))
def _try_history_update() -> None: logger.debug("Try license usage history update.") license_usage_dir.mkdir(parents=True, exist_ok=True) with store.locked(next_run_filepath), store.locked(history_filepath): now = datetime.now() next_run_ts = int(rot47(store.load_text_from_file(next_run_filepath, default="_"))) if not _may_update(now.timestamp(), next_run_ts): return history_dump = _create_or_update_history_dump() store.save_bytes_to_file(history_filepath, history_dump.serialize()) store.save_text_to_file(next_run_filepath, rot47(str(_create_next_run_ts(now)))) logger.debug("Successfully updated history.")
def save(self, crash: 'ABCCrashReport') -> None: """Save the crash report instance to it's crash report directory""" self._prepare_crash_dump_directory(crash) for key, value in crash.serialize().items(): fname = "crash.info" if key == "crash_info" else key if value is None: continue if fname == "crash.info": store.save_text_to_file(crash.crash_dir() / fname, str(json.dumps(value, cls=RobustJSONEncoder)) + "\n") else: store.save_bytes_to_file(crash.crash_dir() / fname, value) self._cleanup_old_crashes(crash.crash_dir().parent)
def _save_data(self, filepath: Path, data) -> None: store.save_bytes_to_file(filepath, pickle.dumps(data))
def test_save_bytes_to_file_unicode(tmp_path, path_type, data): path = path_type(tmp_path / "lala") with pytest.raises(TypeError) as e: store.save_bytes_to_file(path, data) assert "content argument must be bytes, not Text" in "%s" % e
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 save_history_dump(history_dump: LicenseUsageHistoryDump) -> None: history_dump_filepath = get_history_dump_filepath() store.save_bytes_to_file(history_dump_filepath, _serialize_dump(history_dump.for_report()))