rsync_wrapper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rsync_wrapper/rsync.rb +56 -0
- data/lib/rsync_wrapper/version.rb +3 -0
- data/lib/rsync_wrapper.rb +2 -0
- data/rsync_wrapper.gemspec +23 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1bca87d515e5e5fbf59592fde27ffd171e51757c
|
4
|
+
data.tar.gz: d70b4de43a3fd3f22e48a16fe67b9882aa83e84a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f890de180a81cb136a347546db93b6613448fc58e46c960600c6fabb6da56fe0d53142e0da7c84cac7f4b88fd1dbe2a7670291275a3b06f6f716b8b263963f5
|
7
|
+
data.tar.gz: a64832d767154d5b06a1b74225c2565b4f31419159f51a0690f6b1ec4112585682891c55a85d62b9da29bdb1c35c4cb71e7434d415b46a385952f237b619e9ff
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sean Huber
|
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,73 @@
|
|
1
|
+
rsync_wrapper
|
2
|
+
==============
|
3
|
+
|
4
|
+
`rsync_wrapper` is a simple gem that provides a ruby wrapper to the [rsync](http://linux.die.net/man/1/rsync) file copying tool.
|
5
|
+
|
6
|
+
|
7
|
+
Requirements and Dependencies
|
8
|
+
-----------------------------
|
9
|
+
|
10
|
+
Developed/Tested with Ruby version 2.3, but it should work with any version >= 1.9. This gem will only work on an *NIX (OSX/Linux) based operating system that has `rsync`.
|
11
|
+
|
12
|
+
|
13
|
+
Installation
|
14
|
+
-----------------------------
|
15
|
+
|
16
|
+
Add to your `Gemfile`:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'rsync_wrapper'
|
20
|
+
```
|
21
|
+
|
22
|
+
|
23
|
+
Usage
|
24
|
+
-----------------------------
|
25
|
+
|
26
|
+
First, instantiate an instance of `Rsync`:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
rsync = Rsync.new(**options)
|
30
|
+
```
|
31
|
+
|
32
|
+
The constructor accepts the following named arguments:
|
33
|
+
|
34
|
+
| Argument Name | Required? | Object Type | Description |
|
35
|
+
|-----------------------|-----------|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
36
|
+
| `:src_dir` | Required | `String` | An absolute path representing the source directory to have `rsync` copy. e.g., `"/Users/seanhuber/Documents/my_pdfs"` |
|
37
|
+
| `:dest_dir` | Required | `String` | An absolute path representing the destination directory that `rsync` will copy the `:src_dir` to. |
|
38
|
+
| `:include_extensions` | Optional | `Array` | An array of symbols or strings for each file extension `rsync` should include, e.g., `[:doc, :docx, :pdf]` |
|
39
|
+
| `:subdirs_only` | Optional | `Boolean` | An option to have `rsync` only sync files that are in subfolders of `:src_dir`. Defaults to `false`. |
|
40
|
+
| `:log_dir` | Optional | `String` | `rsync_wrapper` has `rsync` pipe its results into a logfile so that the ruby code can then parse this file (and then deletes it). This option should be set to the absolute path of the directory where the temporary logfile will be stored. |
|
41
|
+
| `:logfile` | Optional | `String` | If the `:log_dir` is not specified, you can provide an explicit path for `rsync_wrapper`'s temporary logfile. |
|
42
|
+
|
43
|
+
To execute `rsync`, invoke the `sync!` method which accepts a block with 2 parameters:
|
44
|
+
|
45
|
+
| Argument Index | Object Type | Description |
|
46
|
+
|----------------|-------------|-----------------------------------------------------------------------------------------|
|
47
|
+
| `0` | `String` | The relative path (relative to `:dest_dir` from above) of the copied file. |
|
48
|
+
| `1` | `Boolean` | `true` if the copied file is a new file. `false` if the copied file is an updated file. |
|
49
|
+
|
50
|
+
Example:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
source_directory = '/Users/seanhuber/Documents/my_pdfs'
|
54
|
+
destination_directory = '/Users/seanhuber/Documents/my_pdfs (copied)'
|
55
|
+
|
56
|
+
rsync = Rsync.new(src_dir: source_directory, dest_dir: destination_directory)
|
57
|
+
|
58
|
+
rsync.sync! do |file_path, new_file|
|
59
|
+
puts "Full Path: #{File.join(destination_directory, file_path)}"
|
60
|
+
puts "The relative path of the new file is: #{file_path}"
|
61
|
+
if new_file
|
62
|
+
puts "This is a brand new file."
|
63
|
+
else
|
64
|
+
puts "This is an updated file."
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
|
70
|
+
License
|
71
|
+
-----------------------------
|
72
|
+
|
73
|
+
MIT-LICENSE.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rsync_wrapper"
|
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,56 @@
|
|
1
|
+
class Rsync
|
2
|
+
def initialize **opts
|
3
|
+
@dirs = {}
|
4
|
+
[:src_dir, :dest_dir].each do |k|
|
5
|
+
raise ArgumentError, "#{k} is required" unless opts[k]
|
6
|
+
@dirs[k] = opts[k]
|
7
|
+
end
|
8
|
+
@inclusions = opts[:include_extensions].map{|ext| "*.#{ext}"} || []
|
9
|
+
@exclusions = opts[:exclusions] || []
|
10
|
+
if opts[:subdirs_only]
|
11
|
+
@inclusions << '*/' # include subdirectories
|
12
|
+
@exclusions << '*'
|
13
|
+
end
|
14
|
+
@logfile = opts[:log_dir] ? File.join(opts[:log_dir], "rsync-#{SecureRandom.uuid}.log") : opts[:logfile]
|
15
|
+
end
|
16
|
+
|
17
|
+
def sync! &block
|
18
|
+
exec_rsync
|
19
|
+
parse_logfile &block
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def exec_rsync
|
25
|
+
rsync_opts = ['-ri', "--log-file '#{@logfile}'", '--size-only', '--prune-empty-dirs']
|
26
|
+
rsync_opts += @inclusions.map{|inc| "--include '#{inc}'"}
|
27
|
+
rsync_opts += @exclusions.map{|exc| "--exclude '#{exc}'"}
|
28
|
+
cmd = "rsync #{rsync_opts.join(' ')} \"#{@dirs[:src_dir]}\" \"#{@dirs[:dest_dir]}\" > /dev/null 2>&1"
|
29
|
+
`#{cmd}`
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse_logfile
|
33
|
+
base_dir = File.basename @dirs[:src_dir]
|
34
|
+
File.open(@logfile, 'r') do |f|
|
35
|
+
f.each_line do |line|
|
36
|
+
line_arr = line.split(/ /)
|
37
|
+
next unless line_arr.length > 4
|
38
|
+
#http://andreafrancia.it/2010/03/understanding-the-output-of-rsync-itemize-changes.html
|
39
|
+
changes = line_arr[3]
|
40
|
+
next unless changes.start_with?('>f') # file transfered to local
|
41
|
+
file_path = line_arr[4..-1].join(' ').strip
|
42
|
+
next unless file_path.start_with?(base_dir)
|
43
|
+
|
44
|
+
if block_given?
|
45
|
+
if changes.start_with?('>f+++')
|
46
|
+
yield file_path, true
|
47
|
+
elsif changes[3] == 's'
|
48
|
+
yield file_path, false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# File.delete @logfile
|
55
|
+
end
|
56
|
+
end
|
@@ -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 'rsync_wrapper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rsync_wrapper'
|
8
|
+
spec.version = RsyncWrapper::VERSION
|
9
|
+
spec.authors = ['Sean Huber']
|
10
|
+
spec.email = ['seanhuber@seanhuber.com']
|
11
|
+
spec.summary = 'Configurable wrapper to rsync utility'
|
12
|
+
spec.homepage = 'https://github.com/seanhuber/rsync_wrapper'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = 'exe'
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rsync_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Huber
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-23 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- seanhuber@seanhuber.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/rsync_wrapper.rb
|
72
|
+
- lib/rsync_wrapper/rsync.rb
|
73
|
+
- lib/rsync_wrapper/version.rb
|
74
|
+
- rsync_wrapper.gemspec
|
75
|
+
homepage: https://github.com/seanhuber/rsync_wrapper
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.5.1
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Configurable wrapper to rsync utility
|
99
|
+
test_files: []
|