| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""The artifact reader objects.""" |
| 3 |
|
| 4 |
import abc |
| 5 |
import glob |
| 6 |
import io |
| 7 |
import os |
| 8 |
import json |
| 9 |
import yaml |
| 10 |
|
| 11 |
from artifacts import artifact |
| 12 |
from artifacts import definitions |
| 13 |
from artifacts import errors |
| 14 |
|
| 15 |
|
| 16 |
class BaseArtifactsReader(object): |
| 17 |
"""Artifacts reader interface. |
| 18 |
|
| 19 |
Attributes: |
| 20 |
supported_os (set[str]): supported operating systems. |
| 21 |
""" |
| 22 |
|
| 23 |
# Note that redundant-returns-doc and redundant-yields-doc are broken for |
| 24 |
# pylint 1.7.x for abstract methods. |
| 25 |
# pylint: disable=redundant-returns-doc,redundant-yields-doc |
| 26 |
|
| 27 |
def __init__(self): |
| 28 |
"""Initializes an artifacts reader.""" |
| 29 |
super(BaseArtifactsReader, self).__init__() |
| 30 |
self.supported_os = set() |
| 31 |
|
| 32 |
@abc.abstractmethod |
| 33 |
def ReadArtifactDefinitionValues(self, artifact_definition_values): |
| 34 |
"""Reads an artifact definition from a dictionary. |
| 35 |
|
| 36 |
Args: |
| 37 |
artifact_definition_values (dict[str, object]): artifact definition |
| 38 |
values. |
| 39 |
|
| 40 |
Returns: |
| 41 |
ArtifactDefinition: an artifact definition. |
| 42 |
|
| 43 |
Raises: |
| 44 |
FormatError: if the format of the artifact definition is not set |
| 45 |
or incorrect. |
| 46 |
""" |
| 47 |
|
| 48 |
@abc.abstractmethod |
| 49 |
def ReadDirectory(self, path, extension=None): |
| 50 |
"""Reads artifact definitions from a directory. |
| 51 |
|
| 52 |
This function does not recurse sub directories. |
| 53 |
|
| 54 |
Args: |
| 55 |
path (str): path of the directory to read from. |
| 56 |
extension (Optional[str]): extension of the filenames to read. |
| 57 |
|
| 58 |
Yields: |
| 59 |
ArtifactDefinition: an artifact definition. |
| 60 |
""" |
| 61 |
|
| 62 |
@abc.abstractmethod |
| 63 |
def ReadFile(self, filename): |
| 64 |
"""Reads artifact definitions from a file. |
| 65 |
|
| 66 |
Args: |
| 67 |
filename (str): name of the file to read from. |
| 68 |
|
| 69 |
Yields: |
| 70 |
ArtifactDefinition: an artifact definition. |
| 71 |
""" |
| 72 |
|
| 73 |
@abc.abstractmethod |
| 74 |
def ReadFileObject(self, file_object): |
| 75 |
"""Reads artifact definitions from a file-like object. |
| 76 |
|
| 77 |
Args: |
| 78 |
file_object (file): file-like object to read from. |
| 79 |
|
| 80 |
Yields: |
| 81 |
ArtifactDefinition: an artifact definition. |
| 82 |
|
| 83 |
Raises: |
| 84 |
FormatError: if the format of the artifact definition is not set |
| 85 |
or incorrect. |
| 86 |
""" |
| 87 |
|
| 88 |
|
| 89 |
class ArtifactsReader(BaseArtifactsReader): |
| 90 |
"""Artifacts reader common functionality.""" |
| 91 |
|
| 92 |
# Note that redundant-yields-doc is broken for pylint 1.7.x for |
| 93 |
# abstract methods. |
| 94 |
# pylint: disable=redundant-yields-doc |
| 95 |
|
| 96 |
def __init__(self): |
| 97 |
"""Initializes an artifacts reader.""" |
| 98 |
super(ArtifactsReader, self).__init__() |
| 99 |
self.supported_os = set(definitions.SUPPORTED_OS) |
| 100 |
|
| 101 |
# Pylint fails on detecting the type of definition_object based on |
| 102 |
# the docstring. |
| 103 |
# pylint: disable=missing-type-doc |
| 104 |
def _ReadSupportedOS(self, definition_values, definition_object, name): |
| 105 |
"""Reads the optional artifact or source type supported OS. |
| 106 |
|
| 107 |
Args: |
| 108 |
definition_values (dict[str, object]): artifact definition values. |
| 109 |
definition_object (ArtifactDefinition|SourceType): the definition object. |
| 110 |
name (str): name of the artifact definition. |
| 111 |
|
| 112 |
Raises: |
| 113 |
FormatError: if there are undefined supported operating systems. |
| 114 |
""" |
| 115 |
supported_os = definition_values.get('supported_os', []) |
| 116 |
if not isinstance(supported_os, list): |
| 117 |
supported_os_type = type(supported_os) |
| 118 |
raise errors.FormatError( |
| 119 |
f'Invalid supported_os type: {supported_os_type!s}') |
| 120 |
|
| 121 |
undefined_supported_os = set(supported_os).difference(self.supported_os) |
| 122 |
if undefined_supported_os: |
| 123 |
undefined_supported_os = ', '.join(undefined_supported_os) |
| 124 |
raise errors.FormatError(( |
| 125 |
f'Artifact definition: {name:s} undefined supported operating ' |
| 126 |
f'system: {undefined_supported_os:s}.')) |
| 127 |
|
| 128 |
definition_object.supported_os = supported_os |
| 129 |
|
| 130 |
def _ReadSources(self, artifact_definition_values, artifact_definition, name): |
| 131 |
"""Reads the artifact definition sources. |
| 132 |
|
| 133 |
Args: |
| 134 |
artifact_definition_values (dict[str, object]): artifact definition |
| 135 |
values. |
| 136 |
artifact_definition (ArtifactDefinition): an artifact definition. |
| 137 |
name (str): name of the artifact definition. |
| 138 |
|
| 139 |
Raises: |
| 140 |
FormatError: if the type indicator is not set or unsupported, |
| 141 |
or if required attributes are missing. |
| 142 |
""" |
| 143 |
sources = artifact_definition_values.get('sources') |
| 144 |
if not sources: |
| 145 |
raise errors.FormatError( |
| 146 |
f'Invalid artifact definition: {name:s} missing sources.') |
| 147 |
|
| 148 |
for source in sources: |
| 149 |
type_indicator = source.get('type', None) |
| 150 |
if not type_indicator: |
| 151 |
raise errors.FormatError( |
| 152 |
f'Invalid artifact definition: {name:s} source type.') |
| 153 |
|
| 154 |
attributes = source.get('attributes', None) |
| 155 |
|
| 156 |
try: |
| 157 |
source_type = artifact_definition.AppendSource( |
| 158 |
type_indicator, attributes) |
| 159 |
except errors.FormatError as exception: |
| 160 |
raise errors.FormatError( |
| 161 |
f'Invalid artifact definition: {name:s}, with error: {exception!s}') |
| 162 |
|
| 163 |
# TODO: deprecate these left overs from the collector definition. |
| 164 |
if source_type: |
| 165 |
if source.get('returned_types', None): |
| 166 |
raise errors.FormatError(( |
| 167 |
f'Invalid artifact definition: {name:s} returned_types no longer ' |
| 168 |
f'supported.')) |
| 169 |
|
| 170 |
self._ReadSupportedOS(source, source_type, name) |
| 171 |
if set(source_type.supported_os) - set( |
| 172 |
artifact_definition.supported_os): |
| 173 |
raise errors.FormatError(( |
| 174 |
f'Invalid artifact definition: {name:s} missing ' |
| 175 |
f'supported_os.')) |
| 176 |
|
| 177 |
def ReadArtifactDefinitionValues(self, artifact_definition_values): |
| 178 |
"""Reads an artifact definition from a dictionary. |
| 179 |
|
| 180 |
Args: |
| 181 |
artifact_definition_values (dict[str, object]): artifact definition |
| 182 |
values. |
| 183 |
|
| 184 |
Returns: |
| 185 |
ArtifactDefinition: an artifact definition. |
| 186 |
|
| 187 |
Raises: |
| 188 |
FormatError: if the format of the artifact definition is not set |
| 189 |
or incorrect. |
| 190 |
""" |
| 191 |
if not artifact_definition_values: |
| 192 |
raise errors.FormatError('Missing artifact definition values.') |
| 193 |
|
| 194 |
different_keys = ( |
| 195 |
set(artifact_definition_values) - definitions.TOP_LEVEL_KEYS) |
| 196 |
if different_keys: |
| 197 |
different_keys = ', '.join(different_keys) |
| 198 |
raise errors.FormatError(f'Undefined keys: {different_keys:s}') |
| 199 |
|
| 200 |
name = artifact_definition_values.get('name', None) |
| 201 |
if not name: |
| 202 |
raise errors.FormatError('Invalid artifact definition missing name.') |
| 203 |
|
| 204 |
# The description is assumed to be mandatory. |
| 205 |
description = artifact_definition_values.get('doc', None) |
| 206 |
if not description: |
| 207 |
raise errors.FormatError( |
| 208 |
f'Invalid artifact definition: {name:s} missing description.') |
| 209 |
|
| 210 |
aliases = artifact_definition_values.get('aliases', None) |
| 211 |
|
| 212 |
artifact_definition = artifact.ArtifactDefinition( |
| 213 |
name, aliases=aliases, description=description) |
| 214 |
|
| 215 |
if artifact_definition_values.get('collectors', []): |
| 216 |
raise errors.FormatError( |
| 217 |
f'Invalid artifact definition: {name:s} still uses collectors.') |
| 218 |
|
| 219 |
urls = artifact_definition_values.get('urls', []) |
| 220 |
if not isinstance(urls, list): |
| 221 |
raise errors.FormatError( |
| 222 |
f'Invalid artifact definition: {name:s} urls is not a list.') |
| 223 |
|
| 224 |
artifact_definition.provides = artifact_definition_values.get( |
| 225 |
'provides', []) |
| 226 |
self._ReadSupportedOS(artifact_definition_values, artifact_definition, name) |
| 227 |
artifact_definition.urls = urls |
| 228 |
self._ReadSources(artifact_definition_values, artifact_definition, name) |
| 229 |
|
| 230 |
return artifact_definition |
| 231 |
|
| 232 |
def ReadDirectory(self, path, extension='yaml'): |
| 233 |
"""Reads artifact definitions from a directory. |
| 234 |
|
| 235 |
This function does not recurse sub directories. |
| 236 |
|
| 237 |
Args: |
| 238 |
path (str): path of the directory to read from. |
| 239 |
extension (Optional[str]): extension of the filenames to read. |
| 240 |
|
| 241 |
Yields: |
| 242 |
ArtifactDefinition: an artifact definition. |
| 243 |
""" |
| 244 |
if extension: |
| 245 |
glob_spec = os.path.join(path, f'*.{extension:s}') |
| 246 |
else: |
| 247 |
glob_spec = os.path.join(path, '*') |
| 248 |
|
| 249 |
for artifact_file in glob.glob(glob_spec): |
| 250 |
for artifact_definition in self.ReadFile(artifact_file): |
| 251 |
yield artifact_definition |
| 252 |
|
| 253 |
def ReadFile(self, filename): |
| 254 |
"""Reads artifact definitions from a file. |
| 255 |
|
| 256 |
Args: |
| 257 |
filename (str): name of the file to read from. |
| 258 |
|
| 259 |
Yields: |
| 260 |
ArtifactDefinition: an artifact definition. |
| 261 |
""" |
| 262 |
with io.open(filename, 'r', encoding='utf-8') as file_object: |
| 263 |
for artifact_definition in self.ReadFileObject(file_object): |
| 264 |
yield artifact_definition |
| 265 |
|
| 266 |
@abc.abstractmethod |
| 267 |
def ReadFileObject(self, file_object): |
| 268 |
"""Reads artifact definitions from a file-like object. |
| 269 |
|
| 270 |
Args: |
| 271 |
file_object (file): file-like object to read from. |
| 272 |
|
| 273 |
Yields: |
| 274 |
ArtifactDefinition: an artifact definition. |
| 275 |
|
| 276 |
Raises: |
| 277 |
FormatError: if the format of the artifact definition is not set |
| 278 |
or incorrect. |
| 279 |
""" |
| 280 |
|
| 281 |
|
| 282 |
class JsonArtifactsReader(ArtifactsReader): |
| 283 |
"""JSON artifacts reader.""" |
| 284 |
|
| 285 |
def ReadFileObject(self, file_object): |
| 286 |
"""Reads artifact definitions from a file-like object. |
| 287 |
|
| 288 |
Args: |
| 289 |
file_object (file): file-like object to read from. |
| 290 |
|
| 291 |
Yields: |
| 292 |
ArtifactDefinition: an artifact definition. |
| 293 |
|
| 294 |
Raises: |
| 295 |
FormatError: if the format of the JSON artifact definition is not set |
| 296 |
or incorrect. |
| 297 |
""" |
| 298 |
# TODO: add try, except? |
| 299 |
json_definitions = json.loads(file_object.read()) |
| 300 |
|
| 301 |
last_artifact_definition = None |
| 302 |
for json_definition in json_definitions: |
| 303 |
try: |
| 304 |
artifact_definition = self.ReadArtifactDefinitionValues(json_definition) |
| 305 |
except errors.FormatError as exception: |
| 306 |
error_location = 'At start' |
| 307 |
if last_artifact_definition: |
| 308 |
error_location = f'After: {last_artifact_definition.name:s}' |
| 309 |
|
| 310 |
raise errors.FormatError(f'{error_location:s} {exception!s}') |
| 311 |
|
| 312 |
yield artifact_definition |
| 313 |
last_artifact_definition = artifact_definition |
| 314 |
|
| 315 |
|
| 316 |
class YamlArtifactsReader(ArtifactsReader): |
| 317 |
"""YAML artifacts reader.""" |
| 318 |
|
| 319 |
def ReadFileObject(self, file_object): |
| 320 |
"""Reads artifact definitions from a file-like object. |
| 321 |
|
| 322 |
Args: |
| 323 |
file_object (file): file-like object to read from. |
| 324 |
|
| 325 |
Yields: |
| 326 |
ArtifactDefinition: an artifact definition. |
| 327 |
|
| 328 |
Raises: |
| 329 |
FormatError: if the format of the YAML artifact definition is not set |
| 330 |
or incorrect. |
| 331 |
""" |
| 332 |
# TODO: add try, except? |
| 333 |
yaml_generator = yaml.safe_load_all(file_object) |
| 334 |
|
| 335 |
last_artifact_definition = None |
| 336 |
for yaml_definition in yaml_generator: |
| 337 |
try: |
| 338 |
artifact_definition = self.ReadArtifactDefinitionValues(yaml_definition) |
| 339 |
except errors.FormatError as exception: |
| 340 |
error_location = 'At start' |
| 341 |
if last_artifact_definition: |
| 342 |
error_location = f'After: {last_artifact_definition.name:s}' |
| 343 |
|
| 344 |
raise errors.FormatError(f'{error_location:s} {exception!s}') |
| 345 |
|
| 346 |
yield artifact_definition |
| 347 |
last_artifact_definition = artifact_definition |