Skip to content

Commit f38d547

Browse files
committed
Add ability to get workflow overviews for packed CWL file
1 parent f8a81a5 commit f38d547

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/main/java/org/commonwl/view/cwl/CWLService.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,51 @@ public CWLService(RDFService rdfService,
110110
this.singleFileSizeLimit = singleFileSizeLimit;
111111
}
112112

113+
/**
114+
* Gets whether a file is packed using schema salad
115+
* @param workflowFile The file to be parsed
116+
* @return Whether the file is packed
117+
*/
118+
public boolean isPacked(File workflowFile) throws IOException {
119+
if (workflowFile.length() > singleFileSizeLimit) {
120+
return false;
121+
}
122+
String fileContent = readFileToString(workflowFile);
123+
return fileContent.contains("$graph");
124+
}
125+
126+
/**
127+
* Gets a list of workflows from a packed CWL file
128+
* @param packedFile The packed CWL file
129+
* @return The list of workflow overviews
130+
*/
131+
public List<WorkflowOverview> getWorkflowOverviewsFromPacked(File packedFile) throws IOException {
132+
if (packedFile.length() <= singleFileSizeLimit) {
133+
List<WorkflowOverview> overviews = new ArrayList<>();
134+
135+
JsonNode packedJson = yamlStringToJson(readFileToString(packedFile));
136+
137+
if (packedJson.has(DOC_GRAPH)) {
138+
for (JsonNode jsonNode : packedJson.get(DOC_GRAPH)) {
139+
if (extractProcess(jsonNode) == CWLProcess.WORKFLOW) {
140+
WorkflowOverview overview = new WorkflowOverview(jsonNode.get(ID).asText(),
141+
extractLabel(jsonNode), extractDoc(jsonNode));
142+
overviews.add(overview);
143+
}
144+
}
145+
} else {
146+
throw new IOException("The file given was not recognised as a packed CWL file");
147+
}
148+
149+
return overviews;
150+
151+
} else {
152+
throw new IOException("File '" + packedFile.getName() + "' is over singleFileSizeLimit - " +
153+
FileUtils.byteCountToDisplaySize(packedFile.length()) + "/" +
154+
FileUtils.byteCountToDisplaySize(singleFileSizeLimit));
155+
}
156+
}
157+
113158
/**
114159
* Gets the Workflow object from internal parsing
115160
* @param workflowFile The workflow file to be parsed

src/test/java/org/commonwl/view/cwl/CWLServiceTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.ByteArrayInputStream;
4040
import java.io.File;
4141
import java.io.IOException;
42+
import java.util.List;
4243
import java.util.Map;
4344

4445
import static org.apache.commons.io.FileUtils.readFileToString;
@@ -202,6 +203,23 @@ public void workflowOverviewOverSingleFileSizeLimitThrowsIOException() throws Ex
202203

203204
}
204205

206+
/**
207+
* Get workflow overviews from a packed file
208+
* TODO: Get better example with multiple workflows with label/doc
209+
*/
210+
@Test
211+
public void workflowOverviewsFromPackedFile() throws Exception {
212+
CWLService cwlService = new CWLService(Mockito.mock(RDFService.class),
213+
Mockito.mock(CWLTool.class), 5242880);
214+
File packedFile = new File("src/test/resources/cwl/make_to_cwl/dna.cwl");
215+
assertTrue(cwlService.isPacked(packedFile));
216+
List<WorkflowOverview> overviews = cwlService.getWorkflowOverviewsFromPacked(packedFile);
217+
assertEquals(1, overviews.size());
218+
assertEquals("main", overviews.get(0).getFileName());
219+
assertNull(overviews.get(0).getLabel());
220+
assertNull(overviews.get(0).getDoc());
221+
}
222+
205223
/**
206224
* Validate a LobSTR workflow
207225
* See: https://github.com/common-workflow-language/workflows/tree/master/workflows/lobSTR

0 commit comments

Comments
 (0)