Class: Yast::MiscClass

Inherits:
Module
  • Object
show all
Defined in:
../../library/general/src/modules/Misc.rb

Instance Method Summary (collapse)

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.

Examples:

Misc::CustomSysconfigRead (“INSTALLED_LANGUAGES”, “”, Installation::destdir + “/etc/sysconfig/language”);

Parameters:

  • key (String)

    Key of the value we want to read from sysconfig file.

  • defaultv

    Default value

  • location (String)

    Full path to target sysconfig file.

Returns:

  • Success –> Result of SCR::Read
    Failure –> Default value



207
208
209
210
211
212
213
214
215
216
217
218
# File '../../library/general/src/modules/Misc.rb', line 207

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

Parameters:

  • hardware_entry (Hash)

    map map of .probe entry

Returns:

  • (String)

    vendor and device name



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File '../../library/general/src/modules/Misc.rb', line 66

def hardware_name(hardware_entry)
  hardware_entry = deep_copy(hardware_entry)
  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
# 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

Parameters:

  • first (String)

    string name of first file to try

  • second (String)

    string name of second file to try

Returns:

  • (Object)

    content of file



55
56
57
58
59
# File '../../library/general/src/modules/Misc.rb', line 55

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 

Parameters:

  • run_command (String)

    what to run

  • log_command (String)

    what to log (passwords masked)

  • script_time_out (Fixnum)

    in sec.

Returns:

  • (Hash)

    with out, err and ret_code



234
235
236
237
238
239
240
241
242
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
# File '../../library/general/src/modules/Misc.rb', line 234

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
  until timed_out
    running = Convert.to_boolean(
      SCR.Read(path(".process.running"), processID)
    )
    # debugging #165821
    if Ops.modulo(time_spent, 100_000) == 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???)

Parameters:

  • command (String)

    a command

  • log_command (String)

    a command to log

  • seconds (Fixnum)

    timeout

Returns:

  • (Hash)

    with out, err and ret_code



337
338
339
340
341
342
343
344
345
# File '../../library/general/src/modules/Misc.rb', line 337

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

Parameters:

  • options (String)

    string module options, e.g. “opt1=val1 opt2=val2 …”

  • optmap (Hash)

    map possible old options $[“opta”:“vala”, …]

Returns:

  • (Hash)

    $[“opt1”:“val1”, “opt2”:“val2”, …]



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File '../../library/general/src/modules/Misc.rb', line 142

def SplitOptions(options, optmap)
  optmap = deep_copy(optmap)
  # step 1: split "opt1=val1 opt2=val2 ..."
  # to ["opt1=val1", "opt2=val2", "..."]

  options_split = Builtins.splitstring(options, " ")

  Builtins.foreach(options_split) do |options_element|
    options_values = Builtins.splitstring(options_element, "=")
    if Builtins.size(options_values) == 1 &&
        Ops.get_string(optmap, options_element, "") == ""
      # single argument
      Ops.set(optmap, options_element, "")
    elsif Builtins.size(options_values) == 2
      # argument with value
      Ops.set(
        optmap,
        Ops.get_string(options_values, 0, ""),
        Ops.get_string(options_values, 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)

Parameters:

  • sysconfig_path (Yast::Path)

    Sysconfig SCR path.

  • defaultv (String)

    Default value

Returns:

  • Success –> Result of SCR::Read
    Failure –> Default value



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File '../../library/general/src/modules/Misc.rb', line 179

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

Parameters:

  • level (Yast::Path)

    path behind .sysconfig for all values

  • values (Array<Array>)

    list of [ .NAME, value] lists

Returns:

  • (Boolean)

    false if SCR::Write reported error



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File '../../library/general/src/modules/Misc.rb', line 110

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.…”, …]

Parameters:

  • lmap (Hash)

    map map of language codes and translations e.g. $[ “default” : “Defaultstring”, “de” : “German.…”, …]

  • lang (String)

    string language as ISO code, either 2 chars (de) or 5 chars (de_DE)

Returns:

  • (String)

    translation



91
92
93
94
95
96
97
98
99
100
# File '../../library/general/src/modules/Misc.rb', line 91

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