Skip to content

Commit 3567338

Browse files
committed
Look up license
1 parent bc441d5 commit 3567338

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
import java.io.File;
2626
import java.io.IOException;
2727
import java.io.StringWriter;
28+
import java.net.URI;
2829
import java.nio.file.Files;
2930
import java.nio.file.Path;
31+
import java.nio.file.Paths;
3032
import java.util.ArrayList;
33+
import java.util.Arrays;
3134
import java.util.HashMap;
3235
import java.util.Iterator;
3336
import java.util.List;
@@ -211,7 +214,7 @@ public Workflow parseWorkflowNative(Path workflowFile, String packedWorkflowId)
211214

212215
// Construct the rest of the workflow model
213216
Workflow workflowModel = new Workflow(label, extractDoc(cwlFile), getInputs(cwlFile),
214-
getOutputs(cwlFile), getSteps(cwlFile), null);
217+
getOutputs(cwlFile), getSteps(cwlFile));
215218

216219
workflowModel.setCwltoolVersion(cwlTool.getVersion());
217220

@@ -251,7 +254,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
251254
// Get paths to workflow
252255
String url = basicModel.getIdentifier();
253256
String workflowFileURI = workflowFile.toAbsolutePath().toUri().toString();
254-
String workTreeUri = workTree.toAbsolutePath().toUri().toString();
257+
URI workTreeUri = workTree.toAbsolutePath().toUri();
255258
String localPath = workflowFileURI;
256259
String gitPath = gitDetails.getPath();
257260
if (packedWorkflowID != null) {
@@ -268,7 +271,7 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
268271
String rdf = cwlTool.getRDF(localPath);
269272
// Replace /tmp/123123 with permalink base
270273
// NOTE: We do not just replace workflowFileURI, all referenced files will also get rewritten
271-
rdf = rdf.replace(workTreeUri,
274+
rdf = rdf.replace(workTreeUri.toString(),
272275
"https://w3id.org/cwl/view/git/" + latestCommit + "/");
273276
// Workaround for common-workflow-language/cwltool#427
274277
rdf = rdf.replace("<rdfs:>", "<http://www.w3.org/2000/01/rdf-schema#>");
@@ -419,6 +422,22 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
419422
wfSteps.put(rdfService.labelFromName(uri), wfStep);
420423
}
421424
}
425+
// Try to determine license
426+
ResultSet licenseResult = rdfService.getLicense(url);
427+
String licenseLink = null;
428+
if (licenseResult.hasNext()) {
429+
licenseLink = licenseResult.next().get("license").toString();
430+
} else {
431+
// Check for "LICENSE"-like files in root of git repo
432+
for (String licenseCandidate : new String[]{"LICENSE", "LICENSE.txt", "LICENSE.md"}) {
433+
// FIXME: This might wrongly match lower-case "license.txt" in case-insensitive file systems
434+
// but the URL would not work
435+
if (Files.isRegularFile(workTree.resolve(licenseCandidate))) {
436+
// Link to it by raw URL
437+
licenseLink = basicModel.getRetrievedFrom().getRawUrl(null, licenseCandidate);
438+
}
439+
}
440+
}
422441

423442
// Docker link
424443
ResultSet dockerResult = rdfService.getDockerLink(url);
@@ -434,7 +453,8 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
434453

435454
// Create workflow model
436455
Workflow workflowModel = new Workflow(label, doc,
437-
wfInputs, wfOutputs, wfSteps, dockerLink);
456+
wfInputs, wfOutputs, wfSteps,
457+
dockerLink, licenseLink);
438458

439459
// Generate DOT graph
440460
StringWriter graphWriter = new StringWriter();

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class RDFService {
3636
private final String queryCtx = "PREFIX cwl: <https://w3id.org/cwl/cwl#>\n" +
3737
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
3838
"PREFIX sld: <https://w3id.org/cwl/salad#>\n" +
39+
"PREFIX dct: <http://purl.org/dc/terms/>\n" +
40+
"PREFIX doap: <http://usefulinc.com/ns/doap#>\n" +
3941
"PREFIX Workflow: <https://w3id.org/cwl/cwl#Workflow/>\n" +
4042
"PREFIX DockerRequirement: <https://w3id.org/cwl/cwl#DockerRequirement/>\n" +
4143
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
@@ -431,4 +433,20 @@ ResultSet runQuery(ParameterizedSparqlString queryString) {
431433
}
432434
}
433435

436+
public ResultSet getLicense(String workflowURI) {
437+
ParameterizedSparqlString licenseQuery = new ParameterizedSparqlString();
438+
licenseQuery.setCommandText(queryCtx +
439+
"SELECT ?license \n" +
440+
"WHERE {\n" +
441+
" GRAPH ?wf {" +
442+
" ?wf rdf:type cwl:Workflow .\n" +
443+
" { ?wf s:license ?license } \n" +
444+
"UNION { ?wf doap:license ?license } \n" +
445+
"UNION { ?wf dct:license ?license } \n" +
446+
" }" +
447+
"}");
448+
licenseQuery.setIri("wf", workflowURI);
449+
return runQuery(licenseQuery);
450+
}
451+
434452
}

0 commit comments

Comments
 (0)