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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4de20bb835ad9abb7ca9b8200d3900881d9445d9
4
- data.tar.gz: 4b8b15b499659263bf014641bc2f8b855cc2572d
3
+ metadata.gz: b3b74d923c3314308b08c1e20305017d452b7730
4
+ data.tar.gz: 060ae82a558dca144846542b7b483f0234751f93
5
5
  SHA512:
6
- metadata.gz: deba5646b3df24fca5c236dd821e766ac71b84a64522d622157299b59b64bbdc416f36df6c2bddffb4603b567cc2d3da309db9164e1f9a53138d6600a2113fa0
7
- data.tar.gz: 5667cf34f1385ad59ddb692270ad743af61b569462949cedec938231750436e1083222e8e6714ccc91da3db8a2780b77039946ac6c2892014c6d4cce929e3545
6
+ metadata.gz: 854ef5741aba85fe543bacb7bbb4dda18c0749bd68f1b0154b0d02054df65d49fa885d1872aa0ef219cf296f9bea3d751ab351200e598fb615e7b147640dd4a5
7
+ data.tar.gz: 4aebefcea41fc014b37075278a20f32dbf34e7876546855dcb68c3ce2a4d84841ee1b5fcf2e52450bae488a5ebaab678f8f6d74a70247b124b67c41d0c749f0f
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ /tsp_runner-*.gem
1
2
  /.bundle/
2
3
  /.yardoc
3
4
  /Gemfile.lock
@@ -2,14 +2,5 @@
2
2
  require 'bundler/setup'
3
3
  require 'tsp_runner'
4
4
 
5
- filename = File.join(File.dirname(__FILE__), '..', 'data', 'input', 'sample_10.csv')
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
@@ -2,35 +2,68 @@ require 'timeout'
2
2
 
3
3
  module TspRunner
4
4
  class Runner
5
- class BaseError < StandardError; end
6
- class TimeoutError < BaseError; end
7
- class InvalidError < BaseError; end
5
+ class InvalidError < StandardError; end
8
6
 
9
- attr_reader :cmd, :input_filename, :output
7
+ attr_reader :cmd, :filename, :time_limit
10
8
 
11
- def initialize(cmd, input_filename)
12
- @cmd, @input_filename = cmd, input_filename
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 run(time_limit)
16
- @output = Timeout.timeout(time_limit) { `#{cmd} #{input_filename}` }
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
- raise TimeoutError, 'Failed: timeout'
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(input_filename)
66
+ @location_hash ||= TspRunner::LocationHash.from_file(filename)
34
67
  end
35
68
  end
36
69
  end
@@ -1,3 +1,3 @@
1
1
  module TspRunner
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tsp_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Collier