13
13
import sys
14
14
import re
15
15
import fnmatch
16
+ import os .path
16
17
17
18
# ----------------------------------------------------------------------------------------------
18
19
19
20
import yaml
20
21
21
22
# ----------------------------------------------------------------------------------------------
22
23
23
- from jwst import version
24
- from jwst .stpipe import cmdline as jwst_cmdline
24
+ from jwst import __version__ as jwst_version
25
+ from jwst .stpipe import pipeline
26
+ from jwst import pipeline as pipepkg
25
27
26
28
# ----------------------------------------------------------------------------------------------
27
29
28
30
import crds
29
- from crds .core import log , exceptions , utils , timestamp
31
+ from crds .core import log , exceptions , utils , timestamp , pysh
30
32
from crds .core .log import srepr
31
33
32
34
# ----------------------------------------------------------------------------------------------
33
35
VERSION_RE_STR = r"(\d+\.\d+.\d+).*"
34
36
35
- CAL_VER = re .match (r"^" + VERSION_RE_STR , version . __version__ ).group (1 )
37
+ CAL_VER = re .match (r"^" + VERSION_RE_STR , jwst_version ).group (1 )
36
38
37
39
GENERATION_DATE = timestamp .now ("T" ).split ("." )[0 ]
38
40
@@ -52,15 +54,15 @@ def __init__(self, input_yaml):
52
54
self .pipeline_cfgs_to_steps = {}
53
55
self .steps_to_reftypes = {}
54
56
self .generate_pipeline_info ()
55
- self .exptypes_to_cfgs = { exp_type : self .exptype_to_pipelines (exp_type )
57
+ self .exptypes_to_cfgs = { exp_type : self .exptype_to_pipelines (exp_type )
56
58
for exp_type in self .exp_types }
57
- self .exptypes_to_reftypes = { exp_type : self .exptype_to_reftypes (exp_type )
59
+ self .exptypes_to_reftypes = { exp_type : self .exptype_to_reftypes (exp_type )
58
60
for exp_type in self .exp_types }
59
61
60
62
def get_body (self ):
61
63
"""Load the input_yaml as a CRDS Struct and return it."""
62
- return utils .Struct (yaml .load (self .input_yaml ))
63
-
64
+ return utils .Struct (yaml .safe_load (self .input_yaml ))
65
+
64
66
def get_updated_yaml (self ):
65
67
"""Modify the input_yaml to replace the cal code version and generation date,
66
68
useful for updating an existing reference and running it through this generator.
@@ -77,18 +79,31 @@ def get_updated_yaml(self):
77
79
else :
78
80
input_body += [line ]
79
81
return "\n " .join (input_body ).split (REFERENCE_DIVIDER )[0 ] + "\n " + REFERENCE_DIVIDER + "\n "
80
-
82
+
81
83
def generate_pipeline_info (self ):
82
84
"""Based on the input YAML and JWST cal code, generate the mappings:
83
85
pipeline_cfgs_to_steps
84
86
steps_to_reftypes
85
87
"""
88
+ pysh .sh ("rm -rf configs" )
89
+ pysh .sh ("collect_pipeline_cfgs configs" )
86
90
self .pipeline_cfgs_to_steps ["skip_2b.cfg" ] = []
87
91
for pipeline_cfg in self .loaded_cfg .pipeline_cfgs :
88
- steps_to_reftypes = jwst_cmdline .steps_to_reftypes_from_config (pipeline_cfg )
92
+ log .info ("Processing" , repr (pipeline_cfg ))
93
+ cfgdir = "configs" # os.path.dirname(pipepkg.__file__) or ""
94
+ cfgpath = os .path .join (cfgdir , pipeline_cfg )
95
+ p = pipeline .Pipeline .from_config_file (cfgpath )
96
+ steps_to_reftypes = {}
97
+ for name , stepcfg in p .steps .items ():
98
+ if stepcfg .get ("skip" , True ):
99
+ log .info ("Considering" , repr (name ), "skip" )
100
+ else :
101
+ log .info ("Considering" , repr (name ), "keep" )
102
+ step = p .step_defs [name ] # class
103
+ steps_to_reftypes [name ] = step .reference_file_types
89
104
self .pipeline_cfgs_to_steps [pipeline_cfg ] = sorted (list (steps_to_reftypes .keys ()))
90
105
self .steps_to_reftypes .update (steps_to_reftypes )
91
-
106
+
92
107
def generate_output_yaml (self ):
93
108
"""Generate the SYSTEM CRDSCFG reference YAML."""
94
109
output_yaml = self .get_updated_yaml ()
@@ -97,14 +112,14 @@ def generate_output_yaml(self):
97
112
output_yaml += yaml .dump ({"exptypes_to_pipelines" : self .exptypes_to_cfgs }) + "\n "
98
113
output_yaml += yaml .dump ({"exptypes_to_reftypes" : self .exptypes_to_reftypes }) + "\n "
99
114
return output_yaml
100
-
115
+
101
116
def __str__ (self ):
102
117
return self .generate_output_yaml ()
103
-
118
+
104
119
def exptype_to_pipelines (self , exp_type ):
105
120
"""For a given EXP_TYPE string, return a list of reftypes needed to process that
106
121
EXP_TYPE through the data levels appropriate for that EXP_TYPE.
107
-
122
+
108
123
Return [reftypes... ]
109
124
"""
110
125
pipelines = []
@@ -116,15 +131,15 @@ def exptype_to_reftypes(self, exp_type):
116
131
"""Return all reftypes associated with processing all steps of all pipelines for `exp_type`."""
117
132
# with log.error_on_exception("Failed exptype_to_reftypes for", srepr(exp_type)):
118
133
reftypes = []
119
- for pipeline in self .exptype_to_pipelines (exp_type ):
134
+ for pipeline in self .exptype_to_pipelines (exp_type ):
120
135
reftypes .extend (self .get_pipeline_types (pipeline , exp_type ))
121
136
reftypes = sorted (list (set (reftypes )))
122
137
return reftypes
123
-
138
+
124
139
def get_level_pipeline (self , level , exp_type ):
125
140
"""Interpret the level_pipeline_exptypes data structure relative to
126
141
processing `level` and `exp_type` to determine a pipeline .cfg file.
127
-
142
+
128
143
Return [ pipeline .cfg ] or []
129
144
"""
130
145
pipeline_exptypes = self .loaded_cfg .level_pipeline_exptypes [level ]
@@ -135,16 +150,16 @@ def get_level_pipeline(self, level, exp_type):
135
150
return [pipeline ]
136
151
log .error ("Unhandled EXP_TYPE" , srepr (exp_type ), "for" , srepr (level ))
137
152
return []
138
-
153
+
139
154
# raise exceptions.CrdsPipelineCfgDeterminationError("Unhandled EXP_TYPE", srepr(exp_type))
140
-
155
+
141
156
def get_pipeline_types (self , pipeline , exp_type ):
142
157
"""Based on a pipeline .cfg filename and an EXP_TYPE, look up
143
158
the Steps corresponding to the .cfg and extrapolate those to the
144
159
reftypes used by those Steps. If there are exceptions to the
145
160
reftypes assigned for a particular Step that depend on EXP_TYPE,
146
161
return the revised types for that Step instead.
147
-
162
+
148
163
Return [reftypes, ...]
149
164
"""
150
165
steps = self .pipeline_cfgs_to_steps [pipeline ]
@@ -160,8 +175,8 @@ def get_pipeline_types(self, pipeline, exp_type):
160
175
found = False
161
176
for exptype_pattern in exptypes :
162
177
if glob_match (exptype_pattern , exp_type ):
163
- log .verbose ("Adding exceptional types" , more_reftypes ,
164
- "for step" , srepr (step ), "case" , srepr (exptype_pattern ),
178
+ log .verbose ("Adding exceptional types" , more_reftypes ,
179
+ "for step" , srepr (step ), "case" , srepr (exptype_pattern ),
165
180
"based on exp_type" , srepr (exp_type ))
166
181
found = True
167
182
reftypes .extend (more_reftypes )
@@ -171,7 +186,7 @@ def get_pipeline_types(self, pipeline, exp_type):
171
186
else :
172
187
raise exceptions .CrdsPipelineTypeDeterminationError ("Unhandled EXP_TYPE for exceptional Step" , srepr (step ))
173
188
return reftypes
174
-
189
+
175
190
# --------------------------------------------------------------------------------------
176
191
177
192
def glob_match (expr , value ):
0 commit comments