Debian Codegraph @

.evergreen/legacy_config_generator/evergreen_config_generator/__init__.py

raw
1 # Copyright 2009-present MongoDB, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 from collections import OrderedDict as OD
17 from typing import Any, Iterable, Mapping, MutableMapping, MutableSequence, Sequence, Union
18
19 import yaml
20 import yamlloader
21
22 Scalar = Union[str, bool, int, None, float]
23 'YAML simple schema scalar types'
24 ValueSequence = Sequence['Value']
25 'Sequence of YAML simple values'
26 MutableValueArray = MutableSequence['Value']
27 'A mutable sequence of JSON values'
28 ValueMapping = Mapping[Scalar, 'Value']
29 'A YAML mapping type (arbitrary scalars as keys)'
30 MutableValueMapping = MutableMapping[Scalar, 'Value']
31 'A mutable YAML mapping type'
32 Value = Union[ValueSequence, ValueMapping, Scalar]
33 'Any YAML simple value'
34 MutableValue = Union[MutableValueMapping, MutableValueArray, Scalar]
35 'Any YAML simple value, which may be a mutable sequence or map'
36
37 ValueOrderedDict = OD[Scalar, Value]
38 'An OrderedDict of YAML values'
39
40
41 class ConfigObject(object):
42 @property
43 def name(self) -> str:
44 return 'UNSET'
45
46 def to_dict(self) -> Value:
47 return OD([('name', self.name)])
48
49
50 # We want legible YAML tasks:
51 #
52 # - name: debug-compile
53 # tags: [zlib, snappy, compression, openssl]
54 # commands:
55 # - command: shell.exec
56 # params:
57 # script: |-
58 # set -o errexit
59 # ...
60 #
61 # Write values compactly except multiline strings, which use "|" style. Write
62 # tag sets as lists.
63
64
65 class _Dumper(yamlloader.ordereddict.Dumper):
66 def __init__(self, *args: Value, **kwargs: Value):
67 super().__init__(*args, **kwargs) # type: ignore
68 self.add_representer(set, type(self).represent_set)
69 # Use "multi_representer" to represent all subclasses of ConfigObject.
70 self.add_multi_representer(ConfigObject, type(self).represent_config_object)
71
72 def represent_scalar(self, tag: str, value: Value, style: str | None = None) -> yaml.ScalarNode:
73 if isinstance(value, (str)) and '\n' in value:
74 style = '|'
75 return super().represent_scalar(tag, value, style) # type: ignore
76
77 def represent_set(self, data: Iterable[Value]) -> yaml.MappingNode:
78 return super().represent_list(sorted(set(data))) # type: ignore
79
80 def represent_config_object(self, obj: ConfigObject) -> yaml.Node:
81 d = obj.to_dict()
82 return super().represent_data(d) # type: ignore
83
84
85 def yaml_dump(obj: Any):
86 return yaml.dump(obj, Dumper=_Dumper, default_flow_style=False)
87
88
89 def generate(config: Any, path: str):
90 """Dump config to a file as YAML.
91 config is a dict, preferably an OrderedDict. path is a file path.
92 """
93 f = open(path, 'w+')
94 f.write(
95 """####################################
96 # Evergreen configuration
97 #
98 # Generated with evergreen_config_generator from
99 # github.com/mongodb-labs/drivers-evergreen-tools
100 #
101 # DO NOT EDIT THIS FILE
102 #
103 ####################################
104 """
105 )
106 f.write(yaml_dump(config))