queue_sync 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: af37da4a62e03a4d4e8f745e119663ba8bc07f7e
4
+ data.tar.gz: 23f4ac642bb878cd614a27eb03e2e7e6ef671eb7
5
+ SHA512:
6
+ metadata.gz: 63e3b403218ba1a47d4ec06e51b7c5aebb0aebe7c969868f1d5d02464ebfb97fb1aaf2b474b21c6e3b28b6aac904378351157295dfb2fb8f8f3f0b7701d81ffc
7
+ data.tar.gz: a4ded6b2800ac52d26b4f81e92b0c489d133da8415172d36b9a895b974b64bc3fb0060df3f0a944141a22edf72f74943565b531b0e22619545cb71329e43c627
data/.gitignore ADDED
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in queue_sync.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Karthik T
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,56 @@
1
+ # QueueSync
2
+
3
+ This is a script that was specifically built to transfer video files from my Macbook pro to my Android phone where they are consumed. This is a restricted sync in which new files are pushed only from source to consumer, and deletions are only propogated from consumer to source, thus becoming a queue.
4
+
5
+ This also means that currently this only works for local source, and ssh remote consumer.
6
+
7
+ Currently this script doesnt delete consumed files, it just moves them into a `.deleted` folder. At some point, this folder might get cleared on the next run of the script, giving you a little safety to catch accidental deletions.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'queue_sync'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install queue_sync
24
+
25
+ ## Usage
26
+
27
+ The script can be used as follows
28
+
29
+ `queue_sync <src_path> <dest_host> <dest_port> <dest_path>`
30
+
31
+ Where the `src_path` and `dest_path` are paths to the folder to be synced on the source and consumer respectively, and `dest_host` and `dest_port` are the host to ssh to and the port to use for connection.
32
+
33
+ I keep a wrapper which hardcodes all the parameters which might stay constant and just reads the `dest_host` from the console since that changes often. This wrapper then calls `queue_sync` appropriately.
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Development
42
+
43
+ 1. Clean up the code into modular components
44
+ 2. Add some tests
45
+ 3. Allow for non ssh consumers and other usability fixes
46
+ 4. Implement actual deletion again
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ktaragorn/queue_sync.
51
+
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "queue_sync"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/queue_sync ADDED
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env ruby
2
+ src_path, dest_host, dest_port, dest_path = ARGV
3
+ port ||= 22
4
+ require "ostruct"
5
+ require 'fileutils'
6
+
7
+ class LastSyncList
8
+ def initialize src_path
9
+ @file = File.join(src_path, ".last_sync_list")
10
+ end
11
+
12
+ def read
13
+ File.readlines(@file).map(&:chomp)
14
+ rescue Errno::ENOENT
15
+ []
16
+ end
17
+
18
+ def write list
19
+ File.write(@file, Array(list).join("\n"))
20
+ end
21
+ end
22
+
23
+ class FileLister
24
+ attr_reader :dir
25
+ def initialize dir
26
+ @dir = File.expand_path(dir)
27
+ end
28
+ def list
29
+ files = _list
30
+ post_process files
31
+ end
32
+
33
+ def file_list_cmd
34
+ #ignore dot files/folders
35
+ "cd \"#{dir}\" && find . -type f"
36
+ end
37
+
38
+ def post_process list_of_files
39
+ is_hidden = ->(f){f.match(/\/\..*/)}
40
+ list_of_files.split("\n").map{|file| file.gsub(dir, "./").gsub(/^.\//, "")}.reject(&is_hidden)
41
+ end
42
+ end
43
+
44
+ class RemoteFileLister < FileLister
45
+ attr_reader :dest
46
+ def initialize dest
47
+ @dest = dest
48
+ super(dest.path)
49
+ end
50
+
51
+ def ssh_command host, port, command
52
+ ret = `ssh #{host} -p #{port} \"#{command}\"`
53
+ if $? != 0
54
+ raise "Command Failed"
55
+ end
56
+ ret
57
+ end
58
+
59
+ def _list
60
+ ssh_command dest.host, dest.port, file_list_cmd
61
+ end
62
+ end
63
+
64
+ class LocalFileLister < FileLister
65
+ def initialize src_path
66
+ super(src_path)
67
+ end
68
+
69
+ def _list
70
+ `#{file_list_cmd}`
71
+ end
72
+ end
73
+
74
+ def current_list_remote dest
75
+ RemoteFileLister.new(dest).list
76
+ end
77
+
78
+ def delete_remote_deleted_files src_path, list
79
+ #TODO once tested, to add a stage here, before the next stage, to wipe .deleted
80
+ del_folder =File.join(src_path, ".deleted")
81
+ Dir.mkdir del_folder unless File.directory?(del_folder)
82
+ list.each do |file|
83
+ puts "Deleting #{file}"
84
+ #2 stage deletion - to prevent accidental deletion
85
+ #perhaps shift to .deleted folder and delete in 2nd pass
86
+
87
+ src = File.join(src_path, file)
88
+ target = File.join(del_folder, file)
89
+ FileUtils.mkdir_p(File.dirname(target))
90
+ File.rename(src, target)
91
+ end
92
+ end
93
+
94
+ def rsync_push_new_files src, dest
95
+ # ignore existing to optimize for android transfer where i think modification time or something is not being set
96
+ # Files are not expected to be modified on the remote, so this should suffice
97
+ # -W to optimize for large files
98
+ # exclude dot files and folders -http://askubuntu.com/a/482920/92812
99
+ system("rsync -rvzWh --exclude \".*\" --exclude \".*/\" --ignore-existing --progress -e \"ssh -p #{dest.port}\" \"#{src}\" \"#{dest.host}:#{dest.path}\"")
100
+ end
101
+
102
+ def current_list_local path
103
+ LocalFileLister.new(path).list
104
+ end
105
+
106
+ dest = OpenStruct.new(host: dest_host, port: dest_port, path: dest_path)
107
+ lsl = LastSyncList.new(src_path)
108
+
109
+ list_at_last_sync = lsl.read
110
+
111
+ delete_remote_deleted_files(src_path, list_at_last_sync - current_list_remote(dest))
112
+ rsync_push_new_files(src_path, dest)
113
+
114
+ lsl.write(current_list_local(src_path))
@@ -0,0 +1,3 @@
1
+ module QueueSync
2
+ VERSION = "0.1.0"
3
+ end
data/lib/queue_sync.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "queue_sync/version"
2
+
3
+ module QueueSync
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'queue_sync/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "queue_sync"
8
+ spec.version = QueueSync::VERSION
9
+ spec.authors = ["Karthik T"]
10
+ spec.email = ["karthikt.holmes+github@gmail.com"]
11
+
12
+ spec.summary = %q{This is a script to sync files from a source to a consumer of said files}
13
+ spec.description = %q{This is a script that was specifically built to transfer video files from my Macbook pro to my Android phone where they are consumed. This is a restricted sync in which new files are pushed only from source to consumer, and deletions are only propogated from consumer to source, thus becoming a queue.}
14
+ spec.homepage = "https://github.com/ktaragorn/queue_sync"
15
+ spec.license = "MIT"
16
+
17
+ # # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: queue_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karthik T
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-01 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: This is a script that was specifically built to transfer video files
42
+ from my Macbook pro to my Android phone where they are consumed. This is a restricted
43
+ sync in which new files are pushed only from source to consumer, and deletions are
44
+ only propogated from consumer to source, thus becoming a queue.
45
+ email:
46
+ - karthikt.holmes+github@gmail.com
47
+ executables:
48
+ - queue_sync
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - ".gitignore"
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/console
58
+ - bin/setup
59
+ - exe/queue_sync
60
+ - lib/queue_sync.rb
61
+ - lib/queue_sync/version.rb
62
+ - queue_sync.gemspec
63
+ homepage: https://github.com/ktaragorn/queue_sync
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.5.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: This is a script to sync files from a source to a consumer of said files
87
+ test_files: []