forked from instructlab/taxonomy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-yaml.py
executable file
·169 lines (159 loc) · 6.02 KB
/
check-yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Standard
import argparse
import glob
import os
import pathlib
import re
import sys
# Third Party
from instructlab.schema.taxonomy import TaxonomyParser
class CheckYaml:
def __init__(
self,
*,
yaml_files: list[pathlib.Path],
taxonomy_folders: list[str] | None = None,
yamllint_config: str | None = None,
schema_version: int | None = None,
message_format: str | None = None,
) -> None:
self.yaml_files = yaml_files
self.taxonomy_folders = taxonomy_folders
self.yamllint_config = yamllint_config
self.schema_version = schema_version
self.message_format = message_format
def check(self) -> int:
exit_code: int = 0
parser = TaxonomyParser(
taxonomy_folders=self.taxonomy_folders,
schema_version=self.schema_version,
message_format=self.message_format,
yamllint_config=self.yamllint_config,
)
for file in self.yaml_files:
taxonomy = parser.parse(file)
if taxonomy.version > 1:
attribution_path = taxonomy.rel_path.with_name("attribution.txt")
if not attribution_path.is_file():
taxonomy.error(
"The \"%s\" file does not exist or is not a file",
attribution_path.name,
)
elif os.path.getsize(attribution_path) == 0:
taxonomy.error(
"The \"%s\" file must be non-empty",
taxonomy.path.with_name(attribution_path.name),
)
# NOTE: The following three warnings are intended for the
# beta only, at the moment, and only to flag issues for
# maintainers to address rather than block on them. We will
# revisit when other content is allowed.
qna_file_path = taxonomy.rel_path.with_name("qna.yaml")
if "knowledge" in qna_file_path.parts:
qna_file_contents = parser.parse(qna_file_path).contents
for element in qna_file_contents["document"]["patterns"]:
if not re.match('.*.md', element):
taxonomy.warning(
"The document \"%s\" should be a markdown "
"file.",
element
)
if not re.match(
'https://github\.com\/.*',
qna_file_contents["document"]["repo"]):
taxonomy.warning(
"The document repo \"%s\" needs to be a "
"GitHub-based repository.",
qna_file_contents["document"]["repo"]
)
if not re.match(
'[0-9a-f]{40}',
qna_file_contents["document"]["commit"]):
taxonomy.warning(
"The document commit \"%s\" needs to be an "
"alphanumeric value that represents a commit. "
"\Please check with the reviewers for help.",
qna_file_contents["document"]["commit"]
)
if taxonomy.errors > 0:
exit_code = 1
if taxonomy.warnings > 0:
exit_code = 0
if not self.yaml_files:
print("No yaml files specified.")
return exit_code
def cli() -> int:
parser = argparse.ArgumentParser(
description="""
Check Taxonomy YAML files for linting and schema validation.
""",
)
parser.add_argument(
"-t",
"--taxonomy-folder",
action="append",
metavar="TAXONOMY_FOLDER",
dest="taxonomy_folders",
help="""
A taxonomy folder. This argument can be specified multiple times.
Alternately, the TAXONOMY_FOLDERS environment variable can be used
to specify a space-separated list of folders.
""",
default=os.environ.get("TAXONOMY_FOLDERS"),
)
parser.add_argument(
"-v",
"--schema-version",
help="""
The version of the Taxonomy schema.
Alternately, the SCHEMA_VERSION environment variable can be used
to specify the version.
Specifying a version less than 1 will use the schema version
specified by each YAML document's "version" key.
If not specified, the highest schema version is used.
""",
default=os.environ.get("SCHEMA_VERSION"),
type=int,
)
parser.add_argument(
"-l",
"--lint-config",
dest="yamllint_config",
help="""
The yamllint configuration data.
Alternately, the YAMLLINT_CONFIG environment variable can be used
to specify the configuration data.
""",
default=os.environ.get("YAMLLINT_CONFIG"),
)
parser.add_argument(
"-f",
"--format",
help="The message format.",
dest="message_format",
choices=["standard", "github", "auto"],
default=None,
)
parser.add_argument(
"yaml_file",
help="A qna.yaml file.",
nargs="*",
type=pathlib.Path,
)
args = parser.parse_args()
taxonomy_folders = args.taxonomy_folders
if isinstance(taxonomy_folders, str):
taxonomy_folders = taxonomy_folders.split()
check_yaml = CheckYaml(
yaml_files=args.yaml_file,
taxonomy_folders=taxonomy_folders,
yamllint_config=args.yamllint_config,
schema_version=args.schema_version,
message_format=args.message_format,
)
exit_code = check_yaml.check()
return exit_code
if __name__ == "__main__":
sys.exit(cli())