torigoya_kit 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82a26407253c5ae7ffc8a3174de840a837608bc4
4
+ data.tar.gz: 92531bc54c8dcad68b4a912f1800fa9f9fa84014
5
+ SHA512:
6
+ metadata.gz: e9ff9c289032353b23984a022bd2f6951ca97dbb386293c4d4b2022cc974d1d6054822bcf47d2d11e1bc765ac72acb434ca12c4328436acdb783c33f9aba87c5
7
+ data.tar.gz: 64fc6d595dde4a30f8c39c9fb4d1d6f5881a2bd636201e2c5e2df5f68d1e3dd4078b7ea2636069d056e75256aa5f50db59f5656e6dfb2fbbd5c9b2d5f534f7ca
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in torigoya_kit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 yutopp
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # TorigoyaKit
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'torigoya_kit'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install torigoya_kit
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/yutopp/torigoya_kit/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+
4
+ require_relative 'session'
5
+
6
+ module TorigoyaKit
7
+ class Client
8
+ def initialize(host, port)
9
+ @host = host
10
+ @port = port
11
+ end
12
+
13
+ ####
14
+ def exec_ticket(ticket)
15
+ return get_session().exec_ticket(ticket)
16
+ end
17
+
18
+ def exec_ticket_with_stream(ticket, &block)
19
+ return get_session().exec_ticket_with_stream(ticket, &block)
20
+ end
21
+
22
+ ####
23
+ def update_packages()
24
+ return get_session().update_packages()
25
+ end
26
+
27
+ ####
28
+ def reload_proc_table()
29
+ return get_session().reload_proc_table()
30
+ end
31
+
32
+ def update_proc_table()
33
+ return get_session().update_proc_table()
34
+ end
35
+
36
+ def get_proc_table()
37
+ return get_session().get_proc_table()
38
+ end
39
+
40
+ private
41
+ def get_session()
42
+ return Session.new(@host, @port)
43
+ end
44
+ end # class Client
45
+ end # module TorigoyaKit
@@ -0,0 +1,134 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+
4
+ require 'yaml'
5
+ require 'time'
6
+
7
+ module TorigoyaKit
8
+ module Package
9
+
10
+ #
11
+ class Util
12
+ def self.package_name_regex
13
+ /^torigoya-([a-zA-Z0-9+]+)(-|_)(([a-zA-Z0-9]+\.?)+)(-1)?_.*\.deb$/
14
+ end
15
+
16
+ # return
17
+ # [name, raw_version]
18
+ def self.parse_package_name( package_name )
19
+ reg = package_name.scan( self.package_name_regex )
20
+ if reg[0].nil?
21
+ raise "#{package_name} is invalid package name..."
22
+ end
23
+
24
+ return reg[0].values_at( 0, 2 )
25
+ end
26
+ end
27
+
28
+
29
+ #
30
+ class Tag
31
+ def initialize( package_name )
32
+ #
33
+ @package_name = package_name
34
+ @name, version = Util.parse_package_name( package_name )
35
+ @version = version.gsub( /^(999.)(.*)/, "head" ).gsub( /^(888.)(.*)/, "dev" ).gsub( /^(777.)(.*)/, "stable" )
36
+ @display_version = version.gsub( /^(999.)/, "HEAD-" ).gsub( /^(888.)/, "DEV-" ).gsub( /^(777.)/, "STABLE-" )
37
+ end
38
+ attr_reader :package_name, :name, :version, :display_version
39
+ end
40
+
41
+
42
+ #
43
+ class AvailableProfile
44
+ def initialize( package_name, built_date )
45
+ @package_name = package_name
46
+ @built_date = built_date
47
+ end
48
+ attr_reader :package_name, :built_date
49
+
50
+ def to_yaml
51
+ return YAML.dump( {
52
+ 'package_name' => @package_name,
53
+ 'built_date' => @built_date
54
+ } )
55
+ end
56
+
57
+ def self.from_yaml( yaml )
58
+ obj = YAML.load( yaml )
59
+ return self.new( obj['package_name'], obj['built_date'].instance_of?( String ) ? Time.parse( obj['built_date'] ) : obj['built_date'] )
60
+ end
61
+
62
+ def self.load_from_yaml( yaml_filename )
63
+ obj = YAML.load_file( yaml_filename )
64
+ return self.new( obj['package_name'], obj['built_date'].instance_of?( String ) ? Time.parse( obj['built_date'] ) : obj['built_date'] )
65
+ end
66
+ end
67
+
68
+
69
+ #
70
+ class ProfileHolder
71
+ def initialize( holder_path )
72
+ @holder_path = holder_path
73
+ end
74
+
75
+ def update_package( package_name, built_date )
76
+ profile_name = self.class.make_profile_name( package_name )
77
+ profile_path = "#{@holder_path}/#{profile_name}"
78
+ a_profile = AvailableProfile.new( package_name, built_date )
79
+
80
+ if File.exists?( profile_path )
81
+ # compare date
82
+ current_profile = AvailableProfile.load_from_yaml( profile_path )
83
+ if a_profile.built_date > current_profile.built_date
84
+ # update information
85
+ self.class.save_profile( profile_path, a_profile )
86
+ end
87
+ else
88
+ # save immediately
89
+ self.class.save_profile( profile_path, a_profile )
90
+ end
91
+
92
+ return profile_path
93
+ end
94
+
95
+ def delete_package( package_name )
96
+ profile_name = self.class.make_profile_name( package_name )
97
+ profile_path = "#{@holder_path}/#{profile_name}"
98
+
99
+ if File.exists?( profile_path )
100
+ system( "rm -f #{profile_path}" )
101
+ end
102
+
103
+ return profile_path
104
+ end
105
+
106
+ def list_profiles()
107
+ profs = []
108
+ Dir.chdir( @holder_path ) do
109
+ Dir.glob( '*.yml' ).sort_by {|f| File.mtime( f )}.reverse.each do |filename|
110
+ profs << AvailableProfile.load_from_yaml( filename )
111
+ end
112
+ end
113
+ return profs # sorted old to new
114
+ end
115
+
116
+ def list_tag_and_date()
117
+ profs = list_profiles()
118
+ return profs.map{|e| { tags: Tag.new( e.package_name ), built_date: e.built_date } }
119
+ end
120
+
121
+ def self.save_profile( path, available_profile )
122
+ File.open( path, "w" ) do |f|
123
+ f.write( available_profile.to_yaml )
124
+ end
125
+ end
126
+
127
+ def self.make_profile_name( package_name )
128
+ tag = Tag.new( package_name )
129
+ return "#{tag.name}-#{tag.version}.yml"
130
+ end
131
+ end
132
+
133
+ end # module Packages
134
+ end # module TorigoyaKit
@@ -0,0 +1,120 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+
4
+ require 'msgpack'
5
+ require_relative 'stream_result'
6
+
7
+ module TorigoyaKit
8
+ #
9
+ class Protocol
10
+ #
11
+ MessageKindIndexBegin = 0
12
+
13
+ # Sent from client
14
+ MessageKindAcceptRequest = 0
15
+ MessageKindTicketRequest = 1
16
+ MessageKindUpdateRepositoryRequest = 2
17
+ MessageKindReloadProcTableRequest = 3
18
+ MessageKindUpdateProcTableRequest = 4
19
+ MessageKindGetProcTableRequest = 5
20
+
21
+ # Sent from server
22
+ MessageKindAccept = 6
23
+ MessageKindOutputs = 7
24
+ MessageKindResult = 8
25
+ MessageKindSystemError = 9
26
+ MessageKindExit = 10
27
+
28
+ MessageKindSystemResult = 11
29
+ MessageKindProcTable = 12
30
+
31
+ #
32
+ MessageKindIndexEnd = 12
33
+ MessageKindInvalid = 0xff
34
+
35
+ #
36
+ HeaderLength = 5
37
+
38
+ #
39
+ class Packet
40
+ def initialize(kind, data)
41
+ if kind < MessageKindIndexBegin || kind > MessageKindIndexEnd
42
+ raise "invalid header"
43
+ end
44
+
45
+ @kind = [kind].pack("C*")
46
+ @encoded_data = data.to_msgpack
47
+ @size = [@encoded_data.size].pack("V*")
48
+ end
49
+ attr_reader :kind, :size, :encoded_data
50
+
51
+ #
52
+ def to_binary
53
+ return @kind + @size + @encoded_data
54
+ end
55
+ end
56
+
57
+ #
58
+ def self.encode(kind, data = nil)
59
+ return Packet.new(kind, data).to_binary
60
+ end
61
+
62
+ def self.encode_to(io, kind, data = nil)
63
+ io.write(encode(kind, data))
64
+ end
65
+
66
+ def self.get_responses(io)
67
+ results = []
68
+ decode_from_stream(io) do |r|
69
+ results << r
70
+ end
71
+
72
+ return results
73
+ end
74
+
75
+ #
76
+ def self.decode(buffer)
77
+ if buffer.size >= HeaderLength
78
+ # read kind
79
+ kind = buffer[0].unpack("c")[0] # 8bit char
80
+
81
+ # read length
82
+ length = buffer[1..4].unpack("I")[0] # 32bit unsigned int(little endian)
83
+
84
+ if buffer.size >= HeaderLength + length
85
+ return true, kind, length, buffer[HeaderLength...(HeaderLength + length)]
86
+ end
87
+ end
88
+
89
+ return false, nil, nil, nil
90
+ end
91
+
92
+ def self.cat_rest(buffer, length)
93
+ return buffer[(HeaderLength + length)..buffer.size]
94
+ end
95
+
96
+ #
97
+ def self.decode_as_client(kind, decoded)
98
+ case kind
99
+ when MessageKindAccept
100
+ return StreamAccept.new
101
+ when MessageKindOutputs
102
+ return StreamOutputResult.from_tuple(decoded)
103
+ when MessageKindResult
104
+ return StreamExecutedResult.from_tuple(decoded)
105
+ when MessageKindSystemError
106
+ return StreamSystemError.from_tuple(decoded)
107
+ when MessageKindExit
108
+ return StreamExit.from_tuple(decoded)
109
+
110
+ when MessageKindSystemResult
111
+ return StreamSystemStatusResult.new(decoded)
112
+ when MessageKindProcTable
113
+ return decoded
114
+ else
115
+ raise "Protocol :: Result kind(#{kind}) is not supported by clitnt side"
116
+ end
117
+ end
118
+
119
+ end # class Protocol
120
+ end # module TorigoyaKit
@@ -0,0 +1,24 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+
4
+ require_relative 'stream_result'
5
+
6
+ module TorigoyaKit
7
+ class TicketResultUnit
8
+ def initialize()
9
+ @out = ""
10
+ @err = ""
11
+ @result = nil
12
+ end
13
+ attr_accessor :out, :err, :result
14
+ end
15
+
16
+ class TicketResult
17
+ def initialize
18
+ @compile = nil
19
+ @link = nil
20
+ @run = {}
21
+ end
22
+ attr_accessor :compile, :link, :run
23
+ end
24
+ end # module TorigoyaKit
@@ -0,0 +1,214 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+
4
+ require 'socket'
5
+ require 'timeout'
6
+ require_relative 'ticket'
7
+ require_relative 'stream_result'
8
+ require_relative 'result'
9
+ require_relative 'protocol'
10
+
11
+ module TorigoyaKit
12
+ class Session
13
+ Version = "v2014/7/5"
14
+
15
+ ########################################
16
+ #
17
+ def initialize(host, port)
18
+ # 5 sec
19
+ timeout(5) do
20
+ @socket = Socket.tcp(host, port)
21
+ end
22
+
23
+ @buffer = ""
24
+ end
25
+
26
+ ########################################
27
+ #
28
+ def exec_ticket(ticket)
29
+ result = TicketResult.new
30
+ exec_ticket_with_stream(ticket) do |res|
31
+ if res.is_a?(StreamOutputResult) || res.is_a?(StreamExecutedResult)
32
+ case res.mode
33
+ when ResultMode::CompileMode
34
+ result.compile = TicketResultUnit.new if result.compile.nil?
35
+ when ResultMode::LinkMode
36
+ result.link = TicketResultUnit.new if result.link.nil?
37
+ when ResultMode::RunMode
38
+ result.run[res.index] = TicketResultUnit.new if result.run[res.index].nil?
39
+ end
40
+
41
+ if res.is_a?(StreamOutputResult)
42
+ case res.output.fd
43
+ when StreamOutput::StdoutFd
44
+ case res.mode
45
+ when ResultMode::CompileMode
46
+ result.compile.out << res.output.buffer
47
+ when ResultMode::LinkMode
48
+ result.link.out << res.output.buffer
49
+ when ResultMode::RunMode
50
+ result.run[res.index].out << res.output.buffer
51
+ end
52
+ when StreamOutput::StderrFd
53
+ case res.mode
54
+ when ResultMode::CompileMode
55
+ result.compile.err << res.output.buffer
56
+ when ResultMode::LinkMode
57
+ result.link.err << res.output.buffer
58
+ when ResultMode::RunMode
59
+ result.run[res.index].err << res.output.buffer
60
+ end
61
+ end
62
+
63
+ elsif res.is_a?(StreamExecutedResult)
64
+ case res.mode
65
+ when ResultMode::CompileMode
66
+ result.compile.result = res.result
67
+ when ResultMode::LinkMode
68
+ result.link.result = res.result
69
+ when ResultMode::RunMode
70
+ result.run[res.index].result = res.result
71
+ end
72
+ end
73
+
74
+ elsif res.is_a?(StreamSystemError)
75
+ raise res.message
76
+ else
77
+ raise "Unexpected error: unknown message was recieved (#{res.class})"
78
+ end
79
+ end
80
+ return result
81
+ end
82
+
83
+ ########################################
84
+ #
85
+ def exec_ticket_with_stream(ticket, &block)
86
+ write(Protocol::MessageKindTicketRequest, ticket)
87
+ read_stream(&block)
88
+ end
89
+
90
+ ########################################
91
+ #
92
+ def update_packages()
93
+ write(Protocol::MessageKindUpdateRepositoryRequest)
94
+ res = read()
95
+ if res.is_a?(StreamSystemStatusResult)
96
+ return
97
+ elsif res.is_a?(StreamSystemError)
98
+ raise res.message
99
+ else
100
+ raise "Unexpected error: unknown message was recieved (#{res.class})"
101
+ end
102
+ end
103
+
104
+ ########################################
105
+ #
106
+ def reload_proc_table()
107
+ write(Protocol::MessageKindReloadProcTableRequest)
108
+ res = read()
109
+ if res.is_a?(StreamSystemStatusResult)
110
+ return
111
+ elsif res.is_a?(StreamSystemError)
112
+ raise res.message
113
+ else
114
+ raise "Unexpected error: unknown message was recieved (#{res.class})"
115
+ end
116
+ end
117
+
118
+ ########################################
119
+ #
120
+ def update_proc_table()
121
+ write(Protocol::MessageKindUpdateProcTableRequest)
122
+ res = read()
123
+ if res.is_a?(StreamSystemStatusResult)
124
+ return
125
+ elsif res.is_a?(StreamSystemError)
126
+ raise res.message
127
+ else
128
+ raise "Unexpected error: unknown message was recieved (#{res.class})"
129
+ end
130
+ end
131
+
132
+ ########################################
133
+ #
134
+ def get_proc_table()
135
+ write(Protocol::MessageKindGetProcTableRequest)
136
+ res = read()
137
+ if res.is_a?(Hash)
138
+ return res
139
+ elsif res.is_a?(StreamSystemError)
140
+ raise res.message
141
+ else
142
+ raise "Unexpected error: unknown message was recieved (#{res.class})"
143
+ end
144
+ end
145
+
146
+ ########################################
147
+ #########################################
148
+ private
149
+
150
+ ########################################
151
+ #
152
+ def read()
153
+ accepted = false
154
+ is_closed = false
155
+
156
+ loop do
157
+ begin
158
+ @buffer << @socket.readpartial(1024)
159
+ rescue EOFError
160
+ is_closed = true
161
+ end
162
+
163
+ is_recieved, kind, length, content = Protocol.decode(@buffer)
164
+ unless is_recieved
165
+ if is_closed
166
+ break
167
+ else
168
+ next
169
+ end
170
+ end
171
+
172
+ # set rest
173
+ @buffer = Protocol.cat_rest(@buffer, length)
174
+
175
+ return Protocol.decode_as_client(kind, MessagePack.unpack(content))
176
+ end # loop
177
+
178
+ return nil
179
+ end
180
+
181
+ ########################################
182
+ #
183
+ def read_stream(&block)
184
+ loop do
185
+ o = read()
186
+ break if o.nil?
187
+
188
+ break if o.is_a?(StreamExit)
189
+ block.call(o) unless block.nil?
190
+ end
191
+ end
192
+
193
+ ########################################
194
+ #
195
+ def write(kind, *args)
196
+ expect_accepted_by_server()
197
+ Protocol.encode_to(@socket, kind, *args)
198
+ end
199
+
200
+ ########################################
201
+ #
202
+ def expect_accepted_by_server
203
+ Protocol.encode_to(@socket, Protocol::MessageKindAcceptRequest, Version)
204
+ res = read()
205
+ if res.is_a?(StreamAccept)
206
+ return
207
+ elsif res.is_a?(StreamSystemError)
208
+ raise res.message
209
+ else
210
+ raise "Unexpected error: unknown message was recieved"
211
+ end
212
+ end
213
+ end # class Session
214
+ end # module TorigoyaKit
@@ -0,0 +1,161 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+
4
+ module TorigoyaKit
5
+ class ResultMode
6
+ CompileMode = 0
7
+ LinkMode = 1
8
+ RunMode = 2
9
+ end
10
+
11
+ #
12
+ class StreamAccept
13
+ end
14
+
15
+ #
16
+ class StreamOutput
17
+ StdoutFd = 0
18
+ StderrFd = 1
19
+
20
+ def initialize(fd, buffer)
21
+ @fd = fd
22
+ @buffer = buffer
23
+ end
24
+ attr_reader :fd, :buffer
25
+
26
+ def self.from_tuple(tupled)
27
+ raise "type error [expected Array but #{message.class}] :: #{self}" unless tupled.is_a?(Array)
28
+ raise "invalid format(tuple size is different [expected 2 but #{tupled.size}] :: #{self})" unless tupled.size == 2
29
+
30
+ return StreamOutput.new(tupled[0], tupled[1])
31
+ end
32
+
33
+ def to_s
34
+ return "#{self.class}/[#{@fd} #{@buffer}]"
35
+ end
36
+ end
37
+
38
+ # related to MessageKindOutputs
39
+ class StreamOutputResult
40
+ def initialize(mode, index, output)
41
+ @mode = mode
42
+ @index = index
43
+ @output = output
44
+ end
45
+ attr_reader :mode, :index, :output
46
+
47
+ def self.from_tuple(tupled)
48
+ raise "type error [expected Array but #{message.class}] :: #{self}" unless tupled.is_a?(Array)
49
+ raise "invalid format(tuple size is different [expected 3 but #{tupled.size}] :: #{self})" unless tupled.size == 3
50
+
51
+ return StreamOutputResult.new(tupled[0],
52
+ tupled[1],
53
+ StreamOutput.from_tuple(tupled[2])
54
+ )
55
+ end
56
+
57
+ def to_s
58
+ return "#{self.class}/[#{@mode} #{@index} #{@output.to_s}]"
59
+ end
60
+ end
61
+
62
+ #
63
+ class ExecutedResult
64
+ def initialize(cpu_time, memory, signal, return_code, command_line, status, error_message)
65
+ @used_cpu_time_sec = cpu_time
66
+ @used_memory_bytes = memory
67
+ @signal = signal
68
+ @return_code = return_code
69
+ @command_line = command_line
70
+ @status = status
71
+ @system_error_message = error_message
72
+ end
73
+ attr_reader :used_cpu_time_sec, :used_memory_bytes, :signal, :return_code, :command_line, :status, :system_error_message
74
+
75
+ def self.from_tuple(tupled)
76
+ raise "type error [expected Array but #{message.class}] :: #{self}" unless tupled.is_a?(Array)
77
+ raise "invalid format(tuple size is different [expected 7 but #{tupled.size}] :: #{self})" unless tupled.size == 7
78
+
79
+ return ExecutedResult.new(tupled[0],
80
+ tupled[1],
81
+ tupled[2],
82
+ tupled[3],
83
+ tupled[4],
84
+ tupled[5],
85
+ tupled[6]
86
+ )
87
+ end
88
+
89
+ def to_s
90
+ return "#{self.class}/[#{@used_cpu_time_sec} #{@used_memory_bytes} #{@signal} #{@return_code} #{@command_line} #{@status} #{@system_error_message}]"
91
+ end
92
+ end
93
+
94
+ # related to StreamExecutedResult
95
+ class StreamExecutedResult
96
+ def initialize(mode, index, result)
97
+ @mode = mode
98
+ @index = index
99
+ @result = result
100
+ end
101
+ attr_reader :mode, :index, :result
102
+
103
+ def self.from_tuple(tupled)
104
+ raise "type error [expected Array but #{message.class}] :: #{self}" unless tupled.is_a?(Array)
105
+ raise "invalid format(tuple size is different [expected 3 but #{tupled.size}] :: #{self})" unless tupled.size == 3
106
+
107
+ return StreamExecutedResult.new(tupled[0],
108
+ tupled[1],
109
+ ExecutedResult.from_tuple(tupled[2])
110
+ )
111
+ end
112
+
113
+ def to_s
114
+ return "#{self.class}/[#{@mode} #{@index} #{@result.to_s}]"
115
+ end
116
+ end
117
+
118
+ # related to MessageKindSystemError
119
+ class StreamSystemError
120
+ def initialize(message)
121
+ @message = message
122
+ end
123
+ attr_reader :message
124
+
125
+ def self.from_tuple(message)
126
+ raise "type error [expected String but #{message.class}] :: #{self}" unless message.is_a?(String)
127
+
128
+ return StreamSystemError.new(message)
129
+ end
130
+
131
+ def to_s
132
+ return "#{self.class}/[#{@message}]"
133
+ end
134
+ end
135
+
136
+ # related to MessageKindExit
137
+ class StreamExit
138
+ def initialize(message)
139
+ @message = message
140
+ end
141
+ attr_reader :message
142
+
143
+ def self.from_tuple(message)
144
+ raise "type error [expected String but #{message.class}] :: #{self}" unless message.is_a?(String)
145
+
146
+ return StreamExit.new(message)
147
+ end
148
+
149
+ def to_s
150
+ return "#{self.class}/[#{@message}]"
151
+ end
152
+ end
153
+
154
+ #
155
+ class StreamSystemStatusResult
156
+ def initialize(status)
157
+ @status = status
158
+ end
159
+ attr_reader :status
160
+ end
161
+ end # module TorigoyaKit
@@ -0,0 +1,132 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+
4
+ require 'msgpack'
5
+
6
+ module TorigoyaKit
7
+ # contains source codes / inputs data
8
+ class SourceData
9
+ def self.make_with_compress(name, code)
10
+ return self.new(name, code, true)
11
+ end
12
+
13
+ def initialize(name, code, is_compressed = false)
14
+ @name = name
15
+ @code = if is_compressed then
16
+ else
17
+ code
18
+ end
19
+ @is_compressed = is_compressed
20
+ end
21
+ attr_reader :name, :code
22
+
23
+ def to_tuple
24
+ return [@name,
25
+ @code,
26
+ @is_compressed
27
+ ]
28
+ end
29
+
30
+ def to_msgpack(out = '')
31
+ return to_tuple.to_msgpack(out)
32
+ end
33
+ end
34
+
35
+ #
36
+ class ExecutionSetting
37
+ def initialize(command_line, structured_command, cpu_limit, memory_limit)
38
+ @command_line = command_line # String
39
+ @structured_command = structured_command # Array!(Array!String)
40
+ @cpu_limit = cpu_limit # uint64 / sec
41
+ @memory_limit = memory_limit # uint64 / bytes
42
+ end
43
+
44
+ def to_tuple
45
+ return [@command_line,
46
+ @structured_command,
47
+ @cpu_limit,
48
+ @memory_limit
49
+ ]
50
+ end
51
+
52
+ def to_msgpack(out = '')
53
+ return to_tuple.to_msgpack(out)
54
+ end
55
+ end
56
+
57
+ #
58
+ class BuildInstruction
59
+ def initialize(compile_setting, link_setting)
60
+ @compile_setting = compile_setting # ExecutionSetting
61
+ @link_setting = link_setting # ExecutionSetting
62
+ end
63
+
64
+ def to_tuple
65
+ return [@compile_setting.to_tuple,
66
+ @link_setting.to_tuple
67
+ ]
68
+ end
69
+
70
+ def to_msgpack(out = '')
71
+ return to_tuple.to_msgpack(out)
72
+ end
73
+ end
74
+
75
+ #
76
+ class Input
77
+ def initialize(stdin, run_setting)
78
+ @stdin = stdin # SourceData
79
+ @run_setting = run_setting # ExecutionSetting
80
+ end
81
+
82
+ def to_tuple
83
+ return [@stdin, @run_setting]
84
+ end
85
+
86
+ def to_msgpack(out = '')
87
+ return to_tuple.to_msgpack(out)
88
+ end
89
+ end
90
+
91
+ #
92
+ class RunInstruction
93
+ def initialize(inputs)
94
+ @inputs = inputs # Array!Input
95
+ end
96
+
97
+ def to_tuple
98
+ return [@inputs.map {|x| x.to_tuple}]
99
+ end
100
+
101
+ def to_msgpack(out = '')
102
+ return to_tuple.to_msgpack(out)
103
+ end
104
+ end
105
+
106
+ #
107
+ class Ticket
108
+ def initialize(base_name, proc_id, proc_version, source_codes, build_inst, run_inst)
109
+ @base_name = base_name
110
+ @proc_id = proc_id
111
+ @proc_version = proc_version
112
+ @source_codes = source_codes # Array!SourceData
113
+ @build_inst = build_inst # BuildInstruction
114
+ @run_inst = run_inst # RunInstruction
115
+ end
116
+
117
+ def to_tuple
118
+ return [@base_name,
119
+ @proc_id,
120
+ @proc_version,
121
+ @source_codes.map {|x| x.to_tuple},
122
+ @build_inst.to_tuple,
123
+ @run_inst.to_tuple
124
+ ]
125
+ end
126
+
127
+ def to_msgpack
128
+ return to_tuple.to_msgpack()
129
+ end
130
+ end
131
+
132
+ end # module TorigoyaKit
@@ -0,0 +1,3 @@
1
+ module TorigoyaKit
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "torigoya_kit/version"
2
+
3
+ require_relative 'torigoya_kit/protocol'
4
+ require_relative 'torigoya_kit/ticket'
5
+ require_relative 'torigoya_kit/stream_result'
6
+ require_relative 'torigoya_kit/result'
7
+ require_relative 'torigoya_kit/session'
8
+ require_relative 'torigoya_kit/client'
9
+
10
+ require_relative 'torigoya_kit/package_utils.rb'
@@ -0,0 +1,161 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+ require 'tmpdir'
4
+ require_relative '../spec_helper'
5
+
6
+ describe :package_utils do
7
+ it "test_util" do
8
+ expect(TorigoyaKit::Package::Util.parse_package_name("torigoya-llvm-3.4_3.4_amd64.deb")).to eq ['llvm', '3.4']
9
+
10
+ expect(TorigoyaKit::Package::Util.parse_package_name("torigoya-llvm_999.2014.4.4.205650_amd64.deb")).to eq ['llvm', '999.2014.4.4.205650']
11
+
12
+ expect do
13
+ TorigoyaKit::Package::Util.parse_package_name("torigababa64.deb")
14
+ end.to raise_error RuntimeError
15
+ end
16
+
17
+ it "test_tag_1" do
18
+ tag = TorigoyaKit::Package::Tag.new("torigoya-llvm-3.4_3.4_amd64.deb")
19
+ expect(tag.package_name).to eq "torigoya-llvm-3.4_3.4_amd64.deb"
20
+ expect(tag.name).to eq "llvm"
21
+ expect(tag.version).to eq "3.4"
22
+ expect(tag.display_version).to eq "3.4"
23
+ end
24
+
25
+ it "test_tag_j1" do
26
+ tag = TorigoyaKit::Package::Tag.new("torigoya-java9_999.2014.4.8.e912167e7ecf_amd64.deb")
27
+ expect(tag.package_name).to eq "torigoya-java9_999.2014.4.8.e912167e7ecf_amd64.deb"
28
+ expect(tag.name).to eq "java9"
29
+ expect(tag.version).to eq "head"
30
+ expect(tag.display_version).to eq "HEAD-2014.4.8.e912167e7ecf"
31
+ end
32
+
33
+ it "test_tag_21" do
34
+ tag = TorigoyaKit::Package::Tag.new("torigoya-llvm_999.2014.4.4.205650_amd64.deb")
35
+ expect(tag.package_name).to eq "torigoya-llvm_999.2014.4.4.205650_amd64.deb"
36
+ expect(tag.name).to eq "llvm"
37
+ expect(tag.version).to eq "head"
38
+ expect(tag.display_version).to eq "HEAD-2014.4.4.205650"
39
+ end
40
+
41
+ it "test_tag_22" do
42
+ tag = TorigoyaKit::Package::Tag.new( "torigoya-llvm_888.2014.4.4.205650_amd64.deb" )
43
+ expect(tag.package_name).to eq "torigoya-llvm_888.2014.4.4.205650_amd64.deb"
44
+ expect(tag.name).to eq "llvm"
45
+ expect(tag.version).to eq "dev"
46
+ expect(tag.display_version).to eq "DEV-2014.4.4.205650"
47
+ end
48
+
49
+ it "test_tag_23" do
50
+ tag = TorigoyaKit::Package::Tag.new( "torigoya-llvm_777.2014.4.4.205650_amd64.deb" )
51
+ expect(tag.package_name).to eq "torigoya-llvm_777.2014.4.4.205650_amd64.deb"
52
+ expect(tag.name).to eq "llvm"
53
+ expect(tag.version).to eq "stable"
54
+ expect(tag.display_version).to eq "STABLE-2014.4.4.205650"
55
+ end
56
+
57
+ it "test_tag_2" do
58
+ expect do
59
+ tag = TorigoyaKit::Package::Tag.new( "torigababa64.deb" )
60
+ end.to raise_error(RuntimeError)
61
+ end
62
+
63
+ it "test_prof_update_exist" do
64
+ Dir.mktmpdir do |dir|
65
+ h = TorigoyaKit::Package::ProfileHolder.new( dir )
66
+
67
+ p_name = "torigoya-llvm_999.2014.4.4.205650_amd64.deb"
68
+ new_p_time = Time.now()
69
+
70
+ begin
71
+ f_path = h.update_package( p_name, new_p_time )
72
+
73
+ expect(f_path).to eq "#{dir}/llvm-head.yml"
74
+ expect(File.exists?(f_path)).to eq true
75
+ end
76
+ end # Dir
77
+ end
78
+
79
+ it "test_prof_update" do
80
+ Dir.mktmpdir do |dir|
81
+ h = TorigoyaKit::Package::ProfileHolder.new( dir )
82
+
83
+ p_name = "torigoya-llvm_999.2014.4.4.205650_amd64.deb"
84
+ new_p_time = Time.now()
85
+ updated_p_time = new_p_time + 100
86
+
87
+ begin
88
+ f_path = h.update_package( p_name, new_p_time )
89
+
90
+ expect(File.exists?(f_path)).to eq true
91
+ f_y = TorigoyaKit::Package::AvailableProfile.load_from_yaml(f_path)
92
+ expect(f_y.package_name).to eq p_name
93
+ expect(f_y.built_date).to eq new_p_time
94
+ end
95
+
96
+ begin
97
+ # Latest File
98
+ f_path = h.update_package(p_name, updated_p_time)
99
+
100
+ expect(File.exists?( f_path )).to eq true
101
+ f_y = TorigoyaKit::Package::AvailableProfile.load_from_yaml(f_path)
102
+ expect(f_y.package_name).to eq p_name
103
+ expect(f_y.built_date).to eq updated_p_time
104
+ end
105
+
106
+ begin
107
+ f_path = h.update_package(p_name, new_p_time)
108
+
109
+ expect(File.exists?(f_path)).to eq true
110
+ f_y = TorigoyaKit::Package::AvailableProfile.load_from_yaml(f_path)
111
+ expect(f_y.package_name).to eq p_name
112
+ expect(f_y.built_date).to eq updated_p_time
113
+ end
114
+ end # Dir
115
+ end
116
+
117
+ it "test_prof_delete" do
118
+ Dir.mktmpdir do |dir|
119
+ h = TorigoyaKit::Package::ProfileHolder.new(dir)
120
+
121
+ p_name = "torigoya-llvm_999.2014.4.4.205650_amd64.deb"
122
+ new_p_time = Time.now()
123
+
124
+ begin
125
+ f_path = h.update_package(p_name, new_p_time)
126
+ expect(File.exists?(f_path)).to eq true
127
+ end
128
+
129
+ begin
130
+ f_path = h.delete_package(p_name)
131
+ expect(File.exists?(f_path)).to eq false
132
+ end
133
+ end # Dir
134
+ end
135
+
136
+ it "test_prof_list" do
137
+ Dir.mktmpdir do |dir|
138
+ h = TorigoyaKit::Package::ProfileHolder.new(dir)
139
+
140
+ build_date = Time.now
141
+ pkgs = [{ name: "torigoya-llvm-3.4_3.4_amd64.deb", date: build_date },
142
+ { name: "torigoya-llvm_999.2014.4.4.205650_amd64.deb", date: build_date }, ]
143
+
144
+ begin
145
+ pkgs.each do |e|
146
+ h.update_package(e[:name], e[:date])
147
+ end
148
+ end
149
+
150
+ begin
151
+ profs = h.list_profiles
152
+
153
+ expect(profs.length).to eq 2
154
+ expect(profs[0].package_name).to eq pkgs[0][:name]
155
+ expect(profs[0].built_date).to eq pkgs[0][:date]
156
+ expect(profs[1].package_name).to eq pkgs[1][:name]
157
+ expect(profs[1].built_date).to eq pkgs[1][:date]
158
+ end
159
+ end # Dir
160
+ end
161
+ end
@@ -0,0 +1,31 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+ require_relative '../spec_helper'
4
+
5
+ describe :request_protocol do
6
+ b = TorigoyaKit::Protocol::Packet.new(TorigoyaKit::Protocol::MessageKindAcceptRequest, 12345)
7
+
8
+ it "size of kind should be 1" do
9
+ expect(b.kind.size).to eq 1
10
+ end
11
+
12
+ it "kind should be TorigoyaKit::Protocol::MessageKindAcceptRequest" do
13
+ expect(b.kind.bytes).to eq [0x00]
14
+ end
15
+
16
+ it "size of size should be 3" do
17
+ expect(b.size.size).to eq 4
18
+ end
19
+
20
+ it "size should be [0x03, 0x00, 0x00, 0x00]" do
21
+ expect(b.size.bytes).to eq [0x03, 0x00, 0x00, 0x00]
22
+ end
23
+
24
+ it "size of encoded_data should be 3" do
25
+ expect(b.encoded_data.size).to eq 3
26
+ end
27
+
28
+ it "encoded_data should be [205, 48, 57]" do
29
+ expect(b.encoded_data.bytes).to eq [205, 48, 57]
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+ require_relative '../spec_helper'
4
+
5
+ describe :session do
6
+ it "session proc exec_ticket" do
7
+ s = TorigoyaKit::Session.new("localhost", 49800)
8
+ p s.exec_ticket(make_ticket())
9
+ end
10
+
11
+ it "session proc update_proc_table" do
12
+ #s = TorigoyaKit::Session.new("localhost", 22222)
13
+ #s.update_packages()
14
+ #s.exec_ticket(make_ticket())
15
+ #s.update_proc_table()
16
+ #p s.get_proc_table()
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+ require_relative '../spec_helper'
4
+
5
+ describe :ticket do
6
+ ticket = make_ticket()
7
+
8
+ it "aaa" do
9
+ expect(ticket).not_to eq nil
10
+ end
11
+
12
+ it "aaa" do
13
+ expect(ticket.to_msgpack).not_to eq nil
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ # Copyright (c) 2014 yutopp
2
+ # Licenced under the MIT License (http://www.opensource.org/licenses/mit-license.php)
3
+ require 'torigoya_kit'
4
+
5
+ def make_ticket
6
+ # source
7
+ source = TorigoyaKit::SourceData.new("prog.cpp", <<EOS
8
+ #include <csignal>
9
+ #include <iostream>
10
+
11
+ int main() {
12
+ std::cout << "hello!" << std::endl;
13
+ std::raise(8);
14
+ std::cout << "unreachable!" << std::endl;
15
+ }
16
+ EOS
17
+ )
18
+
19
+ # set of source
20
+ sources = [source]
21
+
22
+ # build instruction
23
+ bi = TorigoyaKit::BuildInstruction.new(TorigoyaKit::ExecutionSetting.new("", [], 10, 512 * 1024 * 1024),
24
+ TorigoyaKit::ExecutionSetting.new("", [], 10, 512 * 1024 * 1024)
25
+ )
26
+ # input
27
+ input = TorigoyaKit::Input.new(nil,
28
+ TorigoyaKit::ExecutionSetting.new("", [], 10, 512 * 1024 * 1024)
29
+ )
30
+
31
+ # inputs
32
+ inputs = [input]
33
+
34
+ # run instruction
35
+ ri = TorigoyaKit::RunInstruction.new(inputs)
36
+
37
+ # ticket!
38
+ ticket = TorigoyaKit::Ticket.new("aaa", 0, "test", sources, bi, ri)
39
+
40
+ return ticket
41
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'torigoya_kit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "torigoya_kit"
8
+ spec.version = TorigoyaKit::VERSION
9
+ spec.authors = ["yutopp"]
10
+ spec.email = ["yutopp@gmail.com"]
11
+ spec.summary = %q{Toolkits for Torigoya}
12
+ spec.description = %q{This library can control TorigoyaCage}
13
+ spec.homepage = "http://yutopp.net/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "msgpack"
24
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: torigoya_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - yutopp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: msgpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This library can control TorigoyaCage
56
+ email:
57
+ - yutopp@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/torigoya_kit.rb
68
+ - lib/torigoya_kit/client.rb
69
+ - lib/torigoya_kit/package_utils.rb
70
+ - lib/torigoya_kit/protocol.rb
71
+ - lib/torigoya_kit/result.rb
72
+ - lib/torigoya_kit/session.rb
73
+ - lib/torigoya_kit/stream_result.rb
74
+ - lib/torigoya_kit/ticket.rb
75
+ - lib/torigoya_kit/version.rb
76
+ - spec/cases/package_utils_spec.rb
77
+ - spec/cases/protocol_spec.rb
78
+ - spec/cases/session_spec.rb
79
+ - spec/cases/ticket_spec.rb
80
+ - spec/spec_helper.rb
81
+ - torigoya_kit.gemspec
82
+ homepage: http://yutopp.net/
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Toolkits for Torigoya
106
+ test_files:
107
+ - spec/cases/package_utils_spec.rb
108
+ - spec/cases/protocol_spec.rb
109
+ - spec/cases/session_spec.rb
110
+ - spec/cases/ticket_spec.rb
111
+ - spec/spec_helper.rb