9
9
# ==------------------------------------------------------------------------==#
10
10
11
11
import argparse
12
+ import json
12
13
import sys
13
14
from pathlib import Path
14
15
@@ -23,7 +24,7 @@ def main():
23
24
help = "Path to the YAML file containing header specification" ,
24
25
metavar = "FILE" ,
25
26
type = Path ,
26
- nargs = 1 ,
27
+ nargs = "+" ,
27
28
)
28
29
parser .add_argument (
29
30
"-o" ,
@@ -32,6 +33,11 @@ def main():
32
33
type = Path ,
33
34
required = True ,
34
35
)
36
+ parser .add_argument (
37
+ "--json" ,
38
+ help = "Write JSON instead of a header, can use multiple YAML files" ,
39
+ action = "store_true" ,
40
+ )
35
41
parser .add_argument (
36
42
"--depfile" ,
37
43
help = "Path to write a depfile" ,
@@ -52,6 +58,11 @@ def main():
52
58
)
53
59
args = parser .parse_args ()
54
60
61
+ if not args .json and len (args .yaml_file ) != 1 :
62
+ print ("Only one YAML file at a time without --json" , file = sys .stderr )
63
+ parser .print_usage (sys .stderr )
64
+ return 2
65
+
55
66
files_read = set ()
56
67
57
68
def write_depfile ():
@@ -66,35 +77,47 @@ def load_yaml(path):
66
77
files_read .add (path )
67
78
return load_yaml_file (path , HeaderFile , args .entry_point )
68
79
69
- merge_from_files = dict ()
70
-
71
- def merge_from (paths ):
72
- for path in paths :
73
- # Load each file exactly once, in case of redundant merges.
74
- if path in merge_from_files :
75
- continue
76
- header = load_yaml (path )
77
- merge_from_files [path ] = header
78
- merge_from (path .parent / f for f in header .merge_yaml_files )
79
-
80
- # Load the main file first.
81
- [yaml_file ] = args .yaml_file
82
- header = load_yaml (yaml_file )
83
-
84
- # Now load all the merge_yaml_files, and any transitive merge_yaml_files.
85
- merge_from (yaml_file .parent / f for f in header .merge_yaml_files )
86
-
87
- # Merge in all those files' contents.
88
- for merge_from_path , merge_from_header in merge_from_files .items ():
89
- if merge_from_header .name is not None :
90
- print (f"{ merge_from_path !s} : Merge file cannot have header field" , stderr )
91
- return 2
92
- header .merge (merge_from_header )
93
-
94
- # The header_template path is relative to the containing YAML file.
95
- template = header .template (yaml_file .parent , files_read )
96
-
97
- contents = fill_public_api (header .public_api (), template )
80
+ def load_header (yaml_file ):
81
+ merge_from_files = dict ()
82
+
83
+ def merge_from (paths ):
84
+ for path in paths :
85
+ # Load each file exactly once, in case of redundant merges.
86
+ if path in merge_from_files :
87
+ continue
88
+ header = load_yaml (path )
89
+ merge_from_files [path ] = header
90
+ merge_from (path .parent / f for f in header .merge_yaml_files )
91
+
92
+ # Load the main file first.
93
+ header = load_yaml (yaml_file )
94
+
95
+ # Now load all the merge_yaml_files, and transitive merge_yaml_files.
96
+ merge_from (yaml_file .parent / f for f in header .merge_yaml_files )
97
+
98
+ # Merge in all those files' contents.
99
+ for merge_from_path , merge_from_header in merge_from_files .items ():
100
+ if merge_from_header .name is not None :
101
+ print (
102
+ f"{ merge_from_path !s} : Merge file cannot have header field" ,
103
+ file = sys .stderr ,
104
+ )
105
+ return 2
106
+ header .merge (merge_from_header )
107
+
108
+ return header
109
+
110
+ if args .json :
111
+ contents = json .dumps (
112
+ [load_header (file ).json_data () for file in args .yaml_file ],
113
+ indent = 2 ,
114
+ )
115
+ else :
116
+ [yaml_file ] = args .yaml_file
117
+ header = load_header (yaml_file )
118
+ # The header_template path is relative to the containing YAML file.
119
+ template = header .template (yaml_file .parent , files_read )
120
+ contents = fill_public_api (header .public_api (), template )
98
121
99
122
write_depfile ()
100
123
0 commit comments