def test_get_possible_values(): values = ActionName.get_possible_values() assert values assert isinstance(values, list) for action_value in values: assert isinstance(action_value, str) assert "_" not in action_value
def get_from_dict(cls, raw_dict: dict, validate=True) -> "PackageConfig": if validate: cls.validate(raw_dict) synced_files = raw_dict.get("synced_files", None) actions = raw_dict.get("actions", {}) raw_jobs = raw_dict.get("jobs", []) create_tarball_command = raw_dict.get("create_tarball_command", None) current_version_command = raw_dict.get("current_version_command", None) upstream_project_name = cls.get_deprecated_key( raw_dict, "upstream_project_name", "upstream_name") upstream_project_url = raw_dict.get("upstream_project_url", None) if raw_dict.get("dist_git_url", None): logger.warning( "dist_git_url is no longer being processed, " "it is generated from dist_git_base_url and downstream_package_name" ) downstream_package_name = cls.get_deprecated_key( raw_dict, "downstream_package_name", "package_name") specfile_path = raw_dict.get("specfile_path", None) if not specfile_path: if downstream_package_name: specfile_path = f"{downstream_package_name}.spec" logger.info(f"We guess that spec file is at {specfile_path}") else: # guess it? logger.warning("Path to spec file is not set.") dist_git_base_url = raw_dict.get("dist_git_base_url", None) dist_git_namespace = raw_dict.get("dist_git_namespace", None) upstream_ref = nested_get(raw_dict, "upstream_ref") allowed_gpg_keys = raw_dict.get("allowed_gpg_keys", None) create_pr = raw_dict.get("create_pr", False) pc = PackageConfig( specfile_path=specfile_path, synced_files=SyncFilesConfig.get_from_dict(synced_files, validate=False), actions={ActionName(a): cmd for a, cmd in actions.items()}, jobs=[ JobConfig.get_from_dict(raw_job, validate=False) for raw_job in raw_jobs ], upstream_project_name=upstream_project_name, downstream_package_name=downstream_package_name, upstream_project_url=upstream_project_url, dist_git_base_url=dist_git_base_url, dist_git_namespace=dist_git_namespace, create_tarball_command=create_tarball_command, current_version_command=current_version_command, upstream_ref=upstream_ref, allowed_gpg_keys=allowed_gpg_keys, create_pr=create_pr, ) return pc
def validate_all_actions(self, actions: list) -> None: """ Validates all keys and raises exception with list of all invalid keys """ invalid_actions = [ action for action in actions if not ActionName.is_valid_action(action) ] if invalid_actions: raise ValidationError(f"Unknown action(s) provided: {invalid_actions}")
def _deserialize( self, value: Any, attr: Optional[str], data: Optional[Mapping[str, Any]], **kwargs, ) -> Dict: if not isinstance(value, dict): raise ValidationError(f"'dict' required, got {type(value)!r}.") self.validate_all_actions(actions=list(value)) return {ActionName(key): val for key, val in value.items()}
def _deserialize( self, value: Any, attr: Optional[str], data: Optional[Mapping[ActionName, Any]], **kwargs, ) -> Dict: if not isinstance(value, dict): raise ValidationError("Invalid data provided. dict required") self.validate_all_actions(actions=list(value)) data = {ActionName(key): val for key, val in value.items()} return data
}, "synced_files": { "type": "array", "items": SYNCED_FILES_SCHEMA }, "jobs": { "type": "array", "items": JOB_CONFIG_SCHEMA }, "actions": { "type": "object", "properties": {a: { "type": "string" } for a in ActionName.get_possible_values()}, "additionalProperties": False, }, }, "required": ["specfile_path"], } USER_CONFIG_SCHEMA = { "type": "object", "properties": { "debug": { "type": "boolean" }, "fas_user": { "type": "string" },
def get_from_dict(cls, raw_dict: dict, config_file_path: str = None, validate=True) -> "PackageConfig": if validate: cls.validate(raw_dict) synced_files = raw_dict.get("synced_files", None) actions = raw_dict.get("actions", {}) raw_jobs = raw_dict.get("jobs", []) create_tarball_command = raw_dict.get("create_tarball_command", None) current_version_command = raw_dict.get("current_version_command", None) upstream_package_name = cls.get_deprecated_key( raw_dict, "upstream_package_name", "upstream_project_name") or cls.get_deprecated_key( raw_dict, "upstream_package_name", "upstream_name") upstream_project_url = raw_dict.get("upstream_project_url", None) if raw_dict.get("dist_git_url", None): logger.warning( "dist_git_url is no longer being processed, " "it is generated from dist_git_base_url and downstream_package_name" ) downstream_package_name = cls.get_deprecated_key( raw_dict, "downstream_package_name", "package_name") specfile_path = raw_dict.get("specfile_path", None) if not specfile_path: if downstream_package_name: specfile_path = f"{downstream_package_name}.spec" logger.info(f"We guess that spec file is at {specfile_path}") else: # guess it? logger.warning("Path to spec file is not set.") dist_git_base_url = raw_dict.get("dist_git_base_url") dist_git_namespace = raw_dict.get("dist_git_namespace") upstream_ref = raw_dict.get("upstream_ref") allowed_gpg_keys = raw_dict.get("allowed_gpg_keys") create_pr = raw_dict.get("create_pr", False) upstream_tag_template = raw_dict.get("upstream_tag_template", "{version}") # it can be int as well spec_source_id = raw_dict.get("spec_source_id", "Source0") try: spec_source_id = int(spec_source_id) except ValueError: # not a number pass else: # is a number! spec_source_id = f"Source{spec_source_id}" pc = PackageConfig( config_file_path=config_file_path, specfile_path=specfile_path, synced_files=SyncFilesConfig.get_from_dict(synced_files, validate=False), actions={ActionName(a): cmd for a, cmd in actions.items()}, jobs=[ JobConfig.get_from_dict(raw_job, validate=False) for raw_job in raw_jobs ], upstream_package_name=upstream_package_name, downstream_package_name=downstream_package_name, upstream_project_url=upstream_project_url, dist_git_base_url=dist_git_base_url, dist_git_namespace=dist_git_namespace, create_tarball_command=create_tarball_command, current_version_command=current_version_command, upstream_ref=upstream_ref, allowed_gpg_keys=allowed_gpg_keys, create_pr=create_pr, spec_source_id=spec_source_id, upstream_tag_template=upstream_tag_template, ) return pc
def test_get_action_from_name(action_name, result): assert ActionName.get_action_from_name(action_name) == result
def test_is_valid(action, valid): assert ActionName.is_valid_action(action) == valid