Skip to content

Commit

Permalink
Added missing typings.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed May 14, 2022
1 parent def4771 commit 3e5f6b5
Show file tree
Hide file tree
Showing 117 changed files with 1,231 additions and 767 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
pipenv run flake8 .
- name: Run Type Checker
run: |
pipenv run mypy src/build_workflow tests/tests_build_workflow src/checkout_workflow tests/tests_checkout_workflow src/run_assemble.py tests/test_run_assemble.py src/assemble_workflow tests/tests_assemble_workflow src/manifests tests/tests_manifests src/paths tests/tests_paths src/system tests/tests_system src/ci_workflow tests/tests_ci_workflow src/manifests_workflow tests/tests_manifests_workflow
pipenv run mypy .
- name: Run Tests with Coverage
run: |
pipenv run coverage run -m pytest --cov=./src --cov-report=xml
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:
- id: mypy
stages: [commit]
name: mypy
entry: bash -c 'pipenv run mypy src/build_workflow tests/tests_build_workflow src/checkout_workflow tests/tests_checkout_workflow src/run_assemble.py tests/test_run_assemble.py src/assemble_workflow tests/tests_assemble_workflow src/manifests tests/tests_manifests src/paths tests/tests_paths src/system tests/tests_system src/ci_workflow tests/tests_ci_workflow src/manifests_workflow tests/tests_manifests_workflow'
entry: bash -c 'pipenv run mypy .'
language: system
- id: pytest
stages: [commit]
Expand Down
2 changes: 1 addition & 1 deletion src/build_workflow/build_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(
snapshot: bool = True,
build_id: str = None,
output_dir: str = "artifacts",
):
) -> None:
self.build_id = os.getenv("BUILD_NUMBER") or build_id or uuid.uuid4().hex
self.name = name
self.version = version
Expand Down
4 changes: 4 additions & 0 deletions src/build_workflow/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@


class Builder(ABC):
component: Any
target: BuildTarget
output_path: str

def __init__(self, component: Any, target: BuildTarget) -> None:
self.output_path = "builds"
self.component = component
Expand Down
1 change: 1 addition & 0 deletions src/manifests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ def __to_dict__(self) -> dict:

TestManifest.VERSIONS = {"1.0": TestManifest}

TestComponent.__test__ = False # type: ignore[attr-defined]
TestManifest.__test__ = False # type: ignore[attr-defined]
5 changes: 3 additions & 2 deletions src/run_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from system.temporary_directory import TemporaryDirectory


def main():
def main() -> int:
args = BuildArgs()
console.configure(level=args.logging_level)
manifest = InputManifest.from_file(args.manifest)
Expand All @@ -36,7 +36,7 @@ def main():
else:
logging.info(f"Creating {args.ref_manifest}")
manifest.to_file(args.ref_manifest)
exit(0)
return 0

output_dir = BuildOutputDir(manifest.build.filename, args.distribution).dir

Expand Down Expand Up @@ -74,6 +74,7 @@ def main():
build_recorder.write_manifest()

logging.info("Done.")
return 0


if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions src/run_bwc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from test_workflow.test_args import TestArgs


def main():
def main() -> int:
args = TestArgs()

# Any logging.info call preceding to next line in the execution chain will make the console output not displaying logs in console.
Expand All @@ -26,7 +26,9 @@ def main():
all_results.log()

if all_results.failed():
sys.exit(1)
return 1
else:
return 0


if __name__ == "__main__":
Expand Down
21 changes: 11 additions & 10 deletions src/run_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

from checkout_workflow.checkout_args import CheckoutArgs
from git.git_repository import GitRepository
from manifests.input_manifest import InputManifest
from manifests.input_manifest import InputComponentFromSource, InputManifest
from system import console
from system.temporary_directory import TemporaryDirectory


def main():
def main() -> int:
args = CheckoutArgs()
console.configure(level=args.logging_level)
manifest = InputManifest.from_file(args.manifest)
Expand All @@ -27,16 +27,17 @@ def main():

for component in manifest.components.select():
logging.info(f"Checking out {component.name}")

with GitRepository(
component.repository,
component.ref,
os.path.join(work_dir.name, component.name),
component.working_directory,
) as repo:
logging.debug(f"Checked out {component.name} into {repo.dir}")
if type(component) is InputComponentFromSource:
with GitRepository(
component.repository,
component.ref,
os.path.join(work_dir.name, component.name),
component.working_directory,
) as repo:
logging.debug(f"Checked out {component.name} into {repo.dir}")

logging.info(f"Done, checked out into {work_dir.name}.")
return 0


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions src/run_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from system import console


def main():
def main() -> int:
args = CiArgs()
console.configure(level=args.logging_level)

CiManifests.from_file(args.manifest, args).check()
return 0


if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions src/run_integ_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from test_workflow.test_args import TestArgs


def main():
def main() -> int:
args = TestArgs()

# Any logging.info call preceding to next line in the execution chain will make the console output not displaying logs in console.
Expand All @@ -26,7 +26,9 @@ def main():
all_results.log()

if all_results.failed():
sys.exit(1)
return 1
else:
return 0


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion src/run_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from system import console


def main():
def main() -> int:
args = ManifestsArgs()
console.configure(level=args.logging_level)

Expand All @@ -26,6 +26,7 @@ def main():
klass().update(keep=args.keep)

logging.info("Done.")
return 0


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion src/run_perf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
from test_workflow.perf_test.perf_test_runners import PerfTestRunners


def main():
def main() -> int:
"""
Entry point for Performance Test with bundle manifest, config file containing the required arguments for running
rally test and the stack name for the cluster. Will call out in test.sh with perf as argument
"""
perf_args = PerfArgs()
manifest = BundleManifest.from_file(perf_args.bundle_manifest)
PerfTestRunners.from_args(perf_args, manifest).run()
return 0


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion src/run_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
ACCEPTED_SIGNATURE_FILE_TYPES = [".sig"]


def main():
def main() -> int:
parser = argparse.ArgumentParser(description="Sign artifacts")
parser.add_argument("target", type=Path, help="Path to local manifest file or artifact directory.")
parser.add_argument("--component", nargs="?", help="Component name")
Expand All @@ -45,6 +45,7 @@ def main():
signer=Signer())

sign.sign()
return 0


if __name__ == "__main__":
Expand Down
33 changes: 19 additions & 14 deletions src/sign_workflow/sign_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,42 @@
import os
from abc import abstractmethod
from pathlib import Path
from typing import Any, List, Type

from manifests.build_manifest import BuildManifest
from sign_workflow.signer import Signer


class SignArtifacts:
def __init__(self, target: Path, component, artifact_type, signature_type, signer):
target: Path
component: str
artifact_type: str
signature_type: str
signer: Signer

def __init__(self, target: Path, component: str, artifact_type: str, signature_type: str, signer: Signer):
self.target = target
self.component = component
self.artifact_type = artifact_type
self.signature_type = signature_type
self.signer = signer

@abstractmethod
def __sign__(self):
def __sign__(self) -> None:
pass

def sign(self):
def sign(self) -> None:
self.__sign__()
logging.info("Done.")

def __sign_artifacts__(self, artifacts, basepath):
def __sign_artifacts__(self, artifacts: List[str], basepath: Path) -> None:
self.signer.sign_artifacts(artifacts, basepath, self.signature_type)

def __sign_artifact__(self, artifact, basepath):
def __sign_artifact__(self, artifact: str, basepath: Path) -> None:
self.signer.sign_artifact(artifact, basepath, self.signature_type)

@classmethod
def __signer_class__(self, path: Path):
def __signer_class__(self, path: Path) -> Type[Any]:
if path.is_dir():
return SignExistingArtifactsDir
elif path.suffix == ".yml":
Expand All @@ -46,14 +54,13 @@ def __signer_class__(self, path: Path):
return SignArtifactsExistingArtifactFile

@classmethod
def from_path(self, path: Path, component, artifact_type, signature_type, signer):
def from_path(self, path: Path, component: str, artifact_type: str, signature_type: str, signer: Signer) -> Any:
klass = self.__signer_class__(path)
return klass(path, component, artifact_type, signature_type, signer)


class SignWithBuildManifest(SignArtifacts):

def __sign__(self):
def __sign__(self) -> None:
manifest = BuildManifest.from_file(self.target.open("r"))
basepath = self.target.parent
for component in manifest.components.select(focus=self.component):
Expand All @@ -67,15 +74,13 @@ def __sign__(self):


class SignArtifactsExistingArtifactFile(SignArtifacts):

def __sign__(self):
def __sign__(self) -> None:
artifacts = self.target.name
basename = self.target.parent
super().__sign_artifact__(artifacts, basename)


class SignExistingArtifactsDir(SignArtifacts):

def __sign__(self):
def __sign__(self) -> None:
for subdir, dirs, files in os.walk(self.target):
super().__sign_artifacts__(files, subdir)
super().__sign_artifacts__(files, Path(subdir))
22 changes: 13 additions & 9 deletions src/sign_workflow/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import logging
import os
from pathlib import Path
from typing import List

from git.git_repository import GitRepository

Expand All @@ -18,47 +20,49 @@


class Signer:
git_repo: GitRepository

ACCEPTED_FILE_TYPES = [".zip", ".jar", ".war", ".pom", ".module", ".tar.gz", ".whl", ".crate", ".rpm"]

def __init__(self):
def __init__(self) -> None:
self.git_repo = GitRepository(self.get_repo_url(), "HEAD", working_subdirectory="src")
self.git_repo.execute("./bootstrap")
self.git_repo.execute("rm config.cfg")

def sign_artifact(self, artifact, basepath, signature_type):
def sign_artifact(self, artifact: str, basepath: Path, signature_type: str) -> None:
if not self.is_valid_file_type(artifact):
logging.info(f"Skipping signing of file {artifact}")
return
self.generate_signature_and_verify(artifact, basepath, signature_type)

def sign_artifacts(self, artifacts, basepath, signature_type):
def sign_artifacts(self, artifacts: List[str], basepath: Path, signature_type: str) -> None:
for artifact in artifacts:
if not self.is_valid_file_type(artifact):
logging.info(f"Skipping signing of file {artifact}")
continue
self.generate_signature_and_verify(artifact, basepath, signature_type)

def generate_signature_and_verify(self, artifact, basepath, signature_type):
def generate_signature_and_verify(self, artifact: str, basepath: Path, signature_type: str) -> None:
location = os.path.join(basepath, artifact)
self.sign(location, signature_type)
self.verify(location + signature_type)

def is_valid_file_type(self, file_name):
def is_valid_file_type(self, file_name: str) -> bool:
return any(
file_name.endswith(x) for x in Signer.ACCEPTED_FILE_TYPES
)

def get_repo_url(self):
def get_repo_url(self) -> str:
if "GITHUB_TOKEN" in os.environ:
return "https://${GITHUB_TOKEN}@github.com/opensearch-project/opensearch-signer-client.git"
return "https://github.com/opensearch-project/opensearch-signer-client.git"

def __remove_existing_signature__(self, signature_file):
def __remove_existing_signature__(self, signature_file: str) -> None:
if os.path.exists(signature_file):
logging.warning(f"Removing existing signature file {signature_file}")
os.remove(signature_file)

def sign(self, filename, signature_type):
def sign(self, filename: str, signature_type: str) -> None:
signature_file = filename + signature_type
self.__remove_existing_signature__(signature_file)
signing_cmd = [
Expand All @@ -72,6 +76,6 @@ def sign(self, filename, signature_type):
]
self.git_repo.execute(" ".join(signing_cmd))

def verify(self, filename):
def verify(self, filename: str) -> None:
verify_cmd = ["gpg", "--verify-files", filename]
self.git_repo.execute(" ".join(verify_cmd))
5 changes: 5 additions & 0 deletions src/system/temporary_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import shutil
import stat
import tempfile
from pathlib import Path
from types import FunctionType
from typing import Any

Expand All @@ -33,6 +34,10 @@ def __init__(self, keep: bool = False, chdir: bool = False):
else:
self.curdir = None

@property
def path(self) -> Path:
return Path(self.name)

def __enter__(self) -> 'TemporaryDirectory':
return self

Expand Down
Loading

0 comments on commit 3e5f6b5

Please sign in to comment.