forked from elastic/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
96 lines (78 loc) · 3.34 KB
/
generator.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
import argparse
import glob
import os
import yaml
import time
from generators import asciidoc_fields
from generators import beats
from generators import csv_generator
from generators import es_template
from generators import ecs_helpers
from generators import intermediate_files
from schema import loader
from schema import cleaner
from schema import finalizer
from schema import subset_filter
def main():
args = argument_parser()
ecs_version = read_version(args.ref)
print('Running generator. ECS version ' + ecs_version)
# default location to save files
out_dir = 'generated'
docs_dir = 'docs'
if args.out:
default_dirs = False
out_dir = os.path.join(args.out, out_dir)
docs_dir = os.path.join(args.out, docs_dir)
else:
default_dirs = True
ecs_helpers.make_dirs(out_dir)
ecs_helpers.make_dirs(docs_dir)
# To debug issues in the gradual building up of the nested structure, insert
# statements like this after any step of interest.
# ecs_helpers.yaml_dump('ecs.yml', fields)
fields = loader.load_schemas(ref=args.ref, included_files=args.include)
cleaner.clean(fields, strict=args.strict)
finalizer.finalize(fields)
fields = subset_filter.filter(fields, args.subset, out_dir)
nested, flat = intermediate_files.generate(fields, os.path.join(out_dir, 'ecs'), default_dirs)
if args.intermediate_only:
exit()
csv_generator.generate(flat, ecs_version, out_dir)
es_template.generate(flat, ecs_version, out_dir, args.template_settings, args.mapping_settings)
beats.generate(nested, ecs_version, out_dir)
if args.include or args.subset:
exit()
asciidoc_fields.generate(nested, ecs_version, docs_dir)
def argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--intermediate-only', action='store_true',
help='generate intermediary files only')
parser.add_argument('--include', nargs='+',
help='include user specified directory of custom field definitions')
parser.add_argument('--subset', nargs='+',
help='render a subset of the schema')
parser.add_argument('--out', action='store', help='directory to store the generated files')
parser.add_argument('--ref', action='store', help='git reference to use when building schemas')
parser.add_argument('--template-settings', action='store',
help='index template settings to use when generating elasticsearch template')
parser.add_argument('--mapping-settings', action='store',
help='mapping settings to use when generating elasticsearch template')
parser.add_argument('--strict', action='store_true',
help='enforce stricter checking at schema cleanup')
args = parser.parse_args()
# Clean up empty include of the Makefile
if args.include and [''] == args.include:
args.include.clear()
return args
def read_version(ref=None):
if ref:
print('Loading schemas from git ref ' + ref)
tree = ecs_helpers.get_tree_by_ref(ref)
return tree['version'].data_stream.read().decode('utf-8').rstrip()
else:
print('Loading schemas from local files')
with open('version', 'r') as infile:
return infile.read().rstrip()
if __name__ == '__main__':
main()