splib 1.1 → 1.2

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/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ 1.2
2
+ * Added Array#fixed_flatten for pre 1.9 versions
3
+ * Remove IO.binread from CodeReloader for pre 1.9 versions
4
+ * Use array for Conversion information storage since we
5
+ only have Hash ordering in 1.9
6
+ * Use #chr for pre 1.9
7
+ * Make Splib.exec a wrapper for normal and threaded versions
8
+ Check platform and use threaded version when on java
9
+ automatically
1
10
  1.1
2
11
  * Added Splib.type_of? method to the Constants library
3
12
  * Splib.find_const returns nil if constant not found
@@ -17,6 +17,15 @@ The Spox Library is collection of helper methods and classes.
17
17
 
18
18
  {rip}[http://hellorip.com/about.html] makes it easy to install directly from a github repository.
19
19
 
20
+ === Testing
21
+
22
+ This library has been tested on:
23
+
24
+ * Ruby 1.8.6-p376
25
+ * Ruby 1.8.7-p248
26
+ * Ruby 1.9.1-p383
27
+ * JRuby 1.4
28
+
20
29
  === Usage
21
30
 
22
31
  The Spox Library has various things located within it. The Splib#load method will allow you to load individual parts of the library, or the entire thing into your program. Lets take a quick look at some of the things available from this library.
@@ -1,5 +1,6 @@
1
1
  module Splib
2
- LIBS = [:CodeReloader,
2
+ LIBS = [:Array,
3
+ :CodeReloader,
3
4
  :Constants,
4
5
  :Conversions,
5
6
  :Exec,
@@ -0,0 +1,33 @@
1
+ module Splib
2
+ # Only add this if we are in the 1.8 world and no one
3
+ # else has added it yet
4
+ if(Object::RUBY_VERSION < '1.9.0' && ![].respond_to?(:fixed_flatten))
5
+ class ::Array
6
+ # 1.9 compatible flatten method that allows
7
+ # a level parameter
8
+ def fixed_flatten(level = -1)
9
+ arr = self
10
+ case
11
+ when level < 0
12
+ arr.flatten!
13
+ when level == 0
14
+ self
15
+ when level > 0
16
+ arr = []
17
+ curr = self
18
+ level.times do
19
+ curr.each do |elm|
20
+ if(elm.respond_to?(:to_ary))
21
+ elm.each{|e| arr << e }
22
+ else
23
+ arr << elm
24
+ end
25
+ end
26
+ curr = arr.dup
27
+ end
28
+ end
29
+ arr
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,4 +1,12 @@
1
1
  module Splib
2
+ # path:: path to file
3
+ # Read contents of file
4
+ def self.read_file(path)
5
+ file = File.open(path, 'rb')
6
+ cont = file.read
7
+ file.close
8
+ cont
9
+ end
2
10
  # path:: path to ruby file
3
11
  # type:: return constants only of given type
4
12
  # Find all constants in a given ruby file. Array of
@@ -7,7 +15,7 @@ module Splib
7
15
  raise ArgumentError.new('Failed to locate plugin file') unless File.exists?(path)
8
16
  consts = []
9
17
  sandbox = Module.new
10
- sandbox.module_eval(IO.binread(path))
18
+ sandbox.module_eval(self.read_file(path))
11
19
  sandbox.constants.each do |const|
12
20
  klass = sandbox.const_get(const)
13
21
  if(type.nil? || (type && klass < type))
@@ -27,7 +35,7 @@ module Splib
27
35
  else
28
36
  holder = self.create_holder(path)
29
37
  end
30
- holder.module_eval(IO.binread(path))
38
+ holder.module_eval(self.read_file(path))
31
39
  holder
32
40
  end
33
41
 
@@ -28,7 +28,7 @@ module Splib
28
28
  def self.type_of?(a, b)
29
29
  case b
30
30
  when String
31
- if(a.class.to_s.slice(0) == '#')
31
+ if(a.class.to_s.slice(0).chr == '#')
32
32
  name = a.class.to_s
33
33
  return name.slice(name.index('::')+2, name.length) == b
34
34
  else
@@ -3,19 +3,21 @@ module Splib
3
3
  # Converts seconds into a human readable string (This is an estimate
4
4
  # and does not account for leaps)
5
5
  def self.format_seconds(secs)
6
- arg = {:year => 31536000,
7
- :month => 2678400,
8
- :week => 604800,
9
- :day => 86400,
10
- :hour => 3600,
11
- :minute => 60,
12
- :second => 1}
6
+ arg = [{:year => 31536000},
7
+ {:month => 2678400},
8
+ {:week => 604800},
9
+ {:day => 86400},
10
+ {:hour => 3600},
11
+ {:minute => 60},
12
+ {:second => 1}]
13
13
  res = ''
14
- arg.each_pair do |k,v|
15
- z = (secs / v).to_i
16
- next unless z > 0
17
- res += " #{z} #{k}#{z == 1 ? '':'s'}"
18
- secs = secs % v
14
+ arg.each do |val|
15
+ val.each_pair do |k,v|
16
+ z = (secs / v).to_i
17
+ next unless z > 0
18
+ res += " #{z} #{k}#{z == 1 ? '':'s'}"
19
+ secs = secs % v
20
+ end
19
21
  end
20
22
  res = '0 seconds' if res.empty?
21
23
  return res.strip
@@ -1,35 +1,98 @@
1
1
  require 'timeout'
2
2
 
3
3
  module Splib
4
+
5
+ # command:: command string to execute
6
+ # timeout:: length of time to execute
7
+ # maxbytes:: maximum number return bytes allowed
8
+ # Execute system command. This is a wrapper method
9
+ # that will redirect to the proper command
10
+ def self.exec(*args)
11
+ if(RUBY_PLATFORM == 'java')
12
+ thread_exec(*args)
13
+ else
14
+ standard_exec(*args)
15
+ end
16
+ end
17
+
4
18
  # command:: command to execute
5
19
  # timeout:: maximum number of seconds to run
6
20
  # maxbytes:: maximum number of result bytes to accept
7
21
  # Execute a system command (use with care)
8
- def self.exec(command, timeout=10, maxbytes=500)
22
+ # This is the normal exec command that is used
23
+ def self.standard_exec(command, timeout=10, maxbytes=500)
24
+ timout = timeout.to_i
25
+ maxbytes = maxbytes.to_i
9
26
  output = []
10
27
  pro = nil
11
28
  begin
12
- Timeout::timeout(timeout) do
29
+ if(timeout > 0)
30
+ Timeout::timeout(timeout) do
31
+ pro = IO.popen(command)
32
+ until(pro.closed? || pro.eof?)
33
+ output << pro.getc.chr
34
+ if(maxbytes > 0 && output.size > maxbytes)
35
+ raise IOError.new("Maximum allowed output bytes exceeded. (#{maxbytes} bytes)")
36
+ end
37
+ end
38
+ end
39
+ else
13
40
  pro = IO.popen(command)
14
41
  until(pro.closed? || pro.eof?)
15
- if(RUBY_VERSION >= '1.9.0')
16
- output << pro.getc
17
- else
18
- output << pro.getc.chr
42
+ output << pro.getc.chr
43
+ if(maxbytes > 0 && output.size > maxbytes)
44
+ raise IOError.new("Maximum allowed output bytes exceeded. (#{maxbytes} bytes)")
19
45
  end
20
- raise IOError.new("Maximum allowed output bytes exceeded. (#{maxbytes} bytes)") unless output.size <= maxbytes
21
46
  end
22
47
  end
23
48
  output = output.join('')
24
- rescue Exception => boom
25
- raise boom
26
49
  ensure
27
- if(RUBY_PLATFORM == 'java')
28
- Process.kill('KILL', pro.pid) unless pro.nil?
29
- else
30
- Process.kill('KILL', pro.pid) if Process.waitpid2(pro.pid, Process::WNOHANG).nil? # make sure the process is dead
31
- end
50
+ Process.kill('KILL', pro.pid) if Process.waitpid2(pro.pid, Process::WNOHANG).nil? # make sure the process is dead
32
51
  end
33
52
  return output
34
53
  end
54
+ # Used for the thread_exec method to notify of completion
55
+ class Complete < StandardError
56
+ end
57
+
58
+ # command:: command to execute
59
+ # timeout:: maximum number of seconds to run
60
+ # maxbytes:: maximum number of result bytes to accept
61
+ # Execute a system command (use with care)
62
+ # This is the threaded exec command that is generally used
63
+ # with JRuby. The regular timeout does not work when executing
64
+ # a process, so we do it in a separate thread and sleep the main
65
+ # thread until the timeout is reached.
66
+ def self.thread_exec(command, timeout=10, maxbytes=500)
67
+ timeout = timeout.to_i
68
+ maxbytes = maxbytes.to_i
69
+ current = Thread.current
70
+ output = []
71
+ pro = nil
72
+ thread = Thread.new do
73
+ boom = Complete.new
74
+ begin
75
+ pro = IO.popen(command)
76
+ until(pro.closed? || pro.eof?)
77
+ output << pro.getc.chr
78
+ if(maxbytes > 0 && output.size > maxbytes)
79
+ raise IOError.new("Maximum allowed output bytes exceeded. (#{maxbytes} bytes)")
80
+ end
81
+ end
82
+ rescue Exception => boom
83
+ # just want it set
84
+ end
85
+ current.raise boom unless boom.is_a?(Timeout::Error)
86
+ end
87
+ begin
88
+ timeout > 0 ? sleep(timeout) : sleep
89
+ thread.raise Timeout::Error.new
90
+ raise Timeout::Error.new
91
+ rescue Complete
92
+ # do nothing
93
+ ensure
94
+ Process.kill('KILL', pro.pid) unless pro.nil?
95
+ end
96
+ output.join('')
97
+ end
35
98
  end
@@ -1,3 +1,4 @@
1
+ require 'thread'
1
2
  module Splib
2
3
  # Exception raised when queue is empty
3
4
  class EmptyQueue < Exception
@@ -2,7 +2,7 @@ spec = Gem::Specification.new do |s|
2
2
  s.name = 'splib'
3
3
  s.author = 'spox'
4
4
  s.email = 'spox@modspox.com'
5
- s.version = '1.1'
5
+ s.version = '1.2'
6
6
  s.summary = 'Spox Library'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.files = Dir['**/*']
@@ -0,0 +1,16 @@
1
+ require 'splib'
2
+ require 'test/unit'
3
+
4
+ class ArrayTest < Test::Unit::TestCase
5
+ def setup
6
+ Splib.load :Array
7
+ end
8
+
9
+ def test_flatten_original
10
+ if([].respond_to?(:fixed_flatten))
11
+ arr = [1,2,[3,[4,5],6],7]
12
+ assert_equal(arr, arr.fixed_flatten(0))
13
+ assert_equal(arr.flatten, arr.fixed_flatten)
14
+ end
15
+ end
16
+ end
@@ -7,47 +7,51 @@ class ConversionsTest < Test::Unit::TestCase
7
7
  end
8
8
 
9
9
  def test_format_seconds
10
- inc = {:year => 60 * 60 * 24 * 365,
11
- :month => 60 * 60 * 24 * 31,
12
- :week => 60 * 60 * 24 * 7,
13
- :day => 60 * 60 * 24,
14
- :hour => 60 * 60,
15
- :minute => 60,
16
- :second => 1}
10
+ inc = [{:year => 60 * 60 * 24 * 365},
11
+ {:month => 60 * 60 * 24 * 31},
12
+ {:week => 60 * 60 * 24 * 7},
13
+ {:day => 60 * 60 * 24},
14
+ {:hour => 60 * 60},
15
+ {:minute => 60},
16
+ {:second => 1}]
17
17
  100.times do |i|
18
18
  time = rand(i)
19
19
  otime = time
20
20
  formatted = []
21
- inc.each_pair do |name, value|
22
- val = (time / value).to_i
23
- if(val > 0)
24
- time = time - (val * value)
25
- formatted << "#{val} #{val == 1 ? name : "#{name}s"}"
21
+ inc.each do |v|
22
+ v.each_pair do |name, value|
23
+ val = (time / value).to_i
24
+ if(val > 0)
25
+ time = time - (val * value)
26
+ formatted << "#{val} #{val == 1 ? name : "#{name}s"}"
27
+ end
26
28
  end
27
29
  end
28
30
  formatted = formatted.empty? ? '0 seconds' : formatted.join(' ')
29
- assert_equal(Splib.format_seconds(otime), formatted)
31
+ assert_equal(formatted, Splib.format_seconds(otime))
30
32
  end
31
33
  end
32
34
 
33
35
  def test_format_size
34
- inc = {"byte" => 1024**0, # 1024^0
35
- "Kilobyte" => 1024**1, # 1024^1
36
- "Megabyte" => 1024**2, # 1024^2
37
- "Gigabyte" => 1024**3, # 1024^3
38
- "Terabyte" => 1024**4, # 1024^4
39
- "Petabyte" => 1024**5, # 1024^5
40
- "Exabyte" => 1024**6, # 1024^6
41
- "Zettabyte" => 1024**7, # 1024^7
42
- "Yottabyte" => 1024**8 # 1024^8
43
- }
36
+ inc = [{"byte" => 1024**0}, # 1024^0
37
+ {"Kilobyte" => 1024**1}, # 1024^1
38
+ {"Megabyte" => 1024**2}, # 1024^2
39
+ {"Gigabyte" => 1024**3}, # 1024^3
40
+ {"Terabyte" => 1024**4}, # 1024^4
41
+ {"Petabyte" => 1024**5}, # 1024^5
42
+ {"Exabyte" => 1024**6}, # 1024^6
43
+ {"Zettabyte" => 1024**7}, # 1024^7
44
+ {"Yottabyte" => 1024**8} # 1024^8
45
+ ]
44
46
  100.times do |i|
45
47
  val = i**rand(i)
46
48
  formatted = nil
47
- inc.each_pair do |name, value|
48
- v = val / value.to_f
49
- if(v.to_i > 0)
50
- formatted = ("%.2f " % v) + " #{name}#{v == 1 ? '' : 's'}".strip
49
+ inc.each do |v|
50
+ v.each_pair do |name, value|
51
+ v = val / value.to_f
52
+ if(v.to_i > 0)
53
+ formatted = ("%.2f " % v) + " #{name}#{v == 1 ? '' : 's'}".strip
54
+ end
51
55
  end
52
56
  end
53
57
  formatted = '0 bytes' if formatted.nil?
@@ -5,7 +5,7 @@ class ExecTest < Test::Unit::TestCase
5
5
  def setup
6
6
  Splib.load :Exec
7
7
  end
8
-
8
+
9
9
  def test_exec
10
10
  assert_raise(IOError) do
11
11
  Splib.exec('echo test', 10, 1)
@@ -1,3 +1,5 @@
1
+ $LOAD_PATH.unshift(File.expand_path("#{__FILE__}/../../lib"))
2
+
1
3
  require 'test/unit'
2
4
  require 'splib'
3
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: splib
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.1"
4
+ version: "1.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - spox
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-30 00:00:00 -08:00
12
+ date: 2010-01-10 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,7 @@ extra_rdoc_files:
23
23
  - README.rdoc
24
24
  - CHANGELOG
25
25
  files:
26
+ - tests/cases/Array.rb
26
27
  - tests/cases/PriorityQueue.rb
27
28
  - tests/cases/UrlShorteners.rb
28
29
  - tests/cases/Exec.rb
@@ -34,6 +35,7 @@ files:
34
35
  - tests/samplecode2.rb
35
36
  - tests/samplecode1.rb
36
37
  - lib/splib.rb
38
+ - lib/splib/Array.rb
37
39
  - lib/splib/PriorityQueue.rb
38
40
  - lib/splib/UrlShorteners.rb
39
41
  - lib/splib/Exec.rb