MCPcopy
hub / github.com/certbot/certbot / delete_files

Function delete_files

certbot/src/certbot/_internal/storage.py:358–422  ·  view source on GitHub ↗

Delete all files related to the certificate. If some files are not found, ignore them and continue.

(config: configuration.NamespaceConfig, certname: str)

Source from the content-addressed store, hash-verified

356
357
358def delete_files(config: configuration.NamespaceConfig, certname: str) -> None:
359 """Delete all files related to the certificate.
360
361 If some files are not found, ignore them and continue.
362 """
363 renewal_filename = renewal_file_for_certname(config, certname)
364 # file exists
365 full_default_archive_dir = full_archive_path(None, config, certname)
366 full_default_live_dir = _full_live_path(config, certname)
367 try:
368 renewal_config = configobj.ConfigObj(
369 renewal_filename, encoding='utf-8', default_encoding='utf-8')
370 except configobj.ConfigObjError:
371 # config is corrupted
372 logger.error("Could not parse %s. You may wish to manually "
373 "delete the contents of %s and %s.", renewal_filename,
374 full_default_live_dir, full_default_archive_dir)
375 raise errors.CertStorageError(
376 "error parsing {0}".format(renewal_filename))
377 finally:
378 # we couldn't read it, but let's at least delete it
379 # if this was going to fail, it already would have.
380 os.remove(renewal_filename)
381 logger.info("Removed %s", renewal_filename)
382
383 # cert files and (hopefully) live directory
384 # it's not guaranteed that the files are in our default storage
385 # structure. so, first delete the cert files.
386 directory_names = set()
387 for kind in ALL_FOUR:
388 link = renewal_config.get(kind)
389 try:
390 os.remove(link)
391 logger.debug("Removed %s", link)
392 except OSError:
393 logger.debug("Unable to delete %s", link)
394 directory = os.path.dirname(link)
395 directory_names.add(directory)
396
397 # if all four were in the same directory, and the only thing left
398 # is the README file (or nothing), delete that directory.
399 # this will be wrong in very few but some cases.
400 if len(directory_names) == 1:
401 # delete the README file
402 directory = directory_names.pop()
403 readme_path = os.path.join(directory, README)
404 try:
405 os.remove(readme_path)
406 logger.debug("Removed %s", readme_path)
407 except OSError:
408 logger.debug("Unable to delete %s", readme_path)
409 # if it's now empty, delete the directory
410 try:
411 os.rmdir(directory) # only removes empty directories
412 logger.debug("Removed %s", directory)
413 except OSError:
414 logger.debug("Unable to remove %s; may not be empty.", directory)
415

Callers

nothing calls this directly

Calls 8

full_archive_pathFunction · 0.85
_full_live_pathFunction · 0.85
formatMethod · 0.80
removeMethod · 0.80
popMethod · 0.80
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected