forked from JeffersonLab/iguana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchameleon
executable file
·78 lines (67 loc) · 2.39 KB
/
chameleon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
require 'yaml'
require 'optparse'
require 'ostruct'
require 'fileutils'
require_relative 'src/bind_c.rb'
# default options
@args = OpenStruct.new
@args.action_yaml = ''
@args.out_dir = ''
@args.out_prefix = ''
@args.verbose = false
@args.quiet = false
# parse arguments
OptionParser.new do |o|
o.banner = "USAGE: #{$0} [OPTIONS]..."
o.separator ''
o.separator 'OPTIONS:'
o.on('-i', '--input [ACTION_YAML]', 'action function specification YAML') {|a| @args.action_yaml = a}
o.separator ''
o.on('-o', '--output [OUTPUT_DIR]', 'output directory', 'default: a subdirectory of ./chameleon-tree') {|a| @args.out_dir = a}
o.on('-p', '--prefix [PREFIX]', 'if specified, each generated filename will start with [PREFIX]') {|a| @args.out_prefix = a}
o.separator ''
o.on('-v', '--verbose', 'print more output') {|a| @args.verbose = true}
o.on('-q', '--quiet', 'print no output (unless --verbose)') {|a| @args.quiet = true}
o.on_tail('-h', '--help', 'show this message') do
puts o
exit
end
end.parse! ARGV
VERBOSE = @args.verbose
ACTION_YAML = @args.action_yaml
# start main generator
@main = Generator.new
@main.verbose "OPTIONS: #{@args}"
# open action function specification
@main.error "option '--input' needs a value; use '--help' for guidance" if ACTION_YAML.empty?
@main.verbose "parsing #{ACTION_YAML}"
@main.error "input file '#{ACTION_YAML}' does not exist" unless File.exist? ACTION_YAML
main_spec = YAML.load_file ACTION_YAML
# parse algorithm info
algo_name = @main.get_spec main_spec, 'algorithm', 'name'
@main.verbose "algorithm name: #{algo_name}"
# make output files
def out_name(name)
[ @args.out_prefix, name ].reject(&:empty?).join '_'
end
algo_dir = File.dirname ACTION_YAML
@main.error "directory '#{algo_dir}' does not exist" unless Dir.exist? algo_dir
out_dir = @args.out_dir.empty? ? File.join('chameleon-tree', algo_dir) : @args.out_dir
FileUtils.mkdir_p out_dir, verbose: @args.verbose
out_files = {
:c_bindings => Bind_c.new(File.join(out_dir, out_name('bind.cc')), algo_name),
}
# parse action functions
@main.get_spec(main_spec, 'actions').each do |action_spec|
out_files[:c_bindings].bind action_spec
end
# print list of generated files
unless @args.quiet
puts "[chameleon] Generated files:"
out_files.each do |key,file|
puts "[chameleon] #{key} => #{file.out_name}"
end
end
# close all generated files
out_files.values.each &:close