MCPcopy Index your code
hub / github.com/HelloZeroNet/ZeroNet / sign

Method sign

src/Content/ContentManager.py:649–776  ·  view source on GitHub ↗
(self, inner_path="content.json", privatekey=None, filewrite=True, update_changed_files=False, extend=None, remove_missing_optional=False)

Source from the content-addressed store, hash-verified

647 # Create and sign a content.json
648 # Return: The new content if filewrite = False
649 def sign(self, inner_path="content.json", privatekey=None, filewrite=True, update_changed_files=False, extend=None, remove_missing_optional=False):
650 if not inner_path.endswith("content.json"):
651 raise SignError("Invalid file name, you can only sign content.json files")
652
653 if inner_path in self.contents:
654 content = self.contents.get(inner_path)
655 if content and content.get("cert_sign", False) is None and self.site.storage.isFile(inner_path):
656 # Recover cert_sign from file
657 content["cert_sign"] = self.site.storage.loadJson(inner_path).get("cert_sign")
658 else:
659 content = None
660 if not content: # Content not exist yet, load default one
661 self.log.info("File %s not exist yet, loading default values..." % inner_path)
662
663 if self.site.storage.isFile(inner_path):
664 content = self.site.storage.loadJson(inner_path)
665 if "files" not in content:
666 content["files"] = {}
667 if "signs" not in content:
668 content["signs"] = {}
669 else:
670 content = {"files": {}, "signs": {}} # Default content.json
671
672 if inner_path == "content.json": # It's the root content.json, add some more fields
673 content["title"] = "%s - ZeroNet_" % self.site.address
674 content["description"] = ""
675 content["signs_required"] = 1
676 content["ignore"] = ""
677
678 if extend:
679 # Add extend keys if not exists
680 for key, val in list(extend.items()):
681 if not content.get(key):
682 content[key] = val
683 self.log.info("Extending content.json with: %s" % key)
684
685 directory = helper.getDirname(self.site.storage.getPath(inner_path))
686 inner_directory = helper.getDirname(inner_path)
687 self.log.info("Opening site data directory: %s..." % directory)
688
689 changed_files = [inner_path]
690 files_node, files_optional_node = self.hashFiles(
691 helper.getDirname(inner_path), content.get("ignore"), content.get("optional")
692 )
693
694 if not remove_missing_optional:
695 for file_inner_path, file_details in content.get("files_optional", {}).items():
696 if file_inner_path not in files_optional_node:
697 files_optional_node[file_inner_path] = file_details
698
699 # Find changed files
700 files_merged = files_node.copy()
701 files_merged.update(files_optional_node)
702 for file_relative_path, file_details in files_merged.items():
703 old_hash = content.get("files", {}).get(file_relative_path, {}).get("sha512")
704 new_hash = files_merged[file_relative_path]["sha512"]
705 if old_hash != new_hash:
706 changed_files.append(inner_directory + file_relative_path)

Callers 1

signFunction · 0.45

Calls 13

hashFilesMethod · 0.95
getValidSignersMethod · 0.95
verifyContentMethod · 0.95
SignErrorClass · 0.85
loadJsonMethod · 0.80
itemsMethod · 0.80
getPathMethod · 0.80
writeJsonMethod · 0.80
getMethod · 0.45
isFileMethod · 0.45
copyMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected