knjrbfw 0.0.55 → 0.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.55
1
+ 0.0.57
data/knjrbfw.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjrbfw}
8
- s.version = "0.0.55"
8
+ s.version = "0.0.57"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-07-05}
12
+ s.date = %q{2012-07-10}
13
13
  s.description = %q{Including stuff for HTTP, SSH and much more.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
@@ -101,6 +101,7 @@ Gem::Specification.new do |s|
101
101
  "lib/knj/image.rb",
102
102
  "lib/knj/includes/appserver_cli.rb",
103
103
  "lib/knj/includes/require_info.rb",
104
+ "lib/knj/iotop.rb",
104
105
  "lib/knj/ip2location.rb",
105
106
  "lib/knj/ironruby-gtk2/button.rb",
106
107
  "lib/knj/ironruby-gtk2/dialog.rb",
@@ -224,6 +225,7 @@ Gem::Specification.new do |s|
224
225
  "lib/knj/scripts/keepalive.rb",
225
226
  "lib/knj/scripts/php_to_rb_helper.rb",
226
227
  "lib/knj/scripts/process_meta_exec.rb",
228
+ "lib/knj/scripts/speed_test.rb",
227
229
  "lib/knj/scripts/svn_merge.rb",
228
230
  "lib/knj/scripts/upgrade_knjrbfw_checker.rb",
229
231
  "lib/knj/sms.rb",
data/lib/knj/arrayext.rb CHANGED
@@ -334,4 +334,22 @@ module Knj::ArrayExt
334
334
  return ret
335
335
  end
336
336
  end
337
+
338
+ #Returns a given hash in an array by mathing the contents up against another hash.
339
+ def self.array_hash_find(args)
340
+ args[:arr].each do |h|
341
+ found = true
342
+
343
+ args[:args].each do |key, val|
344
+ if h[key] != val
345
+ found = false
346
+ break
347
+ end
348
+ end
349
+
350
+ return h if found
351
+ end
352
+
353
+ return nil
354
+ end
337
355
  end
data/lib/knj/datarow.rb CHANGED
@@ -42,6 +42,12 @@ class Knj::Datarow
42
42
  return @depending_data
43
43
  end
44
44
 
45
+ #Returns true if this class has been initialized.
46
+ def self.initialized?
47
+ return false if !@ob or !@columns_sqlhelper_args
48
+ return true
49
+ end
50
+
45
51
  #This is used by 'Knj::Objects' to find out which other objects should be deleted when an object of this class is deleted automatically. Returns the array that tells about autodelete data.
46
52
  #===Examples
47
53
  #This will trigger Knj::Objects to automatically delete all the users pictures, when deleting the current user.
@@ -90,9 +96,24 @@ class Knj::Datarow
90
96
  if val.is_a?(Array)
91
97
  classname, colname, methodname = *val
92
98
  elsif val.is_a?(Hash)
93
- classname = val[:class]
94
- colname = val[:col]
95
- methodname = val[:method]
99
+ classname, colname, methodname = nil, nil, nil
100
+
101
+ val.each do |hkey, hval|
102
+ case hkey
103
+ when :class
104
+ classname = hval
105
+ when :col
106
+ colname = hval
107
+ when :method
108
+ methodname = hval
109
+ when :depends, :autodelete, :where
110
+ #ignore
111
+ else
112
+ raise "Invalid key for 'has_many': '#{hkey}'."
113
+ end
114
+ end
115
+
116
+ colname = "#{self.name.to_s.split("::").last.to_s.downcase}_id".to_sym if colname.to_s.empty?
96
117
 
97
118
  if val[:depends]
98
119
  self.depending_data << {
@@ -111,15 +132,16 @@ class Knj::Datarow
111
132
  raise "Unknown argument: '#{val.class.name}'."
112
133
  end
113
134
 
135
+ raise "No classname given." if !classname
136
+ methodname = "#{classname.to_s.downcase}s".to_sym if !methodname
137
+ raise "No column was given for '#{self.name}' regarding has-many-class: '#{classname}'." if !colname
138
+
114
139
  if val.is_a?(Hash) and val.key?(:where)
115
140
  where_args = val[:where]
116
141
  else
117
142
  where_args = nil
118
143
  end
119
144
 
120
- raise "No classname given." if !classname
121
- methodname = "#{classname.to_s.downcase}s".to_sym if !methodname
122
-
123
145
  define_method(methodname) do |*args, &block|
124
146
  if args and args[0]
125
147
  list_args = args[0]
@@ -181,7 +203,22 @@ class Knj::Datarow
181
203
  elsif val.is_a?(Array)
182
204
  classname, colname, methodname = *val
183
205
  elsif val.is_a?(Hash)
184
- classname, colname, methodname = val[:class], val[:col], val[:method]
206
+ classname, colname, methodname = nil, nil, nil
207
+
208
+ val.each do |hkey, hval|
209
+ case hkey
210
+ when :class
211
+ classname = hval
212
+ when :col
213
+ colname = hval
214
+ when :method
215
+ methodname = hval
216
+ when :required
217
+ #ignore
218
+ else
219
+ raise "Invalid key for class '#{self.name}' functionality 'has_many': '#{hkey}'."
220
+ end
221
+ end
185
222
 
186
223
  if val[:required]
187
224
  colname = "#{classname.to_s.downcase}_id".to_sym if !colname
@@ -306,6 +343,7 @@ class Knj::Datarow
306
343
 
307
344
  #Returns various data for the objects-sql-helper. This can be used to view various informations about the columns and more.
308
345
  def self.columns_sqlhelper_args
346
+ raise "No SQLHelper arguments has been spawned yet." if !@columns_sqlhelper_args
309
347
  return @columns_sqlhelper_args
310
348
  end
311
349
 
data/lib/knj/gtk2_tv.rb CHANGED
@@ -108,10 +108,7 @@ module Knj::Gtk2::Tv
108
108
  # Knj::Gtk2::Tv.sel(treeview) #=> [1, "Kasper"]
109
109
  def self.sel(tv)
110
110
  selected = tv.selection.selected_rows
111
-
112
- if !tv.model or selected.size <= 0
113
- return nil
114
- end
111
+ return nil if !tv.model or selected.size <= 0
115
112
 
116
113
  iter = tv.model.get_iter(selected[0])
117
114
  returnval = []
data/lib/knj/iotop.rb ADDED
@@ -0,0 +1,58 @@
1
+ #Currently broken - cannot get iotop to return values when running through script :-(
2
+ class Knj::Iotop
3
+ def initialize(args)
4
+ @data = {}
5
+ @mutex = Monitor.new
6
+
7
+ cmd = "iotop -bPk"
8
+
9
+ if args[:pids]
10
+ args[:pids].each do |pid|
11
+ cmd << " --pid=#{pid.to_i}"
12
+ end
13
+ end
14
+
15
+ @stdout = IO.popen(cmd)
16
+
17
+ @thread = Knj::Thread.new do
18
+ @stdout.each_line do |line_str|
19
+ if line_str.match(/^Total\s+disk\s+read:\s+([\d\.]+)\s+(K\/s)\s+\|\s+Total\s+disk\s+write:\s+([\d\.]+)\s+(K\/s)\s*$/i)
20
+ #ignore.
21
+ elsif line_str.match(/ PID PRIO USER DISK READ DISK WRITE SWAPIN IO COMMAND/)
22
+ #ignore.
23
+ elsif match = line_str.match(/^\s*(\d+)\s+(.+?)\s+(.+?)\s+([\d\.]+)\s+(K\/s)\s+([\d\.]+)\s+(K\/s)\s+([\d\.]+)\s+(%)\s+([\d\.]+)\s+(%)\s+(.+)\s*$/)
24
+ @mutex.synchronize do
25
+ pid = match[1].to_i
26
+
27
+ if match[12].index("bestseller") != nil
28
+ print line_str + "\n"
29
+ Knj::Php.print_r(match)
30
+ end
31
+
32
+ @data[pid] = {
33
+ :pid => pid,
34
+ :prio => match[2],
35
+ :user => match[3],
36
+ :disk_read => (match[4].to_f * 1024).to_i,
37
+ :disk_write => (match[6].to_f * 1024).to_i,
38
+ :spawpin => match[8].to_f,
39
+ :io => match[10].to_f,
40
+ :cmd => match[12]
41
+ }
42
+ end
43
+ else
44
+ raise "Could not parse line: '#{line_str}'."
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ #Returns information for the given PID.
51
+ def [](pid)
52
+ @mutex.synchronize do
53
+ pid = pid.to_i
54
+ raise "No such PID: '#{pid}'." if !@data.key?(pid)
55
+ return @data[pid]
56
+ end
57
+ end
58
+ end
data/lib/knj/knj.rb CHANGED
@@ -34,4 +34,13 @@ module Knj
34
34
  def self.p(*args, &block)
35
35
  return Knj::Php.print_r(*args, &block)
36
36
  end
37
+
38
+ def self.handle_return(args)
39
+ if args[:block]
40
+ args[:enum].each(&args[:block])
41
+ return nil
42
+ else
43
+ return Array_enumerator.new(args[:enum])
44
+ end
45
+ end
37
46
  end
@@ -764,10 +764,22 @@ class Knj::Db
764
764
  end
765
765
  end
766
766
 
767
+ #Returns the sign to be used for surrounding tables.
767
768
  def col_table
768
769
  return "`"
769
770
  end
770
771
 
772
+ #Optimizes all tables in the database.
773
+ def optimize(args = nil)
774
+ STDOUT.puts "Beginning optimization of database." if @debug or (args and args[:debug])
775
+ self.tables.list do |table|
776
+ STDOUT.puts "Optimizing table: '#{table.name}'." if @debug or (args and args[:debug])
777
+ table.optimize
778
+ end
779
+
780
+ return nil
781
+ end
782
+
771
783
  #Proxies the method to the driver.
772
784
  #
773
785
  #===Examples
@@ -9,6 +9,8 @@ class Knj::Db::Query_buffer
9
9
  @debug = @args[:debug]
10
10
  @lock = Mutex.new
11
11
 
12
+ STDOUT.puts "Query buffer started." if @debug
13
+
12
14
  begin
13
15
  yield(self)
14
16
  ensure
@@ -34,6 +36,7 @@ class Knj::Db::Query_buffer
34
36
  # buffer.delete(:users, {:id => 5})
35
37
  # end
36
38
  def delete(table, where)
39
+ STDOUT.puts "Delete called on table #{table} with arguments: '#{where}'." if @debug
37
40
  self.query(@args[:db].delete(table, where, :return_sql => true))
38
41
  return nil
39
42
  end
@@ -189,6 +189,7 @@ class Knj::Objects
189
189
  if !args[:joins_skip]
190
190
  datarow_obj = self.datarow_obj_from_args(args_def, list_args, realkey[0])
191
191
  args = datarow_obj.columns_sqlhelper_args
192
+ raise "Couldnt get arguments from SQLHelper." if !args
192
193
  else
193
194
  datarow_obj = @args[:module].const_get(realkey[0])
194
195
  args = args_def
@@ -476,10 +477,14 @@ class Knj::Objects
476
477
 
477
478
  def datarow_from_datarow_argument(datarow_argument)
478
479
  if datarow_argument.is_a?(String)
479
- return Knj::Strings.const_get_full(datarow_argument)
480
+ const = Knj::Strings.const_get_full(datarow_argument)
481
+ else
482
+ const = datarow_argument
480
483
  end
481
484
 
482
- return datarow_argument
485
+ self.load_class(datarow_argument.to_s.split("::").last) if !const.initialized? #Make sure the class is initialized.
486
+
487
+ return const
483
488
  end
484
489
 
485
490
  def not(not_v, val)
data/lib/knj/objects.rb CHANGED
@@ -367,6 +367,15 @@ class Knj::Objects
367
367
  return obj
368
368
  end
369
369
 
370
+ #Same as normal get but returns false if not found instead of raising error.
371
+ def get!(*args, &block)
372
+ begin
373
+ return self.get(*args, &block)
374
+ rescue Knj::Errors::NotFound
375
+ return false
376
+ end
377
+ end
378
+
370
379
  def object_finalizer(id)
371
380
  classname = @objects_idclass[id]
372
381
  if classname
@@ -411,6 +420,10 @@ class Knj::Objects
411
420
  end
412
421
 
413
422
  #Returns an array-list of objects. If given a block the block will be called for each element and memory will be spared if running weak-link-mode.
423
+ #===Examples
424
+ # ob.list(:User) do |user|
425
+ # print "Username: #{user.name}\n"
426
+ # end
414
427
  def list(classname, args = {}, &block)
415
428
  args = {} if args == nil
416
429
  classname = classname.to_sym
@@ -442,6 +455,33 @@ class Knj::Objects
442
455
  end
443
456
  end
444
457
 
458
+ #Yields every object that is missing certain required objects (based on 'has_many' required-argument).
459
+ def list_invalid_required(args, &block)
460
+ enum = Enumerator.new do |yielder|
461
+ classname = args[:class]
462
+ classob = @args[:module].const_get(classname)
463
+ required_data = classob.required_data
464
+
465
+ if required_data and !required_data.empty?
466
+ required_data.each do |req_data|
467
+ self.list(args[:class], :cloned_ubuf => true) do |obj|
468
+ puts "Checking #{obj.classname}(#{obj.id}) for required #{req_data[:class]}." if args[:debug]
469
+ id = obj[req_data[:col]]
470
+
471
+ begin
472
+ raise Knj::Errors::NotFound if !id
473
+ obj_req = self.get(req_data[:class], id)
474
+ rescue Knj::Errors::NotFound
475
+ yielder << {:obj => obj, :type => :required, :id => id, :data => req_data}
476
+ end
477
+ end
478
+ end
479
+ end
480
+ end
481
+
482
+ return Knj.handle_return(:enum => enum, :block => block)
483
+ end
484
+
445
485
  #Returns select-options-HTML for inserting into a HTML-select-element.
446
486
  def list_opts(classname, args = {})
447
487
  Knj::ArrayExt.hash_sym(args)
@@ -768,9 +808,7 @@ class Knj::Objects
768
808
  end
769
809
 
770
810
  #Delete any translations that has been set on the object by 'has_translation'-method.
771
- if object.class.translations
772
- _kas.trans_del(object)
773
- end
811
+ _kas.trans_del(object) if object.class.translations
774
812
 
775
813
  #If a buffer is given in arguments, then use that to delete the object.
776
814
  if args and buffer = args[:db_buffer]
@@ -867,6 +905,10 @@ class Knj::Objects
867
905
  def clean_all
868
906
  self.clean(@objects.keys)
869
907
  end
908
+
909
+ def classes_loaded
910
+ return @objects.keys
911
+ end
870
912
  end
871
913
 
872
914
  require "#{$knjpath}objects/objects_sqlhelper"
data/lib/knj/os.rb CHANGED
@@ -143,8 +143,7 @@ module Knj::Os
143
143
 
144
144
  #Runs a command as a process of its own and wont block or be depended on this process.
145
145
  def self.subproc(cmd)
146
- cmd = cmd.to_s + " >> /dev/null 2>&1 &"
147
- %x[#{cmd}]
146
+ %x[#{cmd} >> /dev/null 2>&1 &]
148
147
  end
149
148
 
150
149
  #Returns the xauth file for GDM.
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "timeout"
4
+
5
+ args = {}
6
+ ARGV.each do |arg|
7
+ if match = arg.match(/^tmpfile=(.+)$/)
8
+ args["tmpfile"] = match[1]
9
+ else
10
+ raise "Unknown argument: '#{arg}'."
11
+ end
12
+ end
13
+
14
+ raise "No 'tmpfile' given in arguments." if !args["tmpfile"]
15
+
16
+ #8 kb string.
17
+ str = ("0" * 1024) * 8
18
+ strl = str.length
19
+
20
+ count = 0
21
+ time_begin = Time.now.to_f
22
+
23
+ puts "Starting to write file."
24
+ begin
25
+ Timeout.timeout(4) do
26
+ File.open(args["tmpfile"], "w") do |fp|
27
+ fp.sync = true
28
+
29
+ loop do
30
+ fp.write(str)
31
+ count += strl
32
+ end
33
+ end
34
+ end
35
+ rescue Timeout::Error
36
+ #ignore
37
+ end
38
+
39
+ secs = Time.now.to_f - time_begin
40
+ mb_sec = ((count / secs) / 1024) / 1024
41
+
42
+ puts "#{mb_sec.round(2)} mb/s in #{secs.round(1)} seconds."
43
+
44
+
45
+ puts "Starting to read the file again."
46
+ count = 0
47
+ time_begin = Time.now.to_f
48
+
49
+ begin
50
+ Timeout.timeout(4) do
51
+ File.open(args["tmpfile"], "r") do |fp|
52
+ loop do
53
+ read = fp.read(4096)
54
+ count += read.length
55
+ end
56
+ end
57
+ end
58
+ rescue Timeout::Error
59
+ #ignore
60
+ end
61
+
62
+ secs = Time.now.to_f - time_begin
63
+ mb_sec = ((count / secs) / 1024) / 1024
64
+
65
+ puts "#{mb_sec.round(2)} mb/s in #{secs.round(1)} seconds."
66
+
67
+ File.unlink(args["tmpfile"])
data/spec/objects_spec.rb CHANGED
@@ -76,9 +76,9 @@ describe "Objects" do
76
76
 
77
77
  #Stress it to test threadsafety...
78
78
  threads = []
79
- 0.upto(10) do |tc|
79
+ 0.upto(5) do |tc|
80
80
  threads << Knj::Thread.new do
81
- 0.upto(10) do |ic|
81
+ 0.upto(5) do |ic|
82
82
  user = $ob.add(:User, {:username => "User #{tc}-#{ic}"})
83
83
  raise "No user returned." if !user
84
84
  $ob.delete(user)
@@ -116,9 +116,9 @@ describe "Objects" do
116
116
  )
117
117
 
118
118
  threads = []
119
- 0.upto(10) do
119
+ 0.upto(5) do
120
120
  threads << Knj::Thread.new do
121
- 0.upto(15) do
121
+ 0.upto(5) do
122
122
  ret = $ob2.add(:Group, {:groupname => "User 1"}, {:skip_ret => true})
123
123
  raise "Expected empty return but got something: #{ret}" if ret
124
124
  end
@@ -228,6 +228,28 @@ describe "Objects" do
228
228
  :project_id => 1
229
229
  })
230
230
 
231
+ begin
232
+ $obb.add(:Task, {:name => "Test task"})
233
+ raise "Method should fail but didnt."
234
+ rescue
235
+ #ignore.
236
+ end
237
+
238
+
239
+ #Test 'list_invalid_required'.
240
+ $db.insert(:Task, :name => "Invalid require")
241
+ id = $db.last_id
242
+ found = false
243
+
244
+ $ob.list_invalid_required(:class => :Task) do |d|
245
+ raise "Expected object ID to be #{id} but it wasnt: #{d[:obj].id}" if d[:obj].id.to_i != id.to_i
246
+ $ob.delete(d[:obj])
247
+ found = true
248
+ end
249
+
250
+ raise "Expected to find a task but didnt." if !found
251
+
252
+
231
253
  ret_proc = []
232
254
  $ob.list(:Task) do |task|
233
255
  ret_proc << task
@@ -289,7 +311,7 @@ describe "Objects" do
289
311
 
290
312
  it "should be able to to multiple additions and delete objects through a buffer" do
291
313
  objs = []
292
- 0.upto(10000) do
314
+ 0.upto(500) do
293
315
  objs << {:name => :Kasper}
294
316
  end
295
317
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: knjrbfw
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.55
5
+ version: 0.0.57
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-07-05 00:00:00 +02:00
13
+ date: 2012-07-10 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -195,6 +195,7 @@ files:
195
195
  - lib/knj/image.rb
196
196
  - lib/knj/includes/appserver_cli.rb
197
197
  - lib/knj/includes/require_info.rb
198
+ - lib/knj/iotop.rb
198
199
  - lib/knj/ip2location.rb
199
200
  - lib/knj/ironruby-gtk2/button.rb
200
201
  - lib/knj/ironruby-gtk2/dialog.rb
@@ -318,6 +319,7 @@ files:
318
319
  - lib/knj/scripts/keepalive.rb
319
320
  - lib/knj/scripts/php_to_rb_helper.rb
320
321
  - lib/knj/scripts/process_meta_exec.rb
322
+ - lib/knj/scripts/speed_test.rb
321
323
  - lib/knj/scripts/svn_merge.rb
322
324
  - lib/knj/scripts/upgrade_knjrbfw_checker.rb
323
325
  - lib/knj/sms.rb
@@ -379,7 +381,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
379
381
  requirements:
380
382
  - - ">="
381
383
  - !ruby/object:Gem::Version
382
- hash: -4128197618714444759
384
+ hash: 2259691456146427646
383
385
  segments:
384
386
  - 0
385
387
  version: "0"