From 524dce7d1e81c347129c45491a107c690a071abf Mon Sep 17 00:00:00 2001 From: Xavier Shay Date: Tue, 11 Nov 2014 14:40:38 -0800 Subject: [PATCH] Remove rangeclient, moved to square/rangeclient.git --- ruby_rangeclient/LICENSE.md | 13 -- ruby_rangeclient/README.md | 16 -- ruby_rangeclient/bin/er | 75 -------- ruby_rangeclient/bin/range-compress | 39 ---- ruby_rangeclient/bin/range_diff | 30 ---- ruby_rangeclient/bin/range_split | 36 ---- ruby_rangeclient/doit.sh | 7 - ruby_rangeclient/lib/range/fake_client.rb | 9 - ruby_rangeclient/lib/rangeclient.rb | 210 ---------------------- ruby_rangeclient/rangeclient.gemspec | 21 --- ruby_rangeclient/t/local.t | 18 -- 11 files changed, 474 deletions(-) delete mode 100644 ruby_rangeclient/LICENSE.md delete mode 100644 ruby_rangeclient/README.md delete mode 100755 ruby_rangeclient/bin/er delete mode 100755 ruby_rangeclient/bin/range-compress delete mode 100755 ruby_rangeclient/bin/range_diff delete mode 100755 ruby_rangeclient/bin/range_split delete mode 100755 ruby_rangeclient/doit.sh delete mode 100644 ruby_rangeclient/lib/range/fake_client.rb delete mode 100755 ruby_rangeclient/lib/rangeclient.rb delete mode 100644 ruby_rangeclient/rangeclient.gemspec delete mode 100755 ruby_rangeclient/t/local.t diff --git a/ruby_rangeclient/LICENSE.md b/ruby_rangeclient/LICENSE.md deleted file mode 100644 index 15e4cf8..0000000 --- a/ruby_rangeclient/LICENSE.md +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2011 Square Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/ruby_rangeclient/README.md b/ruby_rangeclient/README.md deleted file mode 100644 index f448cb9..0000000 --- a/ruby_rangeclient/README.md +++ /dev/null @@ -1,16 +0,0 @@ -# rangeclient synopsis - - - rangehost = ARGV.shift - rangearg = ARGV.shift - - # Create range object for communication with ranged - r = Range::Client.new({:host => rangehost}) - - # use ranged to expand the range expression into an Array - # "foo10..12" => [ foo10, foo11, foo12 ] OR %foo => [ foo10, foo11, foo12 ] - hosts = r.expand(rangearg) - - # use ranged to compress the array of hostnames into a range expression - # [ foo10, foo11, foo12 ] => "foo10..12" - range_exp = r.compress(hosts) diff --git a/ruby_rangeclient/bin/er b/ruby_rangeclient/bin/er deleted file mode 100755 index d2feaf7..0000000 --- a/ruby_rangeclient/bin/er +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/ruby - -require 'rubygems' -require 'rangeclient' - -require 'optparse' - -options = { - :timeout => 60, -} -optparse = OptionParser.new do |opts| - opts.on('-e', '--expand', 'Print one element per line') do |arg| - options[:expand] = arg - end - opts.on('-c', '--count', 'Print the count of range elements, not the range itself') do |arg| - options[:count] = arg - end - opts.on('-s', '--sort', 'Sort expanded output') do |arg| - options[:sorted] = arg - end - opts.on('-v', '--vip range:80', 'Which host or host:port to query') do |arg| - host, port = arg.split(':', 2) - options[:host] = host - options[:port] = port unless port.nil? - end - opts.on('-p', '--port 80', 'Port to use when connecting') do |arg| - options[:port] = arg - end - opts.on('-t', '--timeout TIMEOUT', "Wait this long for a response") do |arg| - options[:timeout] = arg.to_i - end - opts.on_tail('-h', '--help', "This help message") do - puts opts - exit - end -end -optparse.parse! - -range = Range::Client.new(options) - -in_range = ARGV.join ',' - -if in_range.empty? - puts optparse - STDERR.puts "[ERROR] No range specified" - exit 1 -end - -if in_range == '-' - in_range = STDIN.readlines.map { |l| l.chomp }.join ',' -end - -nodes = range.expand(in_range) - -if not range.rangeexception.nil? - STDERR.puts "[WARNING] " + range.rangeexception -end - -if options[:count] - # if counting, return count - puts nodes.size -elsif not nodes.size.zero? - # if we got a result, display it accordingly - if options[:expand] - nodes.sort! if options[:sorted] - nodes.each do |node| - puts node - end - else - puts range.compress(nodes) - end -else - STDERR.puts "[WARNING] No nodes returned" - exit 1 -end diff --git a/ruby_rangeclient/bin/range-compress b/ruby_rangeclient/bin/range-compress deleted file mode 100755 index 2fe52c3..0000000 --- a/ruby_rangeclient/bin/range-compress +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/ruby - -require 'rubygems' -require 'rangeclient' -require 'pp' - -require 'optparse' - -optparse = OptionParser.new do |opts| - opts.banner = "Usage: #{__FILE__} " - opts.on_tail('-h', '--help', "This help message") do - puts opts - exit - end -end -optparse.parse! - -range = Range::Client.new() - -in_range = ARGV.join(',') - -if in_range.empty? - puts optparse - puts "[ERROR] No range specified" - exit 1 -end - -if in_range == '-' - in_range = STDIN.readlines.map { |l| l.chomp }.join(',') -end - -nodes = range.compress(in_range.split(',')) - -if not nodes.size.zero? - puts nodes -else - puts "[WARNING] No nodes returned" - exit 1 -end diff --git a/ruby_rangeclient/bin/range_diff b/ruby_rangeclient/bin/range_diff deleted file mode 100755 index df444a2..0000000 --- a/ruby_rangeclient/bin/range_diff +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/ruby - -require 'rubygems' -require 'rangeclient' - -first_range = ARGV.shift -second_range = ARGV.shift -raise "#{$0} " unless first_range and second_range - -r = Range::Client.new -first_hosts = r.expand(first_range) -second_hosts = r.expand(second_range) - -first_hosts.sort! -second_hosts.sort! - -retval = 0 -if first_hosts == second_hosts - puts "Ranges are equal." -else - if not (first_hosts - second_hosts).length.zero? - puts "Only in argv[1]: #{r.compress(first_hosts - second_hosts)}" - retval += 1 - end - if not (second_hosts - first_hosts).length.zero? - puts "Only in argv[2]: #{r.compress(second_hosts - first_hosts)}" - retval += 1 - end -end -exit(retval) diff --git a/ruby_rangeclient/bin/range_split b/ruby_rangeclient/bin/range_split deleted file mode 100755 index f1a6ef9..0000000 --- a/ruby_rangeclient/bin/range_split +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/ruby -# given a range and N buckets, slice the range -# into N buckets, attempting even bucket distribution -# based on hostname - -require 'rubygems' -require 'rangeclient' - -range_arg = ARGV.shift; -num_buckets = ARGV.shift; -num_buckets = num_buckets.to_i - -r = Range::Client.new -hosts = r.expand(range_arg) -hosts.sort! - -buckets = [] - -# if we asked for more buckets than exist for hosts -# reduce the number of buckets to be 1:1 with number of hosts -num_buckets = hosts.length if num_buckets > hosts.length - -num_buckets.times do |ii| - buckets << [] -end - -current_bucket = 0 -hosts.each do |host| - buckets[current_bucket] << host - current_bucket += 1 - current_bucket = current_bucket % num_buckets -end - -buckets.each do |hosts| - puts r.compress(hosts) -end diff --git a/ruby_rangeclient/doit.sh b/ruby_rangeclient/doit.sh deleted file mode 100755 index 2447194..0000000 --- a/ruby_rangeclient/doit.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -gem build rangeclient.gemspec - -cd t -for i in *.t; do echo "Testing: $i"; ./$i || exit 1; done - diff --git a/ruby_rangeclient/lib/range/fake_client.rb b/ruby_rangeclient/lib/range/fake_client.rb deleted file mode 100644 index c7e6213..0000000 --- a/ruby_rangeclient/lib/range/fake_client.rb +++ /dev/null @@ -1,9 +0,0 @@ -class Range - # Provide a fake client for use in testing. Ideally a set of tests should be - # run against both this and the real client to ensure they are sync. - FakeClient = Struct.new(:responses) do - def expand(query) - responses.fetch(query) - end - end -end diff --git a/ruby_rangeclient/lib/rangeclient.rb b/ruby_rangeclient/lib/rangeclient.rb deleted file mode 100755 index eea3c86..0000000 --- a/ruby_rangeclient/lib/rangeclient.rb +++ /dev/null @@ -1,210 +0,0 @@ -#!/usr/bin/ruby - -require 'rubygems' -require 'net/http' -require 'uri' - -class Range::Client - attr_accessor :host, :port, :timeout, :rangeexception - - # used to split hostnames into component parts for compression - @@NodeRegx = / - ([-\w.]*?) # $1 - prefix - (\d+) # $2 - start of range - (\.[-A-Za-z\d.]*[-A-Za-z]+[-A-Za-z\d.]*)? # optional domain - /x; - - def initialize(options = {}) - @host = 'range' - @host = ENV['RANGE_HOST'] if ENV.has_key?('RANGE_HOST') - @host = options[:host] if options.member?(:host) - - @port = '80' - @port = ENV['RANGE_PORT'] if ENV.has_key?('RANGE_PORT') - @port = options[:port] if options.member?(:port) - - @ssl = ENV['RANGE_SSL'] || options[:ssl] - - @timeout = 60 - @timeout = options[:timeout] if options.member?(:timeout) - end - - def expand(arg) - escaped_arg = URI.escape arg - http = Net::HTTP.new(@host, @port) - http.read_timeout = @timeout - http.use_ssl = @ssl - req = Net::HTTP::Get.new('/range/list?' + escaped_arg) - resp = http.request(req) - @rangeexception = resp['rangeexception'] - return resp.body.split "\n" - end - - -# Keep this extremely basic code for reference -# def compress(nodes) -# escaped_arg = CGI.escape nodes.join "," -# return RestClient.get "http://#{@options[:host]}:#{@options[:port]}/range/expand?#{escaped_arg}" -# end - -# Take a page from the Perl Seco::Data::Range and perform this locally -- more efficient in both speed/size -# This was ported over from the Perl version, so it's not quite idiomatic ruby - - def compress(nodes) - domain_tbl = {} - no_domain_list = [] - nodes.each do |n| - # If this is a quoted range, just compress it without collapsing - return _simple_compress(nodes) if n =~ /^(?:"|q\()/ - - # Break out host and key by domain, to enable {foo1,foo3}.bar.com grouping - host, domain = n.split('.', 2) - if domain - domain_tbl[domain] ||= [] - domain_tbl[domain] << host - else - no_domain_list << host - end - end - result = [] - # Range elements with no domain component do not group - # just return - if not no_domain_list.empty? - result << _simple_compress(no_domain_list) - end - - domain_tbl.keys.sort.each do |domain| - r = _extra_compress(domain_tbl[domain]) - r.gsub!(/\.#{domain},/) {","} - r.gsub!(/\.#{domain}$/) {""} - if r=~ /,/ - r = "{#{r}}" - end - result << "#{r}.#{domain}" - end - return result.join "," - end - - def _extra_compress(nodes) - domains = {} - nodes = nodes.dup - nodes.each do |node| - node.gsub!(/^([a-z]+)(\d+)([a-z]\w+)\./) { "#{$1}#{$2}.UNDOXXX#{$3}." } - end - result = _simple_compress(nodes) - result.each_char do |r| - r.gsub!(/(\d+\.\.\d+)\.UNDOXXX/) {"{#{$1}}"} - r.gsub!(/(\d+)\.UNDOXXX/) {"#{$1}"} - end - return result - end - - def _simple_compress(nodes) - # dedup nodes - set = {} - nodes.each do |node| - set[node] = true - end - nodes = set.keys - nodes = _sort_nodes(nodes) - - result = [] - prev_prefix, prev_digits, prev_suffix = "", nil, "" - prev_n = nil - count = 0 - - nodes.each do |n| - if n =~ /\A#{@@NodeRegx}\z/ - # foo100abc => foo 100 abc - prefix, digits, suffix = $1, $2, $3 - prefix = "" if prefix.nil? - suffix = "" if suffix.nil? - else - prefix, digits, suffix = n, nil, nil - end - if (not digits.to_i.zero?) and - (prefix == prev_prefix) and - (suffix == prev_suffix) and - (not prev_digits.nil?) and - (digits.to_i == prev_digits.to_i + count + 1) - count += 1 - next - end - - if prev_n - if count > 0 - result << _get_group(prev_prefix, prev_digits, count, prev_suffix) - else - result << prev_n - end - end - prev_n = n - prev_prefix = prefix - prev_digits = digits - prev_suffix = suffix - count = 0 - end #nodes.each - - if count > 0 - result << _get_group(prev_prefix, prev_digits, count, prev_suffix) - else - result << prev_n - end - return result.join "," - end - - def _sort_nodes(nodes) - sorted = nodes.map { |n| - # decorate-sort-undecorate - # FIXME can this all be pushed into sort_by? - n =~ /\A#{@@NodeRegx}\z/ - - [ n, - $1.nil? ? "" : $1, - $2.nil? ? 0 : $2.to_i, - $3.nil? ? "" : $3, - ] - }.sort_by { |e| - [ e[1], e[3], e[2], e[0] ] - }.map { |n| - n[0] - } - return sorted - end - - def _get_group(prefix, digits, count, suffix) - prefix = "" if prefix.nil? - group = sprintf("%s%0*d..%s", - prefix, - digits.to_s.length, - digits.to_i, # sometimes has leading zeroes - _ignore_common_prefix(digits, (digits.to_i + count).to_s) - ) - suffix = "" if suffix.nil? - return group + suffix - end - - def _ignore_common_prefix(start_pos, end_pos) - len_start = start_pos.to_s.length - return end_pos if len_start < end_pos.to_s.length - pick = 0 - len_start.times do |i| - pick = i - # find the point at which the two strings deviate - break if (start_pos[0..i] != end_pos[0..i]) - end - # and return that substring prior to deviation - return end_pos[pick..-1] - end - -end - -if __FILE__ == $0 - require 'pp' - rangearg = ARGV.shift - r = Range::Client.new - hosts = r.expand(rangearg) - pp hosts - pp r.compress(hosts) - pp r.rangeexception -end diff --git a/ruby_rangeclient/rangeclient.gemspec b/ruby_rangeclient/rangeclient.gemspec deleted file mode 100644 index 63c3004..0000000 --- a/ruby_rangeclient/rangeclient.gemspec +++ /dev/null @@ -1,21 +0,0 @@ -# -*- encoding: utf-8 -*- -Gem::Specification.new do |s| - s.name = "rangeclient" - s.version = "0.0.13" - s.platform = Gem::Platform::RUBY - s.authors = ["Evan Miller"] - s.email = ["evan@squareup.com"] - s.summary = "Simple range client for ruby." - s.description = "Use with range from https://github.com/square/libcrange" - s.homepage = "https://github.com/square/prodeng/tree/master/ruby_rangeclient" - - s.required_rubygems_version = ">= 1.3.6" - - s.default_executable = %q{er} - s.executables = %W{ er range-compress range_split range_diff} - - s.files = Dir.glob("lib/**/*") + Dir.glob("bin/*") + %w(README.md) - s.extra_rdoc_files = ["LICENSE.md"] - s.rdoc_options = ["--charset=UTF-8"] -end - diff --git a/ruby_rangeclient/t/local.t b/ruby_rangeclient/t/local.t deleted file mode 100755 index 6f6d31f..0000000 --- a/ruby_rangeclient/t/local.t +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/ruby -# local tests which don't require a range server - -$:.push '../lib' - -require 'test/unit' -require 'rangeclient' - - -class TestRangeClient < Test::Unit::TestCase - - def test_compress - r = Range::Client.new - assert_equal(r.compress(%W{foo100 foo101}), "foo100..1") - end - -end -