helper_classes 0.3.1 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97717fc8635b0abbbd5f199f5cdf4859c6beb0a8
4
- data.tar.gz: e70548a8c8ac0facbcef3b4885a4da12ba8fcd58
3
+ metadata.gz: fae65be85ffe47f9f08c0454d77b14a9cb65c1ec
4
+ data.tar.gz: f3391e02c3bb88c9d30ab6539f68dfd33ec5b1e4
5
5
  SHA512:
6
- metadata.gz: 3bf51d94db7ffadd84521dfe4e5e884a906966ab8c7aa387b846d13e5c8fbee16ec2fe2ab67eb82c82cba0b7fe412628e6106663443ad0e82bea03be6a88eaba
7
- data.tar.gz: dd1deac10b92dfacbcd6aad3dc409712b08daabc05edb1a312655d8c56e4b14391fd3eb3ba5a8aa8b1c1196b99603c31aa515bf063c373d7b2bc6b1156ab92c4
6
+ metadata.gz: 945c14738b13388cbe87c9b0f1fa31579d4b34c52fded06710b8deea1112fa31ff2111eae18be4332114c411e60f1cb0e7e3bbf97657cc8627d9238bf37de64e
7
+ data.tar.gz: 047748387d592edb99ce9e40885ff6bf0c9687106e11f459eb7b1658f77df168aed8baf06700bbbd95e39fea37e8e71ee1468c506b9bbf1d9391033aa8bc3a99
@@ -1,13 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'helper_classes'
3
- s.version = '0.3.1'
4
- s.date = '2015-05-12'
3
+ s.version = '0.3.5'
4
+ s.date = '2015-06-20'
5
5
  s.summary = 'Hash._accessor Array.to_sym and DPuts'
6
6
  s.description = 'Added accessors to Hash, to_sym to Array and a nice debugging-interface called DPuts'
7
7
  s.authors = ['Linus Gasser']
8
8
  s.email = 'ineiti@linusetviviane.ch'
9
9
 
10
- s.files = `git ls-files -z`.split("\x0")
10
+ s.files = `if [ -d '.git' ]; then git ls-files -z; fi`.split("\x0")
11
11
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
12
12
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
13
13
  s.require_paths = ['lib']
@@ -4,13 +4,28 @@ module HelperClasses
4
4
 
5
5
  module DPuts
6
6
  extend self
7
- attr_accessor :mutex, :silent, :show_time, :terminal_width, :log_file
7
+ attr_accessor :mutex, :silent, :show_time, :terminal_width, :log_file,
8
+ :logall_file, :max_msg_len
8
9
 
9
10
  @mutex = Mutex.new
10
11
  @silent = false
11
12
  @show_time = false
12
13
  @terminal_width = 160
14
+ @max_msg_len = 8192
13
15
  @log_file = false
16
+ @logall_file = false
17
+
18
+ def logfile_valid(f)
19
+ return f && f.to_s.length > 0 && File.exists?(File.dirname(f))
20
+ end
21
+
22
+ def dputs_write(str)
23
+ if logfile_valid(DPuts.logall_file)
24
+ IO.write(DPuts.logall_file, str, mode: 'a')
25
+ else
26
+ puts str
27
+ end
28
+ end
14
29
 
15
30
  def dputs_out(n, s, call)
16
31
  return if DPuts.silent
@@ -27,8 +42,9 @@ module HelperClasses
27
42
  show = (now.to_i / 3600).floor != ($dputs_time.to_i / 3600).floor
28
43
  end
29
44
  if show
30
- puts "\n *** It is now: " +
31
- Time.now.strftime('%Y-%m-%d %H:%M:%S')
45
+ str = "\n *** It is now: " +
46
+ Time.now.strftime('%Y-%m-%d %H:%M:%S') + "\n"
47
+ dputs_write(str)
32
48
  $dputs_time = now
33
49
  end
34
50
  end
@@ -41,22 +57,25 @@ module HelperClasses
41
57
  func.to_s).ljust(30, ['X', 'x', '*', '-', '.', ' '][n])
42
58
  lines = []
43
59
  pos = 0
44
- while (pos < s.length)
60
+ # Don't show enormous strings
61
+ s_trunc = s[0..DPuts.max_msg_len]
62
+ while (pos < s_trunc.length)
45
63
  len = width
46
- if s.length - pos > width
47
- len = s.rindex(/[, .;=&>]/, pos + width)
64
+ if s_trunc.length - pos > width
65
+ len = s_trunc.rindex(/[, .;=&>]/, pos + width)
48
66
  len = len ? len - pos + 1 : width
49
67
  if len < width / 2
50
68
  len = width
51
69
  end
52
70
  end
53
- lines.push s.slice(pos, len)
71
+ lines.push s_trunc.slice(pos, len)
54
72
  pos += len
55
73
  end
56
- puts who + ' ' + lines.shift.to_s
74
+ str = who + ' ' + lines.shift.to_s + "\n"
57
75
  lines.each { |l|
58
- puts ' ' * (32) + l
76
+ str += ' ' * (32) + l + "\n"
59
77
  }
78
+ dputs_write(str)
60
79
  end
61
80
  end
62
81
 
@@ -97,15 +116,16 @@ module HelperClasses
97
116
 
98
117
  def log_msg(mod, msg)
99
118
  dputs(1) { "Info from #{mod}: #{msg}" }
100
- return if not DPuts.log_file
101
- File.open(DPuts.log_file, 'a') { |f|
102
- str = Time.now.strftime("%a %y.%m.%d-%H:%M:%S #{mod}: #{msg}")
103
- f.puts str
104
- }
119
+ if logfile_valid(DPuts.log_file)
120
+ File.open(DPuts.log_file, 'a') { |f|
121
+ str = Time.now.strftime("%a %y.%m.%d-%H:%M:%S #{mod}: #{msg}")
122
+ f.puts str
123
+ }
124
+ end
105
125
  end
106
126
 
107
127
  def dlog_msg(mod, msg)
108
- ddputs(1){"Info from #{mod}: #{msg}"}
128
+ ddputs(1) { "Info from #{mod}: #{msg}" }
109
129
  end
110
130
  end
111
131
  end
@@ -91,6 +91,15 @@ module HelperClasses
91
91
  )
92
92
  end
93
93
 
94
+ def reload
95
+ return unless @system == :ArchLinux
96
+ System.run_bool('/usr/bin/systemctl daemon-reload')
97
+ end
98
+
99
+ def daemon_reload
100
+ reload
101
+ end
102
+
94
103
  def enable_start(service)
95
104
  enable(service)
96
105
  start(service)
@@ -25,11 +25,12 @@ module HelperClasses
25
25
  def rescue_all(msg = 'Error')
26
26
  begin
27
27
  yield
28
- rescue Exception => e
28
+ rescue StandardError => e
29
29
  dputs(0) { "#{Time.now.strftime('%a %y.%m.%d-%H:%M:%S')} - #{msg}" }
30
30
  dputs(0) { "#{e.inspect}" }
31
31
  dputs(0) { "#{e.to_s}" }
32
32
  e.backtrace.each { |l| dputs(0) { l } }
33
+ return false
33
34
  end
34
35
  end
35
36
 
@@ -49,5 +50,37 @@ module HelperClasses
49
50
  return ''
50
51
  end
51
52
  end
53
+
54
+ # Returns the stratum of the ntpd-synchronization:
55
+ # 16 -> not synchronized
56
+ # 2..15 -> synchronized
57
+ def ntpdstratum
58
+ ret = System.run_str('ntpq -c "rv 0 stratum"')
59
+ return 16 if ret =~ /connection refused/i
60
+ stratum = ret.gsub(/.*=/, '')
61
+ return stratum
62
+ end
63
+
64
+ # Waits for NTP to be synchronized or for _n_ seconds
65
+ def ntpd_wait(n = 60)
66
+ Thread.new {
67
+ (1..n).each {
68
+ break if System.ntpdstratum < 16
69
+ sleep 1
70
+ }
71
+ yield
72
+ }
73
+ end
74
+
75
+ # Returns the offset of the ntpd-synchronization:
76
+ # nil -> not synchronized
77
+ # else -> synchronization in ms
78
+ def ntpdoffset
79
+ ret = System.run_str('ntpq -c "rv 0 stratum,offset"')
80
+ return nil if ret =~ /connection refused/i
81
+ stratum, offset = ret.split(/, /).collect { |s| s.gsub(/.*=/, '') }
82
+ return nil if stratum == '16'
83
+ return offset
84
+ end
52
85
  end
53
86
  end
@@ -0,0 +1,17 @@
1
+ class VirtualMethodCalledError < RuntimeError
2
+ attr :name
3
+ def initialize(name)
4
+ super("Virtual function '#{name}' called")
5
+ @name = name
6
+ end
7
+ end
8
+
9
+ class Module
10
+ def virtual(*methods)
11
+ methods.each do |m|
12
+ define_method(m) {
13
+ raise VirtualMethodCalledError, m
14
+ }
15
+ end
16
+ end
17
+ end
@@ -1,7 +1,3 @@
1
- require 'helper_classes/arraysym'
2
- require 'helper_classes/dputs'
3
- require 'helper_classes/hashaccessor'
4
- require 'helper_classes/readconfig'
5
- require 'helper_classes/service'
6
- require 'helper_classes/system'
7
- require 'helper_classes/timing'
1
+ Dir.glob('helper_classes/*.rb').each { |d|
2
+ require d
3
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helper_classes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linus Gasser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-12 00:00:00.000000000 Z
11
+ date: 2015-06-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Added accessors to Hash, to_sym to Array and a nice debugging-interface
14
14
  called DPuts
@@ -29,6 +29,7 @@ files:
29
29
  - lib/helper_classes/service.rb
30
30
  - lib/helper_classes/system.rb
31
31
  - lib/helper_classes/timing.rb
32
+ - lib/helper_classes/virtual.rb
32
33
  - test/hc_service.rb
33
34
  - test/test.rb
34
35
  - test/test_arraysym.rb