native_ext_fetcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e631820654fb2882e0bcc0b2304e1364e1c615e
4
+ data.tar.gz: 35277f285157e5bb2164e43c640f9c53c2ed7f35
5
+ SHA512:
6
+ metadata.gz: 096fac5a0deb44edf0b7ab988ee52a4b7973ef46856f4ab0f83d434e8a32c6a8cf37f2eefa230f30701c2d6eb0515f6b3c29263a25842c84d7345b278367a6a7
7
+ data.tar.gz: c0fabcfa8da1b706c6f23669d82d92ec20b68ead9501e018260d40d17b2ca559f8f7480673a8f62aa18d33828727320620b23109c3cfe1e954ce6a591ede3f2b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in native_ext_fetcher.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'guard'
8
+ gem 'guard-minitest'
9
+ end
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :minitest do
2
+ # with Minitest::Unit
3
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
4
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
5
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Michael Coyne
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # NativeExtFetcher
2
+
3
+ This is a simple gem designed to be used in an extconf.rb file for
4
+ Ruby gems with native extensions. It will detect and normalize host
5
+ details and will attempt to download the proper native library from a
6
+ remote host. This is especially useful with gems that use Rust native
7
+ libraries.
8
+
9
+ This code was abstracted from the
10
+ [Skylight](https://github.com/skylightio/skylight-ruby) gem.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'native_ext_fetcher'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install native_ext_fetcher
27
+
28
+ ## Usage
29
+
30
+ To use this gem, you'd add something like following to your gem's
31
+ extconf.rb
32
+
33
+ ```ruby
34
+ include NativeExtFetcher
35
+
36
+ # Configure the NativeExtFetcher
37
+ native_ext_config 's3.amazonaws.com/bucket_name/some/path', __dir__, {
38
+ linux_x86_64: '[sha2 checksum]'
39
+ }
40
+
41
+ # Fetch the library from S3
42
+ native_ext_fetch! 'libmynativelib'
43
+
44
+ # Setup our Makefile as normal
45
+ LIB_DIRS = [RbConfig::CONFIG['libdir'], native_ext_download_path]
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/native_ext_fetcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
57
+
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
62
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "native_ext_fetcher"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,57 @@
1
+ module NativeExtFetcher
2
+ class Fetcher
3
+ InvalidDigest = Class.new(StandardError)
4
+
5
+ attr_reader :download_path
6
+
7
+ def config(host, path, checksums, options = {})
8
+ @lib_path = File.expand_path(File.join(path, '..', '..', 'lib'))
9
+
10
+ raise ArgumentError, 'Checksums must be a hash' unless Hash === checksums
11
+ raise ArgumentError, 'Library path not found ' unless Dir.exists?(@lib_path)
12
+
13
+ @host = host
14
+ @checksums = checksums
15
+ @options = options
16
+
17
+ @download_path = expand_download_path
18
+ end
19
+
20
+ def fetch!(library)
21
+ File.open(library_local_path(library), 'w+') do |file|
22
+ file.truncate 0 # empty file
23
+
24
+ uri = library_request_uri(library)
25
+ digest = http_client.get_file(file, uri)
26
+
27
+ unless digest == expected_digest
28
+ raise InvalidDigest, "invalid checksums for #{library} from #{uri}"
29
+ end
30
+ end
31
+ end
32
+
33
+ protected
34
+
35
+ def http_client
36
+ @http_client ||= Http.new(@options)
37
+ end
38
+
39
+ def expected_digest
40
+ @checksums[Platform.native_extension_key]
41
+ end
42
+
43
+ def library_local_path(library)
44
+ File.join @download_path, "#{library}#{Platform.native_extension_file_ext}"
45
+ end
46
+
47
+ def library_request_uri(library)
48
+ "https://#{@host}/#{library}#{Platform.native_extension_file_postfix}"
49
+ end
50
+
51
+ def expand_download_path
52
+ File.join(@lib_path, 'native', *Platform.native_extension_tuple).tap do |native_path|
53
+ FileUtils.mkdir_p native_path
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,91 @@
1
+ require 'fileutils'
2
+ require 'digest/sha2'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module NativeExtFetcher
7
+ class Http
8
+ MAX_REDIRECTS = 5
9
+ MAX_RETRIES = 3
10
+
11
+ MaxRedirect = Class.new(StandardError)
12
+ MaxRetries = Class.new(StandardError)
13
+
14
+ def initialize(options = {})
15
+ @max_redirects = options[:max_redirects] || MAX_REDIRECTS
16
+ @max_retries = options[:max_retries] || MAX_RETRIES
17
+
18
+ if http_proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
19
+ uri = URI.parse(http_proxy)
20
+
21
+ @proxy_host, @proxy_port = uri.host, uri.port
22
+ @proxy_user, @proxy_pass = uri.userinfo.split(/:/) if uri.userinfo
23
+ end
24
+ end
25
+
26
+ def get_file(out, uri)
27
+ remaining_attempts = @max_retries
28
+
29
+ @max_redirects.times do |i|
30
+ begin
31
+ host, port, path = deconstruct_uri(uri)
32
+ status, result = http_get(out, host, port, path)
33
+
34
+ case status
35
+ when :success
36
+ return result
37
+
38
+ else uri = result
39
+ end
40
+
41
+ rescue => e
42
+ remaining_attempts -= 1
43
+
44
+ unless remaining_attempts.zero?
45
+ sleep 2
46
+ retry
47
+ end
48
+
49
+ raise MaxRetries, "max retries; #{e.message}"
50
+ end
51
+ end
52
+
53
+ raise MaxRedirect, 'max redirects hit'
54
+ end
55
+
56
+ protected
57
+
58
+ def deconstruct_uri(uri)
59
+ uri = URI.parse(uri)
60
+
61
+ [ uri.host, uri.port, uri.path ]
62
+ end
63
+
64
+ def http_get(out, host, port, path)
65
+ Net::HTTP.start(host, port, @proxy_host, @proxy_pass, @proxy_user, @proxy_pass, use_ssl: true) do |http|
66
+ http.request_get path do |resp|
67
+ case resp
68
+ when Net::HTTPSuccess
69
+ digest = Digest::SHA2.new
70
+
71
+ resp.read_body do |chunk|
72
+ digest << chunk
73
+ out.write chunk
74
+ end
75
+
76
+ return :success, digest.hexdigest
77
+
78
+ when Net::HTTPRedirection
79
+ unless location = resp['location']
80
+ raise "received redirect but no location"
81
+ end
82
+
83
+ return :redirect, location
84
+ else
85
+ raise "received HTTP status code #{resp.code}"
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,68 @@
1
+ require 'rbconfig'
2
+
3
+ module NativeExtFetcher
4
+ module Platform
5
+ class << self
6
+ protected
7
+
8
+ def determine_host_os
9
+ case os = RbConfig::CONFIG['host_os'].downcase
10
+ when /linux/
11
+ 'linux'
12
+ when /darwin/
13
+ 'darwin'
14
+ when /freebsd/
15
+ 'freebsd'
16
+ when /netbsd/
17
+ 'netbsd'
18
+ when /openbsd/
19
+ 'openbsd'
20
+ when /sunos|solaris/
21
+ 'solaris'
22
+ when /mingw|mswin/
23
+ 'windows'
24
+ else os
25
+ end
26
+ end
27
+
28
+ def determine_host_arch
29
+ case cpu = RbConfig::CONFIG['host_cpu'].downcase
30
+ when /amd64|x86_64/
31
+ 'x86_64'
32
+ when /i?86|x86|i86pc/
33
+ 'x86'
34
+ when /ppc|powerpc/
35
+ 'powerpc'
36
+ when /^arm/
37
+ 'arm'
38
+ else
39
+ cpu
40
+ end
41
+ end
42
+
43
+ def determine_host_dlext
44
+ RbConfig::CONFIG['DLEXT'].downcase
45
+ end
46
+ end
47
+
48
+ HOST_OS = determine_host_os.freeze
49
+ HOST_ARCH = determine_host_arch.freeze
50
+ HOST_DLEXT = determine_host_dlext.freeze
51
+
52
+ def self.native_extension_key
53
+ :"#{HOST_OS}_#{HOST_ARCH}"
54
+ end
55
+
56
+ def self.native_extension_file_postfix
57
+ "-#{HOST_OS}-#{HOST_ARCH}.#{HOST_DLEXT}"
58
+ end
59
+
60
+ def self.native_extension_file_ext
61
+ ".#{HOST_DLEXT}"
62
+ end
63
+
64
+ def self.native_extension_tuple
65
+ [HOST_OS, HOST_ARCH]
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module NativeExtFetcher
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'fileutils'
2
+
3
+ require 'native_ext_fetcher/version'
4
+ require 'native_ext_fetcher/platform'
5
+ require 'native_ext_fetcher/fetcher'
6
+ require 'native_ext_fetcher/http'
7
+
8
+ module NativeExtFetcher
9
+ def self.instance
10
+ @instance ||= Fetcher.new
11
+ end
12
+
13
+ instance.public_methods(false).each do |method|
14
+ class_eval <<-RUBY_EVAL
15
+ def native_ext_#{method}(*args)
16
+ NativeExtFetcher.instance.#{method}(*args)
17
+ end
18
+ RUBY_EVAL
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'native_ext_fetcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "native_ext_fetcher"
8
+ spec.version = NativeExtFetcher::VERSION
9
+ spec.authors = ["DefWare LLC"]
10
+ spec.email = ["michael@defware.io"]
11
+ spec.license = "MIT"
12
+
13
+ spec.summary = %q{Native extenstion fetch library}
14
+ spec.description = %q{Fetch native libraries based upon platform details}
15
+ spec.homepage = "https://github.com/DefWare/native_ext_fetcher"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.8"
25
+ spec.add_development_dependency "fakefs", "~> 0.8"
26
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: native_ext_fetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - DefWare LLC
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-13 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fakefs
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ description: Fetch native libraries based upon platform details
70
+ email:
71
+ - michael@defware.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Guardfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/native_ext_fetcher.rb
87
+ - lib/native_ext_fetcher/fetcher.rb
88
+ - lib/native_ext_fetcher/http.rb
89
+ - lib/native_ext_fetcher/platform.rb
90
+ - lib/native_ext_fetcher/version.rb
91
+ - native_ext_fetcher.gemspec
92
+ homepage: https://github.com/DefWare/native_ext_fetcher
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Native extenstion fetch library
116
+ test_files: []