tsp_runner 0.1.0 → 0.1.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 +4 -4
- data/.gitignore +1 -0
- data/exe/tsp_runner +2 -11
- data/lib/tsp_runner/runner.rb +45 -12
- data/lib/tsp_runner/version.rb +1 -1
- 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: b3b74d923c3314308b08c1e20305017d452b7730
|
4
|
+
data.tar.gz: 060ae82a558dca144846542b7b483f0234751f93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 854ef5741aba85fe543bacb7bbb4dda18c0749bd68f1b0154b0d02054df65d49fa885d1872aa0ef219cf296f9bea3d751ab351200e598fb615e7b147640dd4a5
|
7
|
+
data.tar.gz: 4aebefcea41fc014b37075278a20f32dbf34e7876546855dcb68c3ce2a4d84841ee1b5fcf2e52450bae488a5ebaab678f8f6d74a70247b124b67c41d0c749f0f
|
data/.gitignore
CHANGED
data/exe/tsp_runner
CHANGED
@@ -2,14 +2,5 @@
|
|
2
2
|
require 'bundler/setup'
|
3
3
|
require 'tsp_runner'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
begin
|
8
|
-
runner = TspRunner::Runner.new(ARGV.join(' '), filename)
|
9
|
-
runner.run(5)
|
10
|
-
distance = runner.validate!('San Francisco')
|
11
|
-
$stdout.puts("Success: #{distance} km")
|
12
|
-
rescue TspRunner::Runner::BaseError => error
|
13
|
-
$stderr.puts error.message
|
14
|
-
exit(1)
|
15
|
-
end
|
5
|
+
runner = TspRunner::Runner.new(ARGV[0], ARGV[1], *ARGV[2..-1])
|
6
|
+
runner.run
|
data/lib/tsp_runner/runner.rb
CHANGED
@@ -2,35 +2,68 @@ require 'timeout'
|
|
2
2
|
|
3
3
|
module TspRunner
|
4
4
|
class Runner
|
5
|
-
class
|
6
|
-
class TimeoutError < BaseError; end
|
7
|
-
class InvalidError < BaseError; end
|
5
|
+
class InvalidError < StandardError; end
|
8
6
|
|
9
|
-
attr_reader :cmd, :
|
7
|
+
attr_reader :cmd, :filename, :time_limit
|
10
8
|
|
11
|
-
def
|
12
|
-
|
9
|
+
def self.usage
|
10
|
+
puts "Usage: #{$0} <time limit> <input file> <command> [command arg 1]..."
|
11
|
+
puts ''
|
12
|
+
puts ' time limit - maximum duration the executable can run in seconds'
|
13
|
+
puts ' input file - input file with location names and lat/lons'
|
14
|
+
puts ' command - command line executable that calculates the path'
|
15
|
+
puts ' command arg n - optional args for the command line executable'
|
13
16
|
end
|
14
17
|
|
15
|
-
def
|
16
|
-
@
|
18
|
+
def initialize(time_limit, filename, *cmd_and_opts)
|
19
|
+
@time_limit = time_limit
|
20
|
+
@filename = filename
|
21
|
+
@cmd = cmd_and_opts.join(' ')
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate_input
|
25
|
+
return false if time_limit.nil? || filename.nil? || cmd.to_s.length == 0
|
26
|
+
Integer(time_limit.to_s, 10)
|
27
|
+
unless File.exists?(filename.to_s)
|
28
|
+
puts "Input file does not exist: #{filename}"
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
true
|
32
|
+
rescue ArgumentError
|
33
|
+
puts "Invalid time limit: #{time_limit}"
|
34
|
+
false
|
35
|
+
end
|
36
|
+
|
37
|
+
def run
|
38
|
+
unless validate_input
|
39
|
+
self.class.usage
|
40
|
+
return
|
41
|
+
end
|
42
|
+
|
43
|
+
output = Timeout.timeout(time_limit.to_i) { `#{cmd} #{filename}` }
|
44
|
+
distance = validate!(output, 'San Francisco')
|
45
|
+
puts("Success: #{distance} km")
|
46
|
+
rescue InvalidError => error
|
47
|
+
$stderr.puts error
|
48
|
+
exit(1)
|
17
49
|
rescue Timeout::Error
|
18
|
-
|
50
|
+
$stderr.puts 'Failed: timeout'
|
51
|
+
exit(1)
|
19
52
|
end
|
20
53
|
|
21
|
-
def validate!(initial_location_name = nil)
|
54
|
+
def validate!(output, initial_location_name = nil)
|
22
55
|
solution = TspRunner::Solution.from_string(output, location_hash)
|
23
56
|
if solution.valid?(initial_location_name)
|
24
57
|
solution.total_distance / 1000.0
|
25
58
|
else
|
26
|
-
raise InvalidError, ['Failed: invalid', output].join("\n")
|
59
|
+
raise InvalidError, ['Failed: invalid, program output:', output].join("\n")
|
27
60
|
end
|
28
61
|
end
|
29
62
|
|
30
63
|
private
|
31
64
|
|
32
65
|
def location_hash
|
33
|
-
@location_hash ||= TspRunner::LocationHash.from_file(
|
66
|
+
@location_hash ||= TspRunner::LocationHash.from_file(filename)
|
34
67
|
end
|
35
68
|
end
|
36
69
|
end
|
data/lib/tsp_runner/version.rb
CHANGED