changelog tags changeset manifest revisions annotate raw

parameters.rb

changeset 29: f62d79ec0b2c
parent:1c5f0d645fab
author: frsyuki@vcore
date: Sat Mar 31 17:31:36 2007 +0900 (14 years ago)
permissions: -rw-r--r--
1# Created by frsyuki on 2007-03-13.
2# Copyright (c) 2007. All rights reserved.
3
4
5class InvalidParameterError < RuntimeError
6 def initialize(param, msg = "Invalid parameter")
7 @param = param
8 @msg = msg
9 end
10 def to_s
11 return "#{@msg}: #{@param}"
12 end
13end
14
15
16class BootParameter
17 VIVER_PARAMETER_NS = "VIVER"
18 GLOBAL_NS = "GLOBAL"
19 DEFAULT_VIVERCONF_FILE_NAME = "viver.conf"
20 VIVERCONF_RELATIVE_DIR = ["", "etc", "viver", "VIVER"]
21
22 def initialize
23 @entries = Hash.new
24 end
25
26 def parse(path, rewrite = false)
27 if !rewrite && !@entries.empty?
28 return
29 end
30 $log.debug "loading #{path}"
31 File.open(path) {|file|
32 last_ns = GLOBAL_NS
33
34 # XXX: fileがからだとgetsの返り値はnil
35 file.gets.split(" ").each {|cmd|
36 if cmd =~ /\:$/
37 # 最後が: (名前空間指定)
38 last_ns = cmd[0, cmd.length - 1]
39 else
40 key, val = cmd.split("=", 2)
41 if val === nil; val = ""; end
42 @entries[ [last_ns, key] ] = val
43 $log.debug "parameter: #{last_ns}::#{key} = #{val}"
44 end
45 }
46
47 }
48 end
49
50 def set?(key)
51 return ( @entries.key?([GLOBAL_NS, key]) || @entries.key?([VIVER_PARAMETER_NS, key]) )
52 end
53
54 def getValue(key, default)
55 val = @entries[ [VIVER_PARAMETER_NS, key] ] || @entries[ [GLOBAL_NS, key] ]
56 if val === nil
57 return default
58 else
59 return val
60 end
61 end
62
63 def getValueBool(key, default)
64 val = @entries[ [VIVER_PARAMETER_NS, key] ] || @entries[ [GLOBAL_NS, key] ]
65 if val === nil
66 return default
67 elsif val.downcase =~ /false|no|n|off|0/
68 return false
69 else
70 return true
71 end
72 end
73
74
75 def getFileAndHints(key, default_file, default_hints)
76 # key=file
77 # key=:hint,hint
78 # key=file:hint,hint,hint
79 # key=hint,hint
80
81 if (line = getValue(key, nil)) == nil
82 return default_file, default_hints
83 end
84
85 file, hints = line.split(":", 2)
86
87 if file.include?(",") && hints == nil
88 # key=hint,hint
89 hints = file
90 file = default_file
91 elsif (file === nil || file.empty?)
92 # key=:hint,hint
93 file = default_file
94 end
95
96 if hints === nil
97 return file, default_hints
98 else
99 return file, hints.split(",")
100 end
101 end
102
103
104 def loadViverConfFile(path)
105 $log.info "loading VIVER configuration file: #{path}"
106 # XXX: 暫定: loadViverConfFile
107 begin
108 parse(path, true)
109 $log.success "VIVER configurration file #{path} loaded."
110 rescue
111 $log.warn $!
112 end
113 end
114 private :loadViverConfFile
115
116 def loadViverConf(disks)
117 file_name = getValue("viverconf", DEFAULT_VIVERCONF_FILE_NAME)
118
119 rdirs = Array.new
120 if set?("viverconfdir")
121 rdirs = getValue("viverconfdir", "").split(",")
122 else
123 rdirs = VIVERCONF_RELATIVE_DIR
124 end
125
126 rdirs.each {|p|
127 relative = "/#{p}/#{file_name}"
128 volumes = disks.getAllVolume(relative)
129 volumes.each {|v|
130 loadViverConfFile(v.point + relative)
131 }
132 }
133 end
134
135 def loadUserShellParameter(path)
136 $log.info "loading shell parameters: #{path}"
137 # XXX: 暫定: loadUserShellParameter
138 begin
139 parse(path, true)
140 $log.success "VIVER configurration file #{path} loaded."
141 rescue
142 $log.debugError $!
143 end
144 end
145
146 def outputToFile(path)
147=begin
148 # RUNES 0.3 互換フォーマット
149 File.open(path, "w") {|file|
150 file << "#!/bin/sh\n"
151 @entries.each {|k, v|
152 file << "param_#{k[0]}_#{k[1]}=\"#{v}\"\n"
153 }
154
155 file << "system_MP_ARK=\"#{arkfs.point}\"\n"
156 }
157=end
158 # XXX: 未実装: パラメータの書き出し
159 end
160end
161
162
163$log.debug "#{File.basename(__FILE__, ".*")} loaded"