22
22
from .load_tool import fetch_document , resolve_and_validate_document
23
23
from .process import shortname , uniquename
24
24
from .utils import CWLObjectType , CWLOutputType
25
+ from .loghandler import _logger
26
+ from .update import update , ORDERED_VERSIONS
25
27
26
28
LoadRefType = Callable [[Optional [str ], str ], ResolveType ]
27
29
@@ -151,15 +153,37 @@ def pack(
151
153
document_loader .idx [po ["id" ]] = CommentedMap (po .items ())
152
154
document_loader .idx [metadata ["id" ]] = CommentedMap (metadata .items ())
153
155
154
- def loadref (base : Optional [str ], uri : str ) -> ResolveType :
155
- return document_loader .resolve_ref (uri , base_url = base )[0 ]
156
+ found_versions = {
157
+ cast (str , loadingContext .metadata ["cwlVersion" ])
158
+ } # type: Set[str]
159
+
160
+ def loadref (base : Optional [str ], lr_uri : str ) -> ResolveType :
161
+ lr_loadingContext = loadingContext .copy ()
162
+ lr_loadingContext .metadata = {}
163
+ lr_loadingContext , lr_workflowobj , lr_uri = fetch_document (
164
+ lr_uri , lr_loadingContext
165
+ )
166
+ lr_loadingContext , lr_uri = resolve_and_validate_document (
167
+ lr_loadingContext , lr_workflowobj , lr_uri
168
+ )
169
+ found_versions .add (cast (str , lr_loadingContext .metadata ["cwlVersion" ]))
170
+ if lr_loadingContext .loader is None :
171
+ raise Exception ("loader should not be None" )
172
+ return lr_loadingContext .loader .resolve_ref (lr_uri , base_url = base )[0 ]
156
173
157
174
ids = set () # type: Set[str]
158
175
find_ids (processobj , ids )
159
176
160
177
runs = {uri }
161
178
find_run (processobj , loadref , runs )
162
179
180
+ # Figure out the highest version, everything needs to be updated
181
+ # to it.
182
+ m = 0
183
+ for fv in found_versions :
184
+ m = max (m , ORDERED_VERSIONS .index (fv ))
185
+ update_to_version = ORDERED_VERSIONS [m ]
186
+
163
187
for f in runs :
164
188
find_ids (document_loader .resolve_ref (f )[0 ], ids )
165
189
@@ -193,7 +217,7 @@ def rewrite_id(r: str, mainuri: str) -> None:
193
217
rewrite_id (r , uri )
194
218
195
219
packed = CommentedMap (
196
- (("$graph" , CommentedSeq ()), ("cwlVersion" , metadata [ "cwlVersion" ] ))
220
+ (("$graph" , CommentedSeq ()), ("cwlVersion" , update_to_version ))
197
221
)
198
222
namespaces = metadata .get ("$namespaces" , None )
199
223
@@ -208,6 +232,21 @@ def rewrite_id(r: str, mainuri: str) -> None:
208
232
dcr = cast (CommentedMap , dcr )
209
233
if not isinstance (dcr , MutableMapping ):
210
234
continue
235
+
236
+ dcr = update (
237
+ dcr ,
238
+ document_loader ,
239
+ r ,
240
+ loadingContext .enable_dev ,
241
+ metadata ,
242
+ update_to_version ,
243
+ )
244
+
245
+ if "http://commonwl.org/cwltool#original_cwlVersion" in metadata :
246
+ del metadata ["http://commonwl.org/cwltool#original_cwlVersion" ]
247
+ if "http://commonwl.org/cwltool#original_cwlVersion" in dcr :
248
+ del dcr ["http://commonwl.org/cwltool#original_cwlVersion" ]
249
+
211
250
if "$schemas" in metadata :
212
251
for s in metadata ["$schemas" ]:
213
252
schemas .add (s )
0 commit comments