MCPcopy
hub / github.com/HelloZeroNet/ZeroNet / deleteFiles

Method deleteFiles

src/Site/SiteStorage.py:493–547  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

491
492 # Delete site's all file
493 def deleteFiles(self):
494 self.log.debug("Deleting files from content.json...")
495 files = [] # Get filenames
496 for content_inner_path in list(self.site.content_manager.contents.keys()):
497 content = self.site.content_manager.contents.get(content_inner_path, {})
498 files.append(content_inner_path)
499 # Add normal files
500 for file_relative_path in list(content.get("files", {}).keys()):
501 file_inner_path = helper.getDirname(content_inner_path) + file_relative_path # Relative to site dir
502 files.append(file_inner_path)
503 # Add optional files
504 for file_relative_path in list(content.get("files_optional", {}).keys()):
505 file_inner_path = helper.getDirname(content_inner_path) + file_relative_path # Relative to site dir
506 files.append(file_inner_path)
507
508 if self.isFile("dbschema.json"):
509 self.log.debug("Deleting db file...")
510 self.closeDb()
511 self.has_db = False
512 try:
513 schema = self.loadJson("dbschema.json")
514 db_path = self.getPath(schema["db_file"])
515 if os.path.isfile(db_path):
516 os.unlink(db_path)
517 except Exception as err:
518 self.log.error("Db file delete error: %s" % err)
519
520 for inner_path in files:
521 path = self.getPath(inner_path)
522 if os.path.isfile(path):
523 for retry in range(5):
524 try:
525 os.unlink(path)
526 break
527 except Exception as err:
528 self.log.error("Error removing %s: %s, try #%s" % (inner_path, err, retry))
529 time.sleep(float(retry) / 10)
530 self.onUpdated(inner_path, False)
531
532 self.log.debug("Deleting empty dirs...")
533 for root, dirs, files in os.walk(self.directory, topdown=False):
534 for dir in dirs:
535 path = os.path.join(root, dir)
536 if os.path.isdir(path) and os.listdir(path) == []:
537 os.rmdir(path)
538 self.log.debug("Removing %s" % path)
539 if os.path.isdir(self.directory) and os.listdir(self.directory) == []:
540 os.rmdir(self.directory) # Remove sites directory if empty
541
542 if os.path.isdir(self.directory):
543 self.log.debug("Some unknown file remained in site data dir: %s..." % self.directory)
544 return False # Some files not deleted
545 else:
546 self.log.debug("Site data directory deleted: %s..." % self.directory)
547 return True # All clean

Callers 10

deleteMethod · 0.80
testCloneMethod · 0.80
cleanupFunction · 0.80
testRenameMethod · 0.80
testRenameOptionalMethod · 0.80
testArchivedDownloadMethod · 0.80
testOptionalDownloadMethod · 0.80
testFindOptionalMethod · 0.80
testUpdateMethod · 0.80

Calls 8

isFileMethod · 0.95
closeDbMethod · 0.95
loadJsonMethod · 0.95
getPathMethod · 0.95
onUpdatedMethod · 0.95
errorMethod · 0.80
getMethod · 0.45
walkMethod · 0.45

Tested by

no test coverage detected