sftp_downloader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 496b7e381253bd4984bcb2e1c72a84c332106f67
4
+ data.tar.gz: 0991c6846531d9bbfa605e831a24bb0eacca295c
5
+ SHA512:
6
+ metadata.gz: b44d97a0eed49be27078aaf228d8a142dc944047511bf7531b45edcfee97d9ca43dc0b3a71add39d701bbcb8e85df8627354efa3f3d433b03885ff4965de2a26
7
+ data.tar.gz: 4bf3dca3de13e0bb1fb6ededaa19c853b3ac1a0b9959ff0eafb2122b8a646b0195b326b7f09baa7fe7601562ec0c2310040bc20c2b2e98657507451a03d744d6
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -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, 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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sftp_downloader.gemspec
4
+ gemspec
@@ -0,0 +1,45 @@
1
+ The MIT License (MIT)
2
+
3
+ ,,########################################,,
4
+ .*##############################################*
5
+ ,*####*:::*########***::::::::**######:::*###########,
6
+ .*####: *#####*. :*###,.#######*,####*.
7
+ *####: *#####* .###########* ,####*
8
+ .*####: ,#######, ##########* :####*
9
+ *####. :#########*, ,,,,,,,,. ,####:
10
+ ####* ,##############****************:,, .####*
11
+ :####*#####################################**, *####.
12
+ *#####Copyright (c) 2015, Maurizio De Magnis#*, :####:
13
+ .#############################################*,####*
14
+ :#####:*****#####################################.
15
+ *####: .,,,:*****###########,
16
+ .*####, *######*
17
+ .####* :*#######* ,#####*
18
+ *###############*,,,,,,,,::**######,
19
+ *##############################:
20
+ *####*****##########**#####*
21
+ .####*. :####*
22
+ :####* .#####,
23
+ *####: *####:
24
+ .*####, *####*
25
+ :####*####*
26
+ *######,
27
+ *##,
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy
30
+ of this software and associated documentation files (the "Software"), to deal
31
+ in the Software without restriction, including without limitation the rights
32
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33
+ copies of the Software, and to permit persons to whom the Software is
34
+ furnished to do so, subject to the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be included in
37
+ all copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
45
+ THE SOFTWARE.
@@ -0,0 +1,85 @@
1
+ # SFTP Downloader
2
+
3
+ This is a gem that performs asynchronous SFTP downloads, ensuring that notifications of succesful downloads respect a sequential order.
4
+
5
+ For example, this means that if you download the following files:
6
+
7
+ - /my/files/a.txt
8
+ - /my/files/b.txt
9
+ - /my/files/c.txt
10
+
11
+ from the remote host, all downloads will start concurrently but the notification related to `b.txt` will happen only after the notification related to `a.txt`.
12
+ This is especially useful when you need to import and parse files respecting the remote filename ordering.
13
+
14
+ You can also specify if the downloaded files should be removed from the remote location once the downloads are completed.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'sftp_downloader'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install sftp_downloader
31
+
32
+ ## Usage
33
+
34
+ ### Command line
35
+
36
+ ```bash
37
+ sftp_downloader --help
38
+ ```
39
+
40
+ ### Ruby
41
+
42
+ ```ruby
43
+ require 'sftp_downloader'
44
+ require 'logger'
45
+
46
+ logger = Logger.new(STDOUT)
47
+
48
+ credentials = {
49
+ host: 'INSERT_FTP_HOST_HERE',
50
+ username: 'INSERT_FTP_USERNAME_HERE',
51
+ password: 'INSERT_FTP_PASSWORD_HERE',
52
+ }
53
+
54
+ downloader = SftpDownloader::Client.new(
55
+ credentials: credentials, # required
56
+ download_path: './tmp/downloads', # default: './'
57
+ remote_path: '/out/', # default: '/'
58
+ file_pattern: /.+\.csv/, # default: /.*\..*/
59
+ logger: logger, # default: nil (doesn't log debugging info)
60
+ remove_after_download: true, # default: false
61
+ )
62
+
63
+ downloader.perform do |local_path|
64
+ puts "dealing with #{local_path}"
65
+ end
66
+ ```
67
+
68
+ ## Development
69
+
70
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec sftp_downloader` to use the code located in this directory, ignoring other installed copies of this gem.
71
+
72
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## To do
75
+
76
+ - add some tests (duh!)
77
+ - allow the *"classic mode"* in which files are downloaded regardless their naming order
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it ( https://github.com/[my-github-username]/sftp_downloader/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sftp_downloader"
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
@@ -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,83 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "sftp_downloader"
4
+ require 'optparse'
5
+ require 'logger'
6
+
7
+ default_logger = Logger.new(STDOUT)
8
+ default_logger.formatter = proc do |severity, datetime, progname, msg|
9
+ "#{datetime}: #{msg}\n"
10
+ end
11
+
12
+ options = {
13
+ credentials: {},
14
+ }
15
+ cli_options = {}
16
+
17
+ opt_parse = OptionParser.new do |opts|
18
+ opts.banner = "Usage: sftp_downloader [options]"
19
+
20
+ opts.on("-u username", "--username=username", "Specify the username [required]") do |username|
21
+ options[:credentials][:username] = username
22
+ end
23
+
24
+ opts.on("-p password", "--password=password", "Specify the password [required]") do |password|
25
+ options[:credentials][:password] = password
26
+ end
27
+
28
+ opts.on("-h host", "--host=host", "Specify the host [required]") do |host|
29
+ options[:credentials][:host] = host
30
+ end
31
+
32
+ opts.on("-fp file_pattern", "--file-pattern=file_pattern", "Specify the regexp to use to select the files to deal with") do |file_pattern|
33
+ options[:file_pattern] = Regexp.new(file_pattern)
34
+ end
35
+
36
+ opts.on("-r", "--remove-after-download", "Delete the downloaded files in the remote location") do
37
+ options[:remove_after_download] = true
38
+ end
39
+
40
+ opts.on("-lp path", "--local-path=path", "Specify the local path to save the files to") do |path|
41
+ options[:local_path] = path
42
+ end
43
+
44
+ opts.on("-rp path", "--remote-path=path", "Specify the remote path to download the files from") do |path|
45
+ options[:remote_path] = path
46
+ end
47
+
48
+ opts.on_tail("-h", "--help", "Show this message") do
49
+ puts opts
50
+ exit
51
+ end
52
+
53
+ opts.on_tail("--version", "Show version") do
54
+ puts SftpDownloader::VERSION
55
+ exit
56
+ end
57
+
58
+ opts.on('-v', '--verbose', 'Run verbosely') do
59
+ cli_options[:verbose] = true
60
+ end
61
+
62
+ opts.on('-d', '--debug', 'Show debug info') do
63
+ cli_options[:debug] = true
64
+ options[:logger] = default_logger
65
+ end
66
+ end
67
+
68
+ opt_parse.parse!
69
+
70
+ required_arguments = %i[host username password]
71
+ required_arguments.each do |argument|
72
+ if options[:credentials][argument].nil?
73
+ puts opt_parse
74
+ exit
75
+ end
76
+ end
77
+
78
+ client = SftpDownloader::Client.new(options)
79
+ client.perform do |local_path|
80
+ if cli_options[:verbose]
81
+ puts local_path
82
+ end
83
+ end
@@ -0,0 +1,95 @@
1
+ require "sftp_downloader/version"
2
+ require 'fileutils'
3
+ require 'net/sftp'
4
+
5
+ module SftpDownloader
6
+
7
+ class Client
8
+ def initialize(credentials:, download_path: './', remote_path: '/', file_pattern: /.*\..*/, remove_after_download: false, logger: nil)
9
+ @credentials = credentials
10
+ @download_path = download_path
11
+ @remote_path = remote_path
12
+ @logger = logger
13
+ @file_pattern = file_pattern
14
+ @remove_after_download = remove_after_download
15
+ end
16
+
17
+ def perform(&block)
18
+ check_download_directory_and_create_if_necessary
19
+ Net::SFTP.start(@credentials[:host], @credentials[:username], password: @credentials[:password]) do |sftp|
20
+ log "Connected to: #{@credentials[:host]}"
21
+ files = get_files(sftp: sftp)
22
+ downloads = get_downloads(sftp: sftp, files: files)
23
+ removal_tasks = wait_downloads_completion_yield_and_start_removal_if_necessary(sftp: sftp, downloads: downloads, block: block)
24
+ wait_removal_tasks_if_any(removal_tasks: removal_tasks)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def check_download_directory_and_create_if_necessary
31
+ if File.exists?(@download_path)
32
+ unless File.directory?(@download_path)
33
+ raise "#{@download_path} already exists but isn't a directory"
34
+ end
35
+ else
36
+ FileUtils.mkdir_p(@download_path)
37
+ end
38
+ end
39
+
40
+ def get_files(sftp:)
41
+ sftp.
42
+ dir.
43
+ entries(@remote_path).
44
+ select(&:file?).
45
+ map(&:name).
46
+ grep(@file_pattern).
47
+ sort
48
+ end
49
+
50
+ def get_downloads(sftp:, files:)
51
+ files.map do |file|
52
+ log "Starting download: #{file}"
53
+ sftp.download(File.join(@remote_path, file), File.join(@download_path, file))
54
+ end
55
+ end
56
+
57
+ def wait_downloads_completion_yield_and_start_removal_if_necessary(sftp:, downloads:, block:)
58
+ downloads.map do |download|
59
+ log "Waiting for download: #{download.remote}"
60
+ download.wait
61
+ log "Completed download: #{download.remote}"
62
+
63
+ block.call(download.local)
64
+
65
+ if @remove_after_download
66
+ perform_remove(sftp: sftp, download: download)
67
+ end
68
+ end
69
+ end
70
+
71
+ def perform_remove(sftp:, download:)
72
+ log "Starting removal: #{download.remote}"
73
+ {
74
+ remote: download.remote,
75
+ request: sftp.remove(download.remote)
76
+ }
77
+ end
78
+
79
+ def wait_removal_tasks_if_any(removal_tasks:)
80
+ removal_tasks.compact.each do |removal_task|
81
+ log "Waiting for removal: #{removal_task[:remote]}"
82
+ removal_task[:request].wait
83
+ log "Completed removal: #{removal_task[:remote]}"
84
+ end
85
+ end
86
+
87
+ def log(message)
88
+ if @logger
89
+ @logger.debug(message)
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,3 @@
1
+ module SftpDownloader
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sftp_downloader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sftp_downloader"
8
+ spec.version = SftpDownloader::VERSION
9
+ spec.authors = ["Maurizio De Magnis"]
10
+ spec.email = ["maurizio.demagnis@gmail.com"]
11
+
12
+ spec.summary = %q{Asynchronous SFTP downloads that respect filename ordering.}
13
+ spec.description = %q{This is a gem that performs asynchronous SFTP downloads, ensuring that notifications of succesful downloads respect a sequential order.}
14
+ spec.homepage = "https://github.com/olistik/sftp_downloader"
15
+ spec.license = "MIT"
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
+ if spec.respond_to?(:metadata)
23
+ end
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_runtime_dependency "net-sftp", "~> 2.1.2"
28
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sftp_downloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maurizio De Magnis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-22 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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: net-sftp
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.2
55
+ description: This is a gem that performs asynchronous SFTP downloads, ensuring that
56
+ notifications of succesful downloads respect a sequential order.
57
+ email:
58
+ - maurizio.demagnis@gmail.com
59
+ executables:
60
+ - sftp_downloader
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - exe/sftp_downloader
75
+ - lib/sftp_downloader.rb
76
+ - lib/sftp_downloader/version.rb
77
+ - sftp_downloader.gemspec
78
+ homepage: https://github.com/olistik/sftp_downloader
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Asynchronous SFTP downloads that respect filename ordering.
102
+ test_files: []