Repository navigation
Create constructor(s) for creating Metadata from scratch #1459
Copy link
Copy link
Closed
Labels
backlogIssues to address with priority for current development goalsIssues to address with priority for current development goalsrepositoryRelated to the repository implementationRelated to the repository implementationtesting
Milestone
Description
Activity
- addedbacklogIssues to address with priority for current development goalsIssues to address with priority for current development goalsrepositoryRelated to the repository implementationRelated to the repository implementation
on Jun 22, 2021 To make this a bit more concrete: I have another PR that creates a root from scratch that looks like this:
root = Root(1, SPEC_VER, expiry, {}, {}, True) for role in ["root", "timestamp", "snapshot", "targets"]: key, signer = self.create_key() root.roles[role] = Role([], 1) root.add_key(role, key) # store role:signer elsewhere self.md_root = Metadata(root, OrderedDict())
Not pretty: it calls three constructors with at least one useless argument. It could look like
self.md_root = Metadata.new_root(expiry) for role in self.md_root.signed.roles.keys(): key, signer = self.create_key() self.md_root.signed.add_key(role, key) # store role:signer elsewhere
better? other ideas?
I have an alternative suggestion that would not add new API:
md_root = Metadata(Root(expires=expiry_date))
- support default
Nonefor constructor arguments where it makes sense: I think everything except expiry has a reasonable default (the meta dictionaries are debatable) - default values would not be used by deserialization code
- using default value means the constructor should choose a good initial value for new metadata
I think I like this. The only disadvantage is that it makes the constructor signatures even longer, but I can live with that.
I think creating a minimal initial repo would look like
targets = Metadata(Targets(expires=expiry_date)) snapshot = Metadata(Snapshot(expires=expiry_date)) timestamp = Metadata(Timestamp(expires=expiry_date)) root = Metadata(Root(expires=expiry_date)) for role in TOP_LEVEL_ROLE_NAMES: key, signer = create_key() root.signed.add_key(role, key) # TODO store signer # TODO: run snapshot/timestamp update
Reacted by Joshua Lock- support default
Nice. It's hard to argue with a one-liner!
Metadata
Metadata
Assignees
Labels
backlogIssues to address with priority for current development goalsIssues to address with priority for current development goalsrepositoryRelated to the repository implementationRelated to the repository implementationtesting
I think we should have method(s) that make creating new valid metadata from scratch easier as the current constructor has really been designed for the deserialization use case
These would be useful for testing but also will make any repository tools and tutorials better.
Back of envelope proposal with a single method:
Metadata.new_with_defaults(signed_type: Type, expires: datetime) -> MetadataThis would do roughly
This might require a bit of experimentation to find a reasonable solution