Skip to content

Commit

Permalink
Merge pull request #1658 from MVrachev/apply-linters
Browse files Browse the repository at this point in the history
New implementation tests: apply black and isort automatic linting fixes
  • Loading branch information
Jussi Kukkonen authored Nov 4, 2021
2 parents 589ed9e + 6fe36a0 commit ce4a60e
Show file tree
Hide file tree
Showing 9 changed files with 482 additions and 493 deletions.
32 changes: 19 additions & 13 deletions tests/repository_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,51 @@
updater.refresh()
"""

from collections import OrderedDict
from dataclasses import dataclass
from datetime import datetime, timedelta
import logging
import os
import tempfile
from collections import OrderedDict
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Dict, Iterator, List, Optional, Tuple
from urllib import parse

import securesystemslib.hash as sslib_hash
from securesystemslib.keys import generate_ed25519_key
from securesystemslib.signer import SSlibSigner
from typing import Dict, Iterator, List, Optional, Tuple
from urllib import parse

from tuf.api.metadata import TOP_LEVEL_ROLE_NAMES
from tuf.api.serialization.json import JSONSerializer
from tuf.exceptions import FetcherHTTPError
from tuf.api.metadata import (
SPECIFICATION_VERSION,
TOP_LEVEL_ROLE_NAMES,
DelegatedRole,
Delegations,
Key,
Metadata,
MetaFile,
Role,
Root,
SPECIFICATION_VERSION,
Snapshot,
TargetFile,
Targets,
Timestamp,
)
from tuf.api.serialization.json import JSONSerializer
from tuf.exceptions import FetcherHTTPError, RepositoryError
from tuf.ngclient.fetcher import FetcherInterface

logger = logging.getLogger(__name__)

SPEC_VER = ".".join(SPECIFICATION_VERSION)


@dataclass
class RepositoryTarget:
"""Contains actual target data and the related target metadata"""

data: bytes
target_file: TargetFile


class RepositorySimulator(FetcherInterface):
def __init__(self):
self.md_root: Metadata[Root] = None
Expand Down Expand Up @@ -141,7 +145,7 @@ def create_key() -> Tuple[Key, SSlibSigner]:
sslib_key = generate_ed25519_key()
return Key.from_securesystemslib_key(sslib_key), SSlibSigner(sslib_key)

def add_signer(self, role:str, signer: SSlibSigner):
def add_signer(self, role: str, signer: SSlibSigner):
if role not in self.signers:
self.signers[role] = {}
self.signers[role][signer.key_dict["keyid"]] = signer
Expand Down Expand Up @@ -197,7 +201,7 @@ def fetch(self, url: str) -> Iterator[bytes]:
elif path.startswith("/targets/"):
# figure out target path and hash prefix
target_path = path[len("/targets/") :]
dir_parts, sep , prefixed_filename = target_path.rpartition("/")
dir_parts, sep, prefixed_filename = target_path.rpartition("/")
prefix, _, filename = prefixed_filename.partition(".")
target_path = f"{dir_parts}{sep}{filename}"

Expand All @@ -219,7 +223,9 @@ def _fetch_target(self, target_path: str, hash: Optional[str]) -> bytes:
logger.debug("fetched target %s", target_path)
return repo_target.data

def _fetch_metadata(self, role: str, version: Optional[int] = None) -> bytes:
def _fetch_metadata(
self, role: str, version: Optional[int] = None
) -> bytes:
"""Return signed metadata for 'role', using 'version' if it is given.
If version is None, non-versioned metadata is being requested
Expand Down Expand Up @@ -264,7 +270,7 @@ def _compute_hashes_and_length(
data = self._fetch_metadata(role)
digest_object = sslib_hash.digest(sslib_hash.DEFAULT_HASH_ALGORITHM)
digest_object.update(data)
hashes = {sslib_hash.DEFAULT_HASH_ALGORITHM: digest_object.hexdigest()}
hashes = {sslib_hash.DEFAULT_HASH_ALGORITHM: digest_object.hexdigest()}
return hashes, len(data)

def update_timestamp(self):
Expand Down
Loading

0 comments on commit ce4a60e

Please sign in to comment.