25
25
import org .apache .taverna .robundle .Bundles ;
26
26
import org .apache .taverna .robundle .manifest .*;
27
27
import org .commonwl .view .cwl .CWLTool ;
28
+ import org .commonwl .view .cwl .CWLValidationException ;
28
29
import org .commonwl .view .git .GitDetails ;
30
+ import org .commonwl .view .git .GitType ;
29
31
import org .commonwl .view .graphviz .GraphVizService ;
30
32
import org .commonwl .view .workflow .Workflow ;
31
33
import org .eclipse .jgit .api .Git ;
@@ -128,8 +130,9 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
128
130
// Add the files from the Github repo to this workflow
129
131
Set <HashableAgent > authors = new HashSet <>();
130
132
Git gitRepo = Git .open (new File (workflow .getGitRepoPath ()));
131
- Path gitPath = gitRepo .getRepository ().getDirectory ().toPath ();
132
- addFilesToBundle (bundle , bundlePath , gitRepo , gitPath , authors );
133
+ Path relativePath = Paths .get (FilenameUtils .getPath (gitInfo .getPath ()));
134
+ Path gitPath = gitRepo .getRepository ().getWorkTree ().toPath ().resolve (relativePath );
135
+ addFilesToBundle (gitInfo , bundle , bundlePath , gitRepo , gitPath , authors );
133
136
134
137
// Add combined authors
135
138
manifest .setAuthoredBy (new ArrayList <>(authors ));
@@ -148,13 +151,32 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
148
151
// Add annotation files
149
152
GitDetails wfDetails = workflow .getRetrievedFrom ();
150
153
151
- // Run cwltool
154
+ // Get URL to run cwltool
152
155
String rawUrl = wfDetails .getRawUrl ();
156
+ String packedWorkflowID = workflow .getPackedWorkflowID ();
157
+ if (packedWorkflowID != null ) {
158
+ if (packedWorkflowID .charAt (0 ) != '#' ) {
159
+ rawUrl += "#" ;
160
+ }
161
+ rawUrl += packedWorkflowID ;
162
+ }
163
+
164
+ // Run cwltool for annotations
153
165
List <PathAnnotation > manifestAnnotations = new ArrayList <>();
154
- addAggregation (bundle , manifestAnnotations ,
155
- "merged.cwl" , cwlTool .getPackedVersion (rawUrl ));
156
- addAggregation (bundle , manifestAnnotations ,
157
- "workflow.ttl" , cwlTool .getRDF (rawUrl ));
166
+ try {
167
+ addAggregation (bundle , manifestAnnotations ,
168
+ "merged.cwl" , cwlTool .getPackedVersion (rawUrl ));
169
+ } catch (CWLValidationException ex ) {
170
+ logger .error ("Could not pack workflow when creating Research Object" , ex .getMessage ());
171
+ }
172
+ if (gitInfo .getType () != GitType .GENERIC ) {
173
+ try {
174
+ addAggregation (bundle , manifestAnnotations ,
175
+ "workflow.ttl" , cwlTool .getRDF (rawUrl ));
176
+ } catch (CWLValidationException ex ) {
177
+ logger .error ("Could not get RDF for workflow from raw URL" , ex .getMessage ());
178
+ }
179
+ }
158
180
bundle .getManifest ().setAnnotations (manifestAnnotations );
159
181
160
182
// Git2prov history
@@ -174,117 +196,130 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
174
196
175
197
/**
176
198
* Add files to this bundle from a list of Github repository contents
199
+ * @param gitDetails The Git information for the repository
177
200
* @param bundle The RO bundle to add files/directories to
178
201
* @param bundlePath The current path within the RO bundle
179
202
* @param gitRepo The Git repository
180
203
* @param repoPath The current path within the Git repository
181
204
* @param authors The combined set of authors for al the files
182
205
*/
183
- private void addFilesToBundle (Bundle bundle , Path bundlePath ,
184
- Git gitRepo , Path repoPath ,
185
- Set < HashableAgent > authors ) throws IOException {
186
- File [] files = gitRepo . getRepository (). getDirectory ().listFiles ();
206
+ private void addFilesToBundle (GitDetails gitDetails , Bundle bundle , Path bundlePath ,
207
+ Git gitRepo , Path repoPath , Set < HashableAgent > authors )
208
+ throws IOException {
209
+ File [] files = repoPath . toFile ().listFiles ();
187
210
for (File file : files ) {
188
- if (file .isDirectory ()) {
211
+ if (!file .getName ().equals (".git" )) {
212
+ if (file .isDirectory ()) {
189
213
190
- // Create a new folder in the RO for this directory
191
- Path newBundlePath = bundlePath .resolve (file .getName ());
192
- Files .createDirectory (newBundlePath );
214
+ // Create a new folder in the RO for this directory
215
+ Path newBundlePath = bundlePath .resolve (file .getName ());
216
+ Files .createDirectory (newBundlePath );
193
217
194
- // Add all files in the subdirectory to this new folder
195
- addFilesToBundle ( bundle , newBundlePath , gitRepo ,
196
- repoPath . resolve (file .getName ()), authors );
218
+ // Create git details object for subfolder
219
+ GitDetails subfolderGitDetails = new GitDetails ( gitDetails . getRepoUrl (), gitDetails . getBranch () ,
220
+ Paths . get ( gitDetails . getPath ()). resolve (file .getName ()). toString () );
197
221
198
- } else {
199
- try {
200
- // Where to store the new file in bundle
201
- Path bundleFilePath = bundlePath .resolve (file .getName ());
222
+ // Add all files in the subdirectory to this new folder
223
+ addFilesToBundle (subfolderGitDetails , bundle , newBundlePath , gitRepo ,
224
+ repoPath .resolve (file .getName ()), authors );
202
225
203
- // Get direct URL
204
- URI rawURI = new URI ("http://example.com" );
205
-
206
- // Variable to store file contents and aggregation
207
- String fileContent = null ;
208
- PathMetadata aggregation ;
209
-
210
- // Download or externally link if oversized
211
- if (file .length () <= singleFileSizeLimit ) {
212
- // Save file to research object bundle
213
- fileContent = readFileToString (file );
214
- Bundles .setStringValue (bundleFilePath , fileContent );
215
-
216
- // Set retrieved information for this file in the manifest
217
- aggregation = bundle .getManifest ().getAggregation (bundleFilePath );
218
- aggregation .setRetrievedFrom (rawURI );
219
- aggregation .setRetrievedBy (appAgent );
220
- aggregation .setRetrievedOn (aggregation .getCreatedOn ());
221
- } else {
222
- logger .info ("File " + file .getName () + " is too large to download - " +
223
- FileUtils .byteCountToDisplaySize (file .length ()) + "/" +
224
- FileUtils .byteCountToDisplaySize (singleFileSizeLimit ) +
225
- ", linking externally to RO bundle" );
226
-
227
- // Set information for this file in the manifest
228
- aggregation = bundle .getManifest ().getAggregation (rawURI );
229
- Proxy bundledAs = new Proxy ();
230
- bundledAs .setURI ();
231
- bundledAs .setFolder (repoPath );
232
- aggregation .setBundledAs (bundledAs );
233
- }
226
+ } else {
227
+ try {
228
+ // Where to store the new file in bundle
229
+ Path bundleFilePath = bundlePath .resolve (file .getName ());
230
+
231
+ // Create git details object for file
232
+ GitDetails fileGitDetails = new GitDetails (gitDetails .getRepoUrl (), gitDetails .getBranch (),
233
+ Paths .get (gitDetails .getPath ()).resolve (file .getName ()).toString ());
234
+
235
+ // Get direct URL
236
+ URI rawURI = new URI (fileGitDetails .getRawUrl ());
237
+
238
+ // Variable to store file contents and aggregation
239
+ String fileContent = null ;
240
+ PathMetadata aggregation ;
241
+
242
+ // Download or externally link if oversized
243
+ if (file .length () <= singleFileSizeLimit ) {
244
+ // Save file to research object bundle
245
+ fileContent = readFileToString (file );
246
+ Bundles .setStringValue (bundleFilePath , fileContent );
247
+
248
+ // Set retrieved information for this file in the manifest
249
+ aggregation = bundle .getManifest ().getAggregation (bundleFilePath );
250
+ if (gitDetails .getType () != GitType .GENERIC ) {
251
+ aggregation .setRetrievedFrom (rawURI );
252
+ aggregation .setRetrievedBy (appAgent );
253
+ aggregation .setRetrievedOn (aggregation .getCreatedOn ());
254
+ }
255
+ } else {
256
+ logger .info ("File " + file .getName () + " is too large to download - " +
257
+ FileUtils .byteCountToDisplaySize (file .length ()) + "/" +
258
+ FileUtils .byteCountToDisplaySize (singleFileSizeLimit ) +
259
+ ", linking externally to RO bundle" );
260
+
261
+ // Set information for this file in the manifest
262
+ aggregation = bundle .getManifest ().getAggregation (rawURI );
263
+ Proxy bundledAs = new Proxy ();
264
+ bundledAs .setURI ();
265
+ bundledAs .setFolder (repoPath );
266
+ aggregation .setBundledAs (bundledAs );
267
+ }
234
268
235
- // Special handling for cwl files
236
- if (FilenameUtils .getExtension (file .getName ()).equals ("cwl" )) {
237
- // Correct mime type (no official standard for yaml)
238
- aggregation .setMediatype ("text/x-yaml" );
269
+ // Special handling for cwl files
270
+ if (FilenameUtils .getExtension (file .getName ()).equals ("cwl" )) {
271
+ // Correct mime type (no official standard for yaml)
272
+ aggregation .setMediatype ("text/x-yaml" );
239
273
240
- // Add conformsTo for version extracted from regex
241
- if (fileContent != null ) {
242
- Matcher m = cwlVersionPattern .matcher (fileContent );
243
- if (m .find ()) {
244
- aggregation .setConformsTo (new URI ("https://w3id.org/cwl/" + m .group (1 )));
274
+ // Add conformsTo for version extracted from regex
275
+ if (fileContent != null ) {
276
+ Matcher m = cwlVersionPattern .matcher (fileContent );
277
+ if (m .find ()) {
278
+ aggregation .setConformsTo (new URI ("https://w3id.org/cwl/" + m .group (1 )));
279
+ }
245
280
}
246
281
}
247
- }
248
282
249
- // Add authors from git commits to the file
250
- try {
251
- Set <HashableAgent > fileAuthors = new HashSet <>();
252
- Iterable <RevCommit > logs = gitRepo .log ()
253
- .addPath (bundlePath .toString ())
254
- .call ();
255
- for (RevCommit rev : logs ) {
256
- // Use author first with backup of committer
257
- PersonIdent author = rev .getAuthorIdent ();
258
- if (author == null ) {
259
- author = rev .getCommitterIdent ();
260
- }
261
- // Create a new agent and add as much detail as possible
262
- if (author != null ) {
263
- HashableAgent newAgent = new HashableAgent ();
264
- String name = author .getName ();
265
- if (name != null && name .length () > 0 ) {
266
- newAgent .setName (author .getName ());
283
+ // Add authors from git commits to the file
284
+ try {
285
+ Set <HashableAgent > fileAuthors = new HashSet <>();
286
+ Iterable <RevCommit > logs = gitRepo .log ()
287
+ .addPath (Paths .get (gitDetails .getPath ()).resolve (file .getName ()).toString ())
288
+ .call ();
289
+ for (RevCommit rev : logs ) {
290
+ // Use author first with backup of committer
291
+ PersonIdent author = rev .getAuthorIdent ();
292
+ if (author == null ) {
293
+ author = rev .getCommitterIdent ();
267
294
}
268
- String email = author .getEmailAddress ();
269
- if (email != null && email .length () > 0 ) {
270
- newAgent .setUri (new URI ("mailto:" + author .getEmailAddress ()));
295
+ // Create a new agent and add as much detail as possible
296
+ if (author != null ) {
297
+ HashableAgent newAgent = new HashableAgent ();
298
+ String name = author .getName ();
299
+ if (name != null && name .length () > 0 ) {
300
+ newAgent .setName (author .getName ());
301
+ }
302
+ String email = author .getEmailAddress ();
303
+ if (email != null && email .length () > 0 ) {
304
+ newAgent .setUri (new URI ("mailto:" + author .getEmailAddress ()));
305
+ }
306
+ fileAuthors .add (newAgent );
271
307
}
272
- fileAuthors .add (newAgent );
273
308
}
309
+ authors .addAll (fileAuthors );
310
+ aggregation .setAuthoredBy (new ArrayList <>(fileAuthors ));
311
+ } catch (GitAPIException ex ) {
312
+ logger .error ("Could not get commits for file " + repoPath , ex );
274
313
}
275
- authors .addAll (fileAuthors );
276
- aggregation .setAuthoredBy (new ArrayList <>(fileAuthors ));
277
- } catch (GitAPIException ex ) {
278
- logger .error ("Could not get commits for file " + repoPath , ex );
279
- }
280
314
281
- // Set retrieved information for this file in the manifest
282
- aggregation .setRetrievedFrom (rawURI );
283
- aggregation .setRetrievedBy (appAgent );
284
- aggregation .setRetrievedOn (aggregation .getCreatedOn ());
315
+ // Set retrieved information for this file in the manifest
316
+ aggregation .setRetrievedFrom (rawURI );
317
+ aggregation .setRetrievedBy (appAgent );
318
+ aggregation .setRetrievedOn (aggregation .getCreatedOn ());
285
319
286
- } catch (URISyntaxException ex ) {
287
- logger .error ("Error creating URI for RO Bundle" , ex );
320
+ } catch (URISyntaxException ex ) {
321
+ logger .error ("Error creating URI for RO Bundle" , ex );
322
+ }
288
323
}
289
324
}
290
325
}
0 commit comments