CIShell: rp2ci

File rp2ci, 4.2 KB (added by mmc, 11 years ago)

rp2ci conversion script

Line 
1#!/bin/bash
2# ----------------------------------------------------------------------
3#  RAPPTURE 2 CISHELL
4#
5#  Developed this script as part of the 2012 JSMF Workshop on
6#  Plug-and-Play Macroscopes, Nov 16-17 at Indiana University.
7#
8#  Takes a Rappture tool.xml files and updates an existing CIShell
9#  project.  General workflow is:
10#    1) Create a new project for the plug-in in CIShell
11#    2) Run this script:  rp2ci tool.xml projectName
12#    3) Export the project from Eclipse into CIShell
13#
14# ======================================================================
15#  AUTHOR:  Michael McLennan, Purdue University
16#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
17#
18#  See the file "license.terms" for information on usage and
19#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
20# ======================================================================
21#\
22exec tclsh "$0" "$@"
23
24package require Rappture
25
26if {$argc != 2} {
27    puts stderr "usage: rp2ci tool.xml projectName"
28    exit 1
29}
30set rpFile [lindex $argv 0]
31set projName [lindex $argv 1]
32
33if {[catch {Rappture::library $rpFile} tool]} {
34    puts stderr "can't load tool.xml from $rpFile"
35    exit 1
36}
37
38if {![file isdirectory $projName]} {
39    puts stderr "can't find project $projName"
40    exit 1
41}
42if {![file exists [file join $projName META-INF]]} {
43    puts stderr "not a CIShell project: $projName"
44    exit 1
45}
46
47# build up a string of inputs for METADATA.XML
48set ADs ""
49set inputs ""
50foreach path [Rappture::entities -as path $tool input] {
51    set name [$tool element -as id $path]
52    set label [$tool get $path.about.label]
53    set desc [$tool get $path.about.description]
54    set default [$tool get $path.default]
55    append ADs "<AD name=\"$label\" id=\"$name\" type=\"String\" description=\"$desc\" default=\"$default\"/>\n"
56    append inputs "String $name = (String) parameters.get(\"$name\");\n"
57}
58
59set fid [open [file join $projName OSGI-INF metatype METADATA.XML] r]
60set info [read $fid]
61close $fid
62
63if {[regsub {>[ \t\n]*</OCD>} $info ">\n$ADs</OCD>" info] != 1} {
64    error "failed to add inputs to METADATA.XML"
65}
66set fid [open [file join $projName OSGI-INF metatype METADATA.XML] w]
67puts -nonewline $fid $info
68close $fid
69
70# Find the algorithm code
71set dir [file join $projName src]
72set found 0
73while {!$found} {
74    foreach fname [glob [file join $dir *]] {
75        if {[string match *Algorithm.java $fname]} {
76            set found 1
77            break
78        } elseif {[file isdirectory $fname]} {
79            set dir $fname
80            break
81        }
82    }
83}
84
85# Find stuff in the file...
86set fid [open $fname r]
87set algorithm [read $fid]
88close $fid
89
90if {![regexp {package org\.([^;]+);} $algorithm match pkg]} {
91    error "can't find package name in algorithm"
92}
93if {![regexp {public +class +([^ ]+)} $algorithm match class]} {
94    error "can't find class name in algorithm"
95}
96
97# Make substitutions...
98set template {
99package org.@@PACKAGE@@;
100
101import java.util.Dictionary;
102
103import org.cishell.framework.CIShellContext;
104import org.cishell.framework.algorithm.Algorithm;
105import org.cishell.framework.algorithm.AlgorithmExecutionException;
106import org.cishell.framework.data.Data;
107import org.osgi.service.log.LogService;
108
109public class @@CLASS@@ implements Algorithm {
110    private Data[] data;
111    private Dictionary parameters;
112    private CIShellContext ciShellContext;
113        private LogService logger;
114
115    public @@CLASS@@(Data[] data,
116                                  Dictionary parameters,
117                                  CIShellContext ciShellContext) {
118        this.data = data;
119        this.parameters = parameters;
120        this.ciShellContext = ciShellContext;
121        this.logger = (LogService) ciShellContext.getService(LogService.class.getName());
122    }
123
124    public Data[] execute() throws AlgorithmExecutionException {
125        @@INPUTS@@
126        this.logger.log(LogService.LOG_INFO, "Rappture tool: @@TOOL@@");
127
128        //
129        // Fix this to exec tool and return output...
130        //
131        return null;
132    }
133}
134}
135
136set newalg [string map [list \
137    @@PACKAGE@@ $pkg \
138    @@CLASS@@ $class \
139    @@INPUTS@@ $inputs \
140    @@TOOL@@ [$tool get tool.title] \
141] $template]
142
143set fid [open $fname w]
144puts -nonewline $fid $newalg
145close $fid