exedb 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/exedb.rb +40 -6
- data/lib/exedb/version.rb +1 -1
- data/spec/exedb_spec.rb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a2b5ff6b9726798cbba0e04538f9b8e1b9bc3e2
|
4
|
+
data.tar.gz: 96b44c2dba5858cdfa11288b0fd61813235103a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bacc3f2574045104054648091f6511853844eb48f4ddbd9eac6c310625d0d17803ff243ef640badfbe6bf6db2fb6c5d72d88cbb3d1c374fb3dfbba2f0fdc316
|
7
|
+
data.tar.gz: 7cc175baacd4b7b108eb2d00da578f5cc1ccf4106b590508513fdbf0c37069c27ed368f641d1b80648e404052809b3f13d8482a23a14e1ab9ccd3efc3f24c72c
|
data/lib/exedb.rb
CHANGED
@@ -16,13 +16,36 @@ require 'time'
|
|
16
16
|
#
|
17
17
|
# Exit code is available via 'code' method
|
18
18
|
#
|
19
|
+
# Example:
|
20
|
+
# d=Exedb.new("ls -la .")
|
21
|
+
# d.cache_timeout=5 # 5 seconds for cache expire
|
22
|
+
# list=d.get # execute 'ls' and get list of files
|
23
|
+
# list=d.get # just get list again (cached)
|
24
|
+
# sleep 5
|
25
|
+
# list=d.get # execute 'ls' again and get result
|
26
|
+
# list=d.update # force 'ls' execution!
|
27
|
+
# d.line_transform {|l|
|
28
|
+
# if l =~ /^d.*(\S+)$/
|
29
|
+
# return "DIR: $1" # not correct, do not use in production :)
|
30
|
+
# elsif l =~ /^-.*(\S+)$/
|
31
|
+
# return "FILE: $1" # just transform example
|
32
|
+
# else
|
33
|
+
# return nil # skip this line
|
34
|
+
# end
|
35
|
+
# }
|
36
|
+
# d.update # get list of directories and files in special format
|
37
|
+
#
|
19
38
|
class Exedb
|
20
39
|
|
21
40
|
SLEEP_TIME=1
|
22
41
|
DEF_DIR='/tmp/Exedb'
|
23
42
|
DEF_CACHE_TIMEOUT=60
|
24
43
|
|
25
|
-
|
44
|
+
# directory for cache files
|
45
|
+
attr_accessor :cache_dir
|
46
|
+
# default cache timeout
|
47
|
+
attr_accessor :cache_timeout
|
48
|
+
# last cache update time
|
26
49
|
attr_reader :update_time
|
27
50
|
|
28
51
|
# Constructor
|
@@ -92,6 +115,10 @@ class Exedb
|
|
92
115
|
# transform each line in command output
|
93
116
|
# if nil is returned, line is skipped
|
94
117
|
#
|
118
|
+
# Example:
|
119
|
+
# @d.line_transform {|str|
|
120
|
+
# str.downcase
|
121
|
+
# }
|
95
122
|
def line_transform(&block)
|
96
123
|
if block
|
97
124
|
obj = Object.new
|
@@ -114,6 +141,10 @@ class Exedb
|
|
114
141
|
# block is called with parameters: content, return code
|
115
142
|
# returned content replaces original output
|
116
143
|
#
|
144
|
+
# Example:
|
145
|
+
# @d.all_transform {|str,code|
|
146
|
+
# "Total: #{str.lines.count} lines\nExit code: #{code}"
|
147
|
+
# }
|
117
148
|
def all_transform(&block)
|
118
149
|
if block
|
119
150
|
obj = Object.new
|
@@ -124,6 +155,9 @@ class Exedb
|
|
124
155
|
end
|
125
156
|
end
|
126
157
|
|
158
|
+
#
|
159
|
+
# cancel transformation of full output
|
160
|
+
#
|
127
161
|
def no_all_transform
|
128
162
|
@alltransform=nil
|
129
163
|
end
|
@@ -175,9 +209,9 @@ class Exedb
|
|
175
209
|
|
176
210
|
#
|
177
211
|
# Returns symbol of cache state:
|
178
|
-
# - updated = actual
|
179
|
-
# - need_update = new command execution needed
|
180
|
-
# - need_reread = just cache file reread is neede
|
212
|
+
# - :updated = actual
|
213
|
+
# - :need_update = new command execution needed
|
214
|
+
# - :need_reread = just cache file reread is neede
|
181
215
|
#
|
182
216
|
def cache_state
|
183
217
|
if File.exists? @path
|
@@ -217,14 +251,14 @@ protected
|
|
217
251
|
f=u.tr('^qwertyuiopasdfghjklzxcvbnm_-','')
|
218
252
|
d=Digest::SHA256.hexdigest(u)
|
219
253
|
return f[0,60]+'..'+f[-60,60]+d if f.size>128
|
220
|
-
|
254
|
+
return f+d
|
221
255
|
end
|
222
256
|
|
223
257
|
def read_cache
|
224
258
|
File.open(@path, File::RDONLY) { |file|
|
225
259
|
file.flock(File::LOCK_EX)
|
226
260
|
@content=file.read
|
227
|
-
warn "CACHE READ: #{@content}"
|
261
|
+
# warn "CACHE READ: #{@content}"
|
228
262
|
begin
|
229
263
|
File.open("#{@path}.code", File::RDONLY) { |code_file|
|
230
264
|
c=code_file.gets
|
data/lib/exedb/version.rb
CHANGED
data/spec/exedb_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require 'minitest/autorun'
|
|
2
2
|
require "exedb"
|
3
3
|
|
4
4
|
TEST_DIR='/tmp/exedb_test'
|
5
|
+
TEST_CACHE_DIR='/tmp/exedb_test_cache'
|
5
6
|
TEST_FILE='abc_file'
|
6
7
|
TEST_FILE2='efg_file'
|
7
8
|
|
@@ -11,6 +12,7 @@ describe Exedb do
|
|
11
12
|
FileUtils.rm_rf Exedb::DEF_DIR
|
12
13
|
|
13
14
|
@db=Exedb.new
|
15
|
+
@db.cache_dir=TEST_CACHE_DIR
|
14
16
|
FileUtils.rm_rf TEST_DIR
|
15
17
|
Dir.mkdir TEST_DIR
|
16
18
|
File.open(File.join(TEST_DIR,TEST_FILE), "w") { |io| io.puts "ok" }
|
@@ -46,6 +48,7 @@ describe Exedb do
|
|
46
48
|
describe 'parallel istances' do
|
47
49
|
before do
|
48
50
|
@db2=Exedb.new
|
51
|
+
@db2.cache_dir=TEST_CACHE_DIR
|
49
52
|
@db2.update_method="sleep 1;ls -la #{TEST_DIR}"
|
50
53
|
@db.update
|
51
54
|
end
|
@@ -84,6 +87,8 @@ describe Exedb do
|
|
84
87
|
it 'must read cached value' do
|
85
88
|
@db=Exedb.new('x=`cat /tmp/mytestcode`; echo ">>$x<<"; exit $x')
|
86
89
|
@db2=Exedb.new('x=`cat /tmp/mytestcode`; echo ">>$x<<"; exit $x')
|
90
|
+
@db.cache_dir=TEST_CACHE_DIR
|
91
|
+
@db2.cache_dir=TEST_CACHE_DIR
|
87
92
|
File.open("/tmp/mytestcode",'w'){|f| f.puts '1'}
|
88
93
|
@db.code
|
89
94
|
File.open("/tmp/mytestcode",'w'){|f| f.puts '2'}
|
@@ -98,6 +103,8 @@ describe Exedb do
|
|
98
103
|
CMD4='for i in 1 2 3 4; do echo x; sleep 1; done'
|
99
104
|
@db=Exedb.new(CMD4)
|
100
105
|
@db2=Exedb.new(CMD4)
|
106
|
+
@db.cache_dir=TEST_CACHE_DIR
|
107
|
+
@db2.cache_dir=TEST_CACHE_DIR
|
101
108
|
t=Thread.new {
|
102
109
|
@db.get
|
103
110
|
}
|