file_loader 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/file_loader.gemspec +23 -0
- data/lib/file_loader/version.rb +3 -0
- data/lib/file_loader.rb +110 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 20166382acf8d574784bb8e701df90a86ce57d0a
|
4
|
+
data.tar.gz: 80aef5c99adf2db35f73d83da8dfee24dbf8916a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3cf7ffb81bcb4c6ee6f419918a13c08f4efda3e15b44ad677860e8cf1c9f4c4919b4d9604a805f43e47596f8fcf5aff9ea21e4ac1471c210a4faee14cbb02166
|
7
|
+
data.tar.gz: 093fd90d4227c6dc98cb2ee4c051cb66cd31813713fb0275ce1fe67b91b8bc25078acc9746431179e29d11792c93f42372d52597661cb8f259e34be914872715
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 novikov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# FileLoader
|
2
|
+
|
3
|
+
File loader
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'file_loader'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install file_loader
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/file_loader/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/file_loader.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'file_loader/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "file_loader"
|
8
|
+
spec.version = FileLoader::VERSION
|
9
|
+
spec.authors = ["novikov"]
|
10
|
+
spec.email = ["novikov@inventos.ru"]
|
11
|
+
spec.summary = %q{File loader}
|
12
|
+
spec.description = %q{File loader}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/lib/file_loader.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require "file_loader/version"
|
2
|
+
require 'shellwords'
|
3
|
+
require 'socket'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
module FileLoader
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def parse(url)
|
10
|
+
url.match(/^(?:(?<protocol>\w+):\/\/(?:(?<user>.+?)(?:\:(?<password>.+?))?@)?(?<host>.+?):?)?(?<path>\/.+)?$/) or raise("Invalid url #{url}")
|
11
|
+
end
|
12
|
+
|
13
|
+
def download(src, dst, opts = {})
|
14
|
+
src = src.shellescape
|
15
|
+
dst = dst.shellescape
|
16
|
+
parsed_src = parse(src)
|
17
|
+
unless Dir.exists?(File.dirname(dst))
|
18
|
+
FileUtils.mkdir_p(File.dirname(dst), :mode => opts[:permissions] ? opts[:permissions].to_i(8) : nil)
|
19
|
+
end
|
20
|
+
case parsed_src[:protocol]
|
21
|
+
when 'http', 'https', 'ftp'
|
22
|
+
cmd = "wget #{src} -O #{dst}"
|
23
|
+
when 'scp'
|
24
|
+
if Socket.gethostname == parsed_src[:host]
|
25
|
+
cmd = cp_cmd(parsed_src[:path], dst)
|
26
|
+
else
|
27
|
+
cmd = "scp -r -oBatchMode=yes -oStrictHostKeyChecking=no "
|
28
|
+
cmd += "-l #{speed_limit} " if opts[:speed_limit]
|
29
|
+
cmd += '"'
|
30
|
+
user = parsed_src[:user] || opts[:user]
|
31
|
+
cmd += user if user
|
32
|
+
cmd += ":#{parsed_src[:password]}" if parsed_src[:password]
|
33
|
+
cmd += '@' if user
|
34
|
+
cmd += "#{parsed_src[:host]}:#{parsed_src[:path]}\" #{dst}"
|
35
|
+
end
|
36
|
+
when nil
|
37
|
+
cmd = cp_cmd(src, dst)
|
38
|
+
else
|
39
|
+
raise "Unsupported protocol #{parsed_src[:protocol]}"
|
40
|
+
end
|
41
|
+
opts[:logger].debug(cmd) if opts[:logger]
|
42
|
+
exec_cmd(cmd, opts[:retries].to_i) unless opts[:dry_run]
|
43
|
+
end
|
44
|
+
|
45
|
+
def upload(src, dst, opts = {})
|
46
|
+
src = src.shellescape
|
47
|
+
dst = dst.shellescape
|
48
|
+
parsed_dst = parse(dst)
|
49
|
+
case parsed_dst[:protocol]
|
50
|
+
when 'scp'
|
51
|
+
if Socket.gethostname == parsed_dst[:host]
|
52
|
+
unless Dir.exists?(File.dirname(dst))
|
53
|
+
FileUtils.mkdir_p(File.dirname(dst), :mode => opts[:permissions] ? opts[:permissions].to_i(8) : nil)
|
54
|
+
end
|
55
|
+
cmd = cp_cmd(src, parsed_dst[:path])
|
56
|
+
else
|
57
|
+
cmd = "ssh -oBatchMode=yes -oStrictHostKeyChecking=no "
|
58
|
+
user = parsed_dst[:user] || opts[:user]
|
59
|
+
cmd += user if user
|
60
|
+
cmd += ":#{parsed_dst[:password]}" if parsed_dst[:password]
|
61
|
+
cmd += '@' if user
|
62
|
+
cmd += "#{parsed_dst[:host]} \""
|
63
|
+
if opts[:permissions]
|
64
|
+
umask = 'umask %03o' % ('0777'.to_i - opts[:permissions].to_i)
|
65
|
+
cmd += "(#{umask}; "
|
66
|
+
end
|
67
|
+
cmd += "mkdir -p #{File.dirname(parsed_dst[:path])}"
|
68
|
+
cmd += ")" if opts[:permissions]
|
69
|
+
cmd += "\" && scp -r -oBatchMode=yes -oStrictHostKeyChecking=no "
|
70
|
+
cmd += "-l #{opts[:speed_limit]} " if opts[:speed_limit]
|
71
|
+
cmd += "#{src} \""
|
72
|
+
cmd += user if user
|
73
|
+
cmd += ":#{opts[:password]}" if opts[:password]
|
74
|
+
cmd += '@' if user
|
75
|
+
cmd += "#{parsed_dst[:host]}:#{parsed_dst[:path]}\""
|
76
|
+
end
|
77
|
+
when nil
|
78
|
+
unless Dir.exists?(File.dirname(dst))
|
79
|
+
FileUtils.mkdir_p(File.dirname(dst), :mode => opts[:permissions] ? opts[:permissions].to_i(8) : nil)
|
80
|
+
end
|
81
|
+
cmd = cp_cmd(src, dst)
|
82
|
+
else
|
83
|
+
raise "Unsupported protocol #{parsed_src[:protocol]}"
|
84
|
+
end
|
85
|
+
opts[:logger].debug(cmd) if opts[:logger]
|
86
|
+
exec_cmd(cmd, opts[:retries].to_i) unless opts[:dry_run]
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def exec_cmd(cmd, retries = 0, delay = 5)
|
92
|
+
return unless cmd
|
93
|
+
cmd += " 2>&1"
|
94
|
+
(retries + 1).times do |n|
|
95
|
+
res = `#{cmd}`
|
96
|
+
if $?.exitstatus == 0
|
97
|
+
break
|
98
|
+
elsif n >= retries
|
99
|
+
raise res
|
100
|
+
else
|
101
|
+
sleep delay
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def cp_cmd(src, dst, permissions = nil)
|
107
|
+
return if src == dst
|
108
|
+
"cp -r #{src} #{dst}"
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_loader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- novikov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-15 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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
|
+
description: File loader
|
42
|
+
email:
|
43
|
+
- novikov@inventos.ru
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- file_loader.gemspec
|
54
|
+
- lib/file_loader.rb
|
55
|
+
- lib/file_loader/version.rb
|
56
|
+
homepage: ''
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.2.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: File loader
|
80
|
+
test_files: []
|