zazu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +2 -0
  4. data/lib/zazu.rb +110 -0
  5. metadata +61 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 122bb6333e70968f87c722ed46057098565a07f0a93cb1ea5c09d82217477e7e
4
+ data.tar.gz: a0b0493d24f96653457e9853a2fc87108efbd1c958963684451fa55f7878c571
5
+ SHA512:
6
+ metadata.gz: 56e99ec2ef45f43e69cdf274c04af0bfb2f1bdee65e5101b8bf1e3976677eb13b85bc48fc842546d3a191f3fff15c35d2a1fddf19358f547610c65dca674326c
7
+ data.tar.gz: 8b0346c3e0d00771a5a764fe5f90958af5318b07daf69e75d1f06431c663c7de6c2d521385a9042c19cd464fbd8ec5cb4487cc497696dfd48098d979376ff772
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Ryan Calhoun
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # zazu
2
+ Fetch tools and run them
data/lib/zazu.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'colorize'
2
+ require 'etc'
3
+ require 'fileutils'
4
+ require 'logger'
5
+ require 'net/https'
6
+ require 'open3'
7
+ require 'thread'
8
+ require 'tmpdir'
9
+ require 'uri'
10
+
11
+ class Zazu
12
+ VERSION = '0.0.1'
13
+
14
+ class Error < Exception; end
15
+ class DownloadError < Error; end
16
+ class RunError < Error; end
17
+
18
+ attr_reader :name, :path
19
+
20
+ def initialize(name, logger: Logger.new(STDERR), level: Logger::INFO)
21
+ @name = name
22
+ @path = File.join Dir.tmpdir, name + '-download'
23
+ @logger = logger
24
+ @logger.level = level
25
+ end
26
+
27
+ def fetch(url: nil, age: 60*60, &block)
28
+ return false if File.exists?(path) && Time.now - File.stat(path).mtime < age
29
+
30
+ url ||= block.call os, arch
31
+ @logger.info "Downloading from #{url}".cyan
32
+
33
+ download_file url
34
+ end
35
+
36
+ def run(args = [], show: //, hide: /^$/, &block)
37
+ command = [path] + args
38
+ @logger.debug "Running command #{command}".yellow
39
+ Open3.popen3 *command do |i,o,e,t|
40
+ i.close
41
+
42
+ threads = [o,e].map do |x|
43
+ Thread.new do
44
+ x.each_line do |line|
45
+ line.chomp!
46
+ next if line !~ show || line =~ hide
47
+
48
+ if block
49
+ block.call line
50
+ else
51
+ @logger.info line.cyan
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ threads.each &:join
58
+ t.join
59
+
60
+ raise RunError.new "#{path} exited with code #{t.value}" unless t.value == 0
61
+ end
62
+
63
+ true
64
+ end
65
+
66
+ private
67
+
68
+ def download_file(url)
69
+ uri = URI url
70
+ res = Net::HTTP.start uri.host, uri.port, use_ssl: uri.scheme == 'https' do |http|
71
+ http.request Net::HTTP::Get.new uri do |res|
72
+ if res.is_a?(Net::HTTPOK)
73
+ @logger.debug "Opening file #{path}".yellow
74
+ File.open path, 'wb' do |file|
75
+ res.read_body do |chunk|
76
+ file.write chunk
77
+ end
78
+ end
79
+ @logger.debug "Closed file #{path}".yellow
80
+ FileUtils.chmod 0755, path
81
+ end
82
+ end
83
+ end
84
+
85
+ case res
86
+ when Net::HTTPOK
87
+ true
88
+ when Net::HTTPRedirection
89
+ @logger.debug "Following redirect #{res['Location']}"
90
+ download_file res['Location']
91
+ else
92
+ raise DownloadError.new "Error downloading #{url}, got #{res}"
93
+ end
94
+ end
95
+
96
+ def os
97
+ case Etc.uname[:sysname]
98
+ when /linux/i
99
+ :linux
100
+ when /darwin/i
101
+ :mac
102
+ when /windows/i
103
+ :win
104
+ end
105
+ end
106
+
107
+ def arch
108
+ Etc.uname[:machine] =~ /x(86_)?64/ ? 64 : 32
109
+ end
110
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zazu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Calhoun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A configurable build component that can download a tool from a URL if
28
+ necessary, then run it.
29
+ email:
30
+ - ryanjamescalhoun@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - lib/zazu.rb
38
+ homepage: https://github.com/ryancalhoun/zazu
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.0.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Download tools and run them
61
+ test_files: []