|
| 1 | + |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from multistage import errors |
| 7 | + |
| 8 | + |
| 9 | +class BuildConfigEntry: |
| 10 | + def __init__(self, name: str, go_url: str, source_url: str, destination_folder: str) -> None: |
| 11 | + if not name: |
| 12 | + raise errors.KnownError("build 'name' is required") |
| 13 | + if not go_url: |
| 14 | + raise errors.KnownError("build 'go url' is required") |
| 15 | + if not source_url: |
| 16 | + raise errors.KnownError("build 'source' is required") |
| 17 | + if not destination_folder: |
| 18 | + raise errors.KnownError("build 'destination' is required") |
| 19 | + |
| 20 | + self.name = name |
| 21 | + self.go_url = go_url |
| 22 | + self.source_url = source_url |
| 23 | + self.destination_folder = destination_folder |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def new_from_dictionary(cls, data: dict[str, Any]): |
| 27 | + name = data.get("name") or "" |
| 28 | + go_url = data.get("goUrl") or "" |
| 29 | + source_url = data.get("sourceUrl") or "" |
| 30 | + destination_folder = data.get("destinationFolder") or "" |
| 31 | + |
| 32 | + return cls( |
| 33 | + name=name, |
| 34 | + go_url=go_url, |
| 35 | + source_url=source_url, |
| 36 | + destination_folder=destination_folder, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +class DriverConfig: |
| 41 | + def __init__(self, lanes: list["LaneConfig"]) -> None: |
| 42 | + lanes_names = [lane.name for lane in lanes] |
| 43 | + |
| 44 | + if not lanes: |
| 45 | + raise errors.BadConfigurationError("'lanes' are required") |
| 46 | + if len(lanes_names) > len(set(lanes_names)): |
| 47 | + raise errors.BadConfigurationError("lanes names must be unique") |
| 48 | + |
| 49 | + self.lanes = lanes |
| 50 | + self.lanes_by_name = {lane.name: lane for lane in lanes} |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def new_from_dictionary(cls, data: dict[str, Any]): |
| 54 | + lanes_records = data.get("lanes") or [] |
| 55 | + lanes = [LaneConfig.new_from_dictionary(record) for record in lanes_records] |
| 56 | + |
| 57 | + return cls( |
| 58 | + lanes=lanes, |
| 59 | + ) |
| 60 | + |
| 61 | + def get_lanes_names(self) -> list[str]: |
| 62 | + return [lane.name for lane in self.lanes] |
| 63 | + |
| 64 | + def get_lane(self, name: str) -> "LaneConfig": |
| 65 | + return self.lanes_by_name[name] |
| 66 | + |
| 67 | + |
| 68 | +class LaneConfig: |
| 69 | + def __init__(self, name: str, working_directory: str, stages: list["StageConfig"]) -> None: |
| 70 | + stages_names = [stage.name for stage in stages] |
| 71 | + |
| 72 | + if not name: |
| 73 | + raise errors.BadConfigurationError("for all lanes, 'name' is required") |
| 74 | + if not working_directory: |
| 75 | + raise errors.BadConfigurationError(f"for lane {name}, 'working directory' is required") |
| 76 | + if not stages: |
| 77 | + raise errors.BadConfigurationError(f"for lane {name}, 'stages' are required") |
| 78 | + if len(stages) > len(set(stages_names)): |
| 79 | + raise errors.BadConfigurationError("stages names must be unique") |
| 80 | + |
| 81 | + self.name = name |
| 82 | + self.working_directory = Path(working_directory).expanduser().resolve() |
| 83 | + self.stages = stages |
| 84 | + self.stages_by_name = {stage.name: stage for stage in stages} |
| 85 | + |
| 86 | + @classmethod |
| 87 | + def new_from_dictionary(cls, data: dict[str, Any]): |
| 88 | + name = data.get("name") or "" |
| 89 | + working_directory = data.get("workingDirectory") or "" |
| 90 | + stages_records = data.get("stages") or [] |
| 91 | + stages = [StageConfig.new_from_dictionary(record) for record in stages_records] |
| 92 | + |
| 93 | + return cls( |
| 94 | + name=name, |
| 95 | + working_directory=working_directory, |
| 96 | + stages=stages, |
| 97 | + ) |
| 98 | + |
| 99 | + def get_stages_names(self) -> list[str]: |
| 100 | + return [stage.name for stage in self.stages] |
| 101 | + |
| 102 | + def get_stages_including_and_after(self, initial_stage_name: str) -> list["StageConfig"]: |
| 103 | + stages_names = self.get_stages_names() |
| 104 | + index_of_initial_stage_name = stages_names.index(initial_stage_name) |
| 105 | + return self.stages[index_of_initial_stage_name:] |
| 106 | + |
| 107 | + |
| 108 | +class StageConfig: |
| 109 | + def __init__(self, |
| 110 | + name: str, |
| 111 | + until_epoch: int, |
| 112 | + node_status_url: str, |
| 113 | + configuration_archive: str, |
| 114 | + bin: str, |
| 115 | + node_arguments: list[str], |
| 116 | + with_db_lookup_extensions: bool, |
| 117 | + with_indexing: bool) -> None: |
| 118 | + if not name: |
| 119 | + raise errors.BadConfigurationError("for all stages, 'name' is required") |
| 120 | + if not until_epoch: |
| 121 | + raise errors.BadConfigurationError(f"for stage {name}, 'until epoch' is required") |
| 122 | + if not node_status_url: |
| 123 | + raise errors.BadConfigurationError(f"for stage {name}, 'node status url' is required") |
| 124 | + if not configuration_archive: |
| 125 | + raise errors.BadConfigurationError(f"for stage {name}, 'configuration archive' is required") |
| 126 | + if not bin: |
| 127 | + raise errors.BadConfigurationError(f"for stage {name}, 'bin' is required") |
| 128 | + |
| 129 | + self.name = name |
| 130 | + self.until_epoch = until_epoch |
| 131 | + self.node_status_url = node_status_url |
| 132 | + self.configuration_archive = configuration_archive |
| 133 | + self.bin = Path(bin).expanduser().resolve() |
| 134 | + self.node_arguments = node_arguments |
| 135 | + self.with_db_lookup_extensions = with_db_lookup_extensions |
| 136 | + self.with_indexing = with_indexing |
| 137 | + |
| 138 | + @classmethod |
| 139 | + def new_from_dictionary(cls, data: dict[str, Any]): |
| 140 | + name = data.get("name") or "" |
| 141 | + until_epoch = data.get("untilEpoch") or 0 |
| 142 | + node_status_url = data.get("nodeStatusUrl") or "" |
| 143 | + configuration_archive = data.get("configurationArchive") or "" |
| 144 | + bin = data.get("bin") or "" |
| 145 | + node_arguments = data.get("nodeArguments") or [] |
| 146 | + with_db_lookup_extensions = data.get("withDbLookupExtensions") or False |
| 147 | + with_indexing = data.get("withIndexing") or False |
| 148 | + |
| 149 | + return cls( |
| 150 | + name=name, |
| 151 | + until_epoch=until_epoch, |
| 152 | + node_status_url=node_status_url, |
| 153 | + configuration_archive=configuration_archive, |
| 154 | + bin=bin, |
| 155 | + node_arguments=node_arguments, |
| 156 | + with_db_lookup_extensions=with_db_lookup_extensions, |
| 157 | + with_indexing=with_indexing, |
| 158 | + ) |
0 commit comments