Class: Yast::MiscClass
- Inherits:
-
Module
- Object
- Module
- Yast::MiscClass
- Defined in:
- ../../library/general/src/modules/Misc.rb
Instance Method Summary (collapse)
-
- (Object) CustomSysconfigRead(key, defval, location)
Try to read value from sysconfig file and return the result if successful.
-
- (String) hardware_name(hardware_entry)
common function to extract 'name' of hardware.
- - (Object) main
-
- (Object) ReadAlternateFile(first, second)
try to read first file, if it doesn't exist, read second files must reside below /usr/lib/YaST2 files must have ycp syntax.
-
- (Hash) RunCommandWithTimeout(run_command, log_command, script_time_out)
Runs a bash command with timeout.
-
- (Hash) RunDumbTimeout(command, log_command, seconds)
Run - with a timeout - on dumb terminal to disable colors etc - using 'exit $?' because of buggy behavior '.background vs.
-
- (Hash) SplitOptions(options, optmap)
MergeOptions Merges “opt1=val1 opt2=val2 …” and $[“opta”:“vala”, …“] to $[”opt1“:”val1“, ”opt2“:”val2“, ”opta“:”vala“, …] as needed by modules.conf agent.
-
- (Object) SysconfigRead(sysconfig_path, defaultv)
SysconfigRead().
-
- (Boolean) SysconfigWrite(level, values)
SysconfigWrite() write list of sysyconfig entries via rcconfig agent.
-
- (String) translate(lmap, lang)
Define a macro that looks up a localized string in a language map of the form $[ “default” : “Defaultstring”, “de” : “German.…”, …].
Instance Method Details
- (Object) CustomSysconfigRead(key, defval, location)
Try to read value from sysconfig file and return the result if successful. Function reads from arbitrary sysconfig file, for which the agent doesn't exist: e.g. from different partition like /mnt/etc/sysconfig/file.
215 216 217 218 219 220 221 222 223 224 225 226 |
# File '../../library/general/src/modules/Misc.rb', line 215 def CustomSysconfigRead(key, defval, location) return defval if location == "" custom_path = Builtins.topath(location) SCR.RegisterAgent( custom_path, term(:ag_ini, term(:SysConfigFile, location)) ) ret = SysconfigRead(Builtins.add(custom_path, key), defval) SCR.UnregisterAgent(custom_path) ret end |
- (String) hardware_name(hardware_entry)
common function to extract 'name' of hardware
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File '../../library/general/src/modules/Misc.rb', line 67 def hardware_name(hardware_entry) hardware_entry = deep_copy(hardware_entry) sub_vendor = "" sub_device = "" sub_vendor = Ops.get_string(hardware_entry, "sub_vendor", "") sub_device = Ops.get_string(hardware_entry, "sub_device", "") if sub_vendor != "" && sub_device != "" return Ops.add(Ops.add(sub_vendor, "\n"), sub_device) else vendor = Ops.get_string(hardware_entry, "vendor", "") return Ops.add( Ops.add(vendor, vendor != "" ? "\n" : ""), Ops.get_string(hardware_entry, "device", "") ) end end |
- (Object) main
36 37 38 39 40 41 42 43 44 45 46 |
# File '../../library/general/src/modules/Misc.rb', line 36 def main Yast.import "UI" Yast.import "Mode" # Message after finishing installation and before the system # boots for the first time. # @boot_msg = "" end |
- (Object) ReadAlternateFile(first, second)
try to read first file, if it doesn't exist, read second files must reside below /usr/lib/YaST2 files must have ycp syntax
56 57 58 59 60 |
# File '../../library/general/src/modules/Misc.rb', line 56 def ReadAlternateFile(first, second) result = SCR.Read(path(".target.yast2"), [first, nil]) result = SCR.Read(path(".target.yast2"), second) if result == nil deep_copy(result) end |
- (Hash) RunCommandWithTimeout(run_command, log_command, script_time_out)
Runs a bash command with timeout.
Structure:
Returns map
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File '../../library/general/src/modules/Misc.rb', line 243 def RunCommandWithTimeout(run_command, log_command, script_time_out) Builtins.y2milestone( "Running command \"%1\" in background...", log_command ) processID = Convert.to_integer( SCR.Execute(path(".process.start_shell"), run_command) ) if processID == 0 Builtins.y2error("Cannot run '%1'", run_command) return nil end script_out = [] script_err = [] time_spent = 0 return_code = nil timed_out = false sleep_step = 200 # ms script_time_out = Ops.multiply(script_time_out, 1000) # while continuing is needed and while it is possible while !timed_out running = Convert.to_boolean( SCR.Read(path(".process.running"), processID) ) # debugging #165821 if Ops.modulo(time_spent, 100000) == 0 Builtins.y2milestone("running: %1", running) flag = "/tmp/SourceManagerTimeout" if SCR.Read(path(".target.size"), flag) != -1 Builtins.y2milestone("Emergency exit") SCR.Execute(path(".target.remove"), flag) break end end break if !running # at last, enable aborting the sync if Mode.commandline Builtins.sleep(sleep_step) else ui = UI.TimeoutUserInput(sleep_step) if ui == :abort Builtins.y2milestone("aborted") timed_out = true break end end # time-out if Ops.greater_or_equal(time_spent, script_time_out) Builtins.y2error("Command timed out after %1 msec", time_spent) timed_out = true end time_spent = Ops.add(time_spent, sleep_step) end Builtins.y2milestone("Time spent: %1 msec", time_spent) # fetching the return code if not timed-out if !timed_out Builtins.y2milestone("getting output") script_out = Builtins.splitstring( Convert.to_string(SCR.Read(path(".process.read"), processID)), "\n" ) Builtins.y2milestone("getting errors") script_err = Builtins.splitstring( Convert.to_string(SCR.Read(path(".process.read_stderr"), processID)), "\n" ) Builtins.y2milestone("getting status") return_code = Convert.to_integer( SCR.Read(path(".process.status"), processID) ) end Builtins.y2milestone("killing") SCR.Execute(path(".process.kill"), processID) # release the process from the agent SCR.Execute(path(".process.release"), processID) command_ret = { "exit" => return_code, "stdout" => script_out, "stderr" => script_err } Ops.set(command_ret, "timed_out", time_spent) if timed_out deep_copy(command_ret) end |
- (Hash) RunDumbTimeout(command, log_command, seconds)
Run - with a timeout - on dumb terminal to disable colors etc - using 'exit $?' because of buggy behavior '.background vs. ZMD' (FIXME still needed???)
346 347 348 349 350 351 352 353 354 |
# File '../../library/general/src/modules/Misc.rb', line 346 def RunDumbTimeout(command, log_command, seconds) dumb_format = "export TERM=dumb; %1; exit $?" dumb_command = Builtins.sformat(dumb_format, command) dumb_log_command = Builtins.sformat(dumb_format, log_command) # explicit export in case TERM was not in the environment ret = RunCommandWithTimeout(dumb_command, dumb_log_command, seconds) ret = {} if ret == nil deep_copy(ret) end |
- (Hash) SplitOptions(options, optmap)
MergeOptions Merges “opt1=val1 opt2=val2 …” and $[“opta”:“vala”, …“] to $[”opt1“:”val1“, ”opt2“:”val2“, ”opta“:”vala“, …] as needed by modules.conf agent
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File '../../library/general/src/modules/Misc.rb', line 149 def SplitOptions(, optmap) optmap = deep_copy(optmap) # step 1: split "opt1=val1 opt2=val2 ..." # to ["opt1=val1", "opt2=val2", "..."] = Builtins.splitstring(, " ") Builtins.foreach() do || = Builtins.splitstring(, "=") if Builtins.size() == 1 && Ops.get_string(optmap, , "") == "" # single argument Ops.set(optmap, , "") elsif Builtins.size() == 2 # argument with value Ops.set( optmap, Ops.get_string(, 0, ""), Ops.get_string(, 1, "") ) end end deep_copy(optmap) end |
- (Object) SysconfigRead(sysconfig_path, defaultv)
SysconfigRead()
Try an SCR::Read(…) and return the result if successful. On failure return the the second parameter (default value)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File '../../library/general/src/modules/Misc.rb', line 187 def SysconfigRead(sysconfig_path, defaultv) local_ret = Convert.to_string(SCR.Read(sysconfig_path)) if local_ret == nil Builtins.y2warning( "Failed reading '%1', using default value", sysconfig_path ) return defaultv else Builtins.y2milestone("%1: '%2'", sysconfig_path, local_ret) return local_ret end end |
- (Boolean) SysconfigWrite(level, values)
SysconfigWrite() write list of sysyconfig entries via rcconfig agent
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File '../../library/general/src/modules/Misc.rb', line 115 def SysconfigWrite(level, values) values = deep_copy(values) result = true if level == path(".") level = path(".sysconfig") else level = Ops.add(path(".sysconfig"), level) end Builtins.foreach(values) do |entry| if Builtins.size(entry) != 2 Builtins.y2error("bad entry in rc_write()") else if !SCR.Write( Ops.add(level, Ops.get_path(entry, 0, path("."))), Ops.get_string(entry, 1, "") ) result = false end end end result end |
- (String) translate(lmap, lang)
Define a macro that looks up a localized string in a language map of the form $[ “default” : “Defaultstring”, “de” : “German.…”, …]
96 97 98 99 100 101 102 103 104 105 |
# File '../../library/general/src/modules/Misc.rb', line 96 def translate(lmap, lang) lmap = deep_copy(lmap) t = Ops.get_string(lmap, lang, "") if Builtins.size(t) == 0 && Ops.greater_than(Builtins.size(lang), 2) t = Ops.get_string(lmap, Builtins.substring(lang, 0, 2), "") end t = Ops.get_string(lmap, "default", "") if Builtins.size(t) == 0 t end |