Skip to content
This repository has been archived by the owner on Sep 13, 2020. It is now read-only.

do not rely on tar and curl command #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/knapsack/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def self.find_by_name name, *requirements
dependency = Gem::Dependency.new(name, *requirements)

candidates = _all.select { |r|
dependency.match? r.name, r.version
dependency.match? r.name, r.version.to_s.gsub(/[a-z]/i, "")
}.sort_by { |r| r.version }

candidates.last
Expand Down
38 changes: 29 additions & 9 deletions lib/knapsack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
require "digest/md5"
require "digest/sha2"
require "fileutils"
require "open-uri"
require "psych"
require 'zlib'
require 'rubygems/package'

module Knapsack
module Utils
Expand All @@ -12,12 +15,9 @@ def download(url, filename)

ensure_tree File.dirname(filename)

cmd = ["curl", "-L", "-s", "-S", url, "-o", filename]
tmpfile = open(url, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE)

pid = Process.spawn(*cmd, :err => [:child, :out], :out => IO::NULL)
_, status = Process.wait2(pid)

status.success?
File.open(filename, "wb") { |file| file.write(tmpfile.read) }
end
module_function :download

Expand All @@ -38,12 +38,32 @@ def extract(filename, target, options = {})
raise failure_message % ["MD5", options[:md5], digest]
end

cmd = ["tar", "-xf", filename, "-C", target]
file = File.open(filename, "rb")

Gem::Package::TarReader.new(Zlib::GzipReader.new(file)) do |tar|
symlinks = {}

tar.each do |entry|
dest = File.join(target, entry.full_name)

if entry.header.typeflag != '2'
FileUtils.mkdir_p(File.dirname(dest))
else
src = File.dirname(dest) + "/" + entry.header.linkname
symlinks[src] = dest
end

pid = Process.spawn(*cmd, :err => [:child, :out], :out => IO::NULL)
_, status = Process.wait2(pid)
if entry.file?
File.open(dest, "wb") { |f| f.write(entry.read) }
end
end

symlinks.each do |src, dest|
FileUtils.cp_r(src, dest)
end
end

status.success? || options[:ignore_extract_errors]
file.close
end
module_function :extract

Expand Down