quicksync 0.0.7 → 0.0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,20 @@
1
1
  $default_options =
2
2
  {
3
- :run_method => :execute,
3
+ :run_method => :generate,
4
+ :src => "/users/shaun/Dropbox/dtg/development/mgm-sugarcrm/custom",
5
+ :dest => "/inetpub/wwwroot/SugarCRM-main/dev/current/custom",
6
+ :host => "dev.crm.mgmwireless.com",
7
+ :user => "sugarcrm",
8
+ :exclude => %w(.DS_Store .log),
9
+ :include => %w(),
10
+ :copy_options => %w(recursive stats compress),
4
11
  :settings => {
5
12
  :ssh_app => "rsh",
6
13
  :rsync_app => "rsync",
7
14
  :log_file => "",
8
- :verbose => true,
15
+ :verbose => false,
9
16
  :silent => false,
10
17
  },
11
- :from => {
12
- :host => "local",
13
- :user => "",
14
- :dir => "/users/shaun/Dropbox/dtg/development/mgm-sugarcrm/data",
15
- },
16
- :to => {
17
- :host => "dev.crm.mgmwireless.com",
18
- :user => "sugarcrm",
19
- :dir => "/inetpub/wwwroot/SugarCRM-main/dev/current/data",
20
- },
21
- :exclude => %w(.DS_Store .log .csv mytest.txt),
22
- :include => %w(),
23
- :copy_options => %w(progress stats times perms compress dry-run),
24
18
  }
25
19
 
20
+
@@ -0,0 +1,25 @@
1
+ :run_method: :generate
2
+ :src: /users/shaun/Dropbox/dtg/development/mgm-sugarcrm/custom
3
+ :dest: /inetpub/wwwroot/SugarCRM-main/dev/current/custom
4
+ :host: dev.crm.mgmwireless.com
5
+ :user: sugarcrm
6
+ :exclude:
7
+ - DS_Store
8
+ - .log
9
+ :include:
10
+ :copy_options:
11
+ - recursive
12
+ - stat
13
+ - compress
14
+ :settings:
15
+ :ssh_app: rsh
16
+ :rsync_app: rsync
17
+ :log_file: ~
18
+ :verbose: false
19
+ :silent: false
20
+ :sync_types:
21
+ :pull_from: :local
22
+ :push_to: :local
23
+ :copy_local: :local
24
+ :copy_remote: :remote
25
+
data/lib/core_ext.rb CHANGED
@@ -10,8 +10,15 @@ String.class_eval do
10
10
  return false if self == false || self.blank? || self =~ (/\A(false|f|no|n|0)\Z/i)
11
11
  raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
12
12
  end
13
-
14
-
13
+
14
+ end
15
+
16
+ Hash.class_eval do
17
+ def symbolize_keys
18
+ self.keys.each do |key|
19
+ self[(key.to_sym rescue key) || key] = self.delete(key)
20
+ end
21
+ end
15
22
  end
16
23
 
17
24
  def blank?(var)
data/lib/quicksync.rb CHANGED
@@ -5,15 +5,27 @@ require 'quicksync/rsync'
5
5
  require 'quicksync/cli'
6
6
  require 'etc'
7
7
 
8
- search_paths = [Etc.getpwuid.dir, File.join(Pathname.new(File.dirname(__FILE__)).parent,"config")]
9
- file_name = ".quicksync_config"
10
- config_file = ""
11
- search_paths.each { |p|
12
- if File.exists?("#{p}/#{file_name}")
13
- config_file = "#{p}/#{file_name}"
14
- break
8
+ module QuickSync
9
+
10
+ def self.Config
11
+ @config ||= QuickSync.LoadConfig
15
12
  end
16
- }
17
-
13
+
14
+ def self.LoadConfig
15
+
16
+ home_path = Etc.getpwuid.dir
17
+ gem_path = File.join(Pathname.new(File.dirname(__FILE__)).parent,"config")
18
+ search_paths = [home_path, gem_path]
19
+ yaml_filename = "quicksync.yaml"
20
+ yaml_file= "#{gem_path}/#{yaml_filename}"
21
+ search_paths.each { |p|
22
+ file = "#{p}/#{yaml_filename}"
23
+ if File.exists?(file)
24
+ yaml_file = file
25
+ break
26
+ end
27
+ }
28
+ return YAML::load( File.open( yaml_file ) )
29
+ end
30
+ end
18
31
 
19
- load "#{config_file}"
@@ -1,159 +1,141 @@
1
-
2
-
3
1
  module QuickSync
4
2
  class RSync
5
3
 
6
- attr_accessor :settings, :from, :to, :exclude, :include, :copy_options, :run_method
7
- attr_reader :default_options, :logger
8
-
4
+ attr_accessor :src, :dest, :user, :host, :exclude, :include, :copy_options, :settings, :run_method, :run_on, :sync_types
5
+ attr_reader :default_options, :logger, :sync_type, :config
9
6
  def initialize
10
7
  @logger = QuickSync.Logger
11
8
  @logger.level = QuickSync::Logger::INFO
12
- @default_options = $default_options
13
-
9
+ @default_options = QuickSync.Config
10
+ @sync_types = @default_options[:sync_types]
11
+
14
12
  end
15
13
 
16
- def copy_options_to_s
17
- return copy_options.map{ |o| "--#{o}"}.join(' ')
14
+ def set_sync_type(sync_type)
15
+ puts "sync_type value set to #{sync_type.to_sym}"
16
+ @sync_type = sync_type.to_sym
18
17
  end
19
18
 
20
- def include_to_s
21
- if include.any?
22
- return include.map { |i| "--include=\"#{i}\"" }.join(' ')
23
- end
19
+
20
+ def src_fully_qualified
21
+ return src
24
22
  end
25
-
26
- def exclude_to_s
27
- if exclude.any?
28
- return exclude.map { |e| "--exclude=\"#{e}\"" }.join(' ')
23
+
24
+ def dest_fully_qualified
25
+ if sync_type == :pull_from || sync_type == :push_to
26
+ return "#{user}@#{host}:#{dest}"
27
+ else
28
+ return "#{dest}"
29
29
  end
30
30
  end
31
-
32
- def default_options
33
- @default_options
31
+
32
+ def run_on
33
+ # logger.debug "run_on function called, :sync_type=#{sync_type}, sync_types[sync_type]=#{sync_types[sync_type]}"
34
+ return sync_types[sync_type]
34
35
  end
35
36
 
36
- def parse_options(from_v,to_v,options)
37
-
38
- if from_v.nil? || from_v.empty?
39
- raise ArgumentError, ":from can not be empty"
40
- end
37
+ def pull_from(src,dest,options={})
38
+ set_sync_type(__method__)
39
+ return sync(src,dest,options)
40
+ end
41
41
 
42
- if to_v.nil? || to_v.empty?
43
- raise ArgumentError, ":to can not be empty"
44
- end
45
-
46
- logger.debug "QuickSync.parse_options: from class=#{from_v.class}"
42
+ def push_to(src,dest,options={})
43
+ set_sync_type(__method__)
44
+ return sync(src,dest,options)
45
+ end
47
46
 
48
- @from = from_v == String ? default_options[:from].merge({:dir=>from_v}): default_options[:from].merge(from_v)
49
- @to = to_v == String ? default_options[:to].merge({:dir=>to_v}): default_options[:to].merge(to_v)
50
- @exclude = options.length > 0 && ! options[:exclude].nil? ? options[:exclude]: default_options[:exclude]
51
- @include = options.length > 0 && ! options[:include].nil? ? options[:include]: default_options[:include]
52
- @copy_options = options.length > 0 && ! options[:copy_options].nil? ? default_options[:copy_options].concat(options[:copy_options]).uniq!: default_options[:copy_options]
53
- @settings = options.length > 0 && ! options[:settings].nil? ? default_options[:settings].merge(options[:settings]): default_options[:settings]
54
- @run_method = default_options[:run_method]
55
-
56
- logger.debug "QuickSync.parse_options:"
57
- logger.debug " from=#{from}"
58
- logger.debug " to=#{to}"
59
- logger.debug " exclude=#{exclude}"
60
- logger.debug " include=#{include}"
61
- logger.debug " copy_options=#{copy_options}"
62
- logger.debug " settings=#{settings}"
47
+ def copy_local(src,dest,options={})
48
+ set_sync_type(__method__)
49
+ return sync(src,dest,options)
50
+ end
63
51
 
52
+ def copy_remote(src,dest,options={})
53
+ set_sync_type(__method__)
54
+ return sync(src,dest,options)
64
55
  end
65
-
66
- def pull_from(from,to,options={})
67
- parse_options(from,to,options)
68
- # if from[:dir] does not exist then abort as there is nothing to do
56
+
57
+ def sync(src,dest,options={})
58
+ parse_options(src,dest,options)
69
59
  cmd = generate_cmd
70
- logger.info "RSync.pull_from: run_method=#{run_method}"
60
+ logger.info "quicksync command:\n #{cmd}"
71
61
  if run_method == :execute
72
- # run the actual command before returning it
73
62
  logger.info "RSync.pull_from: about to execute command"
74
- system(cmd)
75
-
63
+ # system(cmd)
76
64
  end
77
- logger.info "quicksync command:\n #{cmd}"
65
+
78
66
  return cmd
79
67
  end
80
-
81
- def push_to(from,to,options={})
82
- parse_options(from,to,options)
83
- # if from[:dir] does not exist then abort as there is nothing to do
84
- cmd = generate_cmd
85
- logger.info "RSync.push_to: run_method=#{run_method}"
86
- if run_method == :execute
87
- # run the actual command before returning it
88
- logger.info "RSync.push_to: about to execute command"
89
- system(cmd)
90
-
91
- end
92
- logger.info "quicksync command:\n #{cmd}"
93
- return cmd
68
+
69
+ def to_str
70
+ out = "QuickSync.to_str:\n"
71
+ out += " :src=#{src}\n"
72
+ out += " :dest=#{dest}\n"
73
+ out += " :dest_fully_qualified=#{dest_fully_qualified}\n"
74
+ out += " :host=#{host}\n"
75
+ out += " :user=#{user}\n"
76
+ out += " :exclude=#{exclude.join(',')}\n" if ! exclude.nil?
77
+ out += " :include=#{include.join(',')}\n" if ! include.nil?
78
+ out += " :copy_options=#{copy_options.join(" --")}\n"
79
+ out += " :settings=#{settings}\n"
80
+ out += " :sync_type=#{sync_type}\n"
81
+ out += " :sync_types=#{sync_types}\n"
82
+ out += " :run_on=#{run_on}\n"
83
+ out += " :run_method=#{run_method}\n"
84
+ return out
94
85
  end
95
-
96
- def run_on
97
- if is_local(from[:host]) && is_local(to[:host])
98
- return :local
99
- elsif is_local(from[:host]) && is_remote(to[:host])
100
- return :local
101
- elsif is_remote(from[:host]) && is_remote(to[:host])
102
- return :remote
103
- elsif is_remote(from[:host]) && is_local(to[:host])
104
- return :remote
105
- else
106
- return :local
86
+
87
+ def parse_options(src,dest,options)
88
+
89
+ if src.nil? || src.empty?
90
+ raise ArgumentError, ":src can not be empty"
107
91
  end
108
- end
109
-
110
- def is_local(the_host)
111
- if ! the_host.nil? && ! the_host.empty? && the_host.include?("local")
112
- return true
113
- else
114
- return false
92
+
93
+ if dest.nil? || dest.empty?
94
+ raise ArgumentError, ":dest can not be empty"
115
95
  end
96
+
97
+ @src = ( ! src.nil? && ! src.empty? ) ? src: default_options[:src]
98
+ @dest = ( ! dest.nil? && ! dest.empty? ) ? dest: default_options[:dest]
99
+ @host = ( ! host.nil? && ! host.empty? ) ? host: default_options[:host]
100
+ @user = ( ! user.nil? && ! user.empty? ) ? user: default_options[:user]
101
+ @exclude = options.length > 0 && ! options[:exclude].nil? ? options[:exclude]: default_options[:exclude]
102
+ @include = options.length > 0 && ! options[:include].nil? ? options[:include]: default_options[:include]
103
+ @copy_options = options.length > 0 && ! options[:copy_options].nil? ? options[:copy_options]: default_options[:copy_options]
104
+ @settings = options.length > 0 && ! options[:settings].nil? ? default_options[:settings].merge(options[:settings]): default_options[:settings]
105
+ @run_method = options.length > 0 && ! options[:run_method].nil? ? options[:run_method]: default_options[:run_method]
106
+
107
+ logger.important " parse_options: #{self.to_str}"
108
+
116
109
  end
117
110
 
118
- def is_remote(the_host)
119
- if ! the_host.nil? && ! the_host.empty? && ! the_host.include?("local")
120
- return true
121
- else
122
- return false
123
- end
111
+ def copy_options_to_s
112
+ return copy_options.map{ |o| "--#{o}"}.join(' ')
124
113
  end
125
-
126
- def from_to_s
127
-
128
- logger.debug "RSync.from_to_s: from=#{from}"
129
- if ! from[:host].nil? && ! from[:host].empty? && ! from[:host].include?("local")
130
- return from[:user]+"@"+from[:host] + ":" + from[:dir]
131
- else
132
- return from[:dir]
114
+
115
+ def include_to_s
116
+ if include.any?
117
+ return include.map { |i| "--include=\"#{i}\"" }.join(' ')
133
118
  end
134
119
  end
135
-
136
- def to_to_s
137
- val = to[:dir]
138
- if ! to[:host].nil? && ! to[:host].empty? && ! to[:host].include?("local")
139
- val = to[:user]+"@"+to[:host] + ":" + to[:dir]
140
- else
141
- return to[:dir]
120
+
121
+ def exclude_to_s
122
+ if exclude.any?
123
+ return exclude.map { |e| "--exclude=\"#{e}\"" }.join(' ')
142
124
  end
143
125
  end
144
126
 
145
-
146
127
  def generate_cmd
147
128
  logger.debug "QuickSync.generated_cmd: method called"
148
-
149
129
  cmd = "#{settings[:rsync_app]} #{copy_options_to_s}"
150
130
  cmd << " #{include_to_s}" if !include.nil? && include.any?
151
131
  cmd << " #{exclude_to_s}" if !exclude.nil? && exclude.any?
152
- cmd << " #{from_to_s}/ #{to_to_s}"
153
-
132
+ cmd << " #{src_fully_qualified}/ #{dest_fully_qualified}"
133
+
154
134
  return cmd
155
135
  end
156
136
 
137
+
138
+
157
139
  end
158
140
 
159
141
  end
@@ -1,3 +1,3 @@
1
1
  module QuickSync
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.7.1"
3
3
  end
@@ -2,31 +2,55 @@ require 'helper'
2
2
  require 'quicksync'
3
3
 
4
4
  class TestQuickSync < Test::Unit::TestCase
5
+
6
+ attr_accessor :src, :dest, :user, :host, :exclude, :include, :copy_options, :settings, :run_method, :copy_method, :run_on
7
+
5
8
  def setup
6
- $logger = QuickSync.Logger
7
- $logger.level = QuickSync::Logger::INFO
9
+ @logger = QuickSync.Logger
10
+ @logger.level = QuickSync::Logger::INFO
11
+ @config = QuickSync.Config
12
+ @quicksync = QuickSync::RSync.new
13
+ @src = @config[:src]
14
+ @dest = @config[:dest]
15
+ @host = @config[:host]
16
+ @user = @config[:user]
17
+ @options = @config
8
18
 
9
19
  end
10
20
 
11
21
  def teardown
12
22
 
13
23
  end
14
-
15
- def test_rsync
24
+
25
+
26
+ def test_config
16
27
 
17
28
  assert_nothing_raised do
18
-
19
- options = $default_options
20
- qs = QuickSync::RSync.new
29
+ @config = QuickSync.Config
30
+ end
31
+ end
21
32
 
22
- from = options[:from]
23
- to = options[:to]
24
- cmd = qs.pull_from(from,to,options)
33
+ def test_rsync
25
34
 
35
+ puts "test_rsync: testing pull_from"
36
+ assert_nothing_raised do
37
+ cmd = @quicksync.pull_from(@src,@dest,@options)
38
+ end
39
+ puts "test_rsync: testing push_to"
40
+ assert_nothing_raised do
41
+ cmd = @quicksync.push_to(@src,@dest,@options)
42
+ end
43
+ puts "test_rsync: testing copy_local"
44
+ assert_nothing_raised do
45
+ cmd = @quicksync.copy_local(@src,@dest,@options)
46
+ end
47
+ puts "test_rsync: testing copy_remote"
48
+ assert_nothing_raised do
49
+ cmd = @quicksync.copy_remote(@src,@dest,@options)
26
50
  end
27
51
 
28
-
29
52
 
53
+
30
54
  end
31
55
 
32
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quicksync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -205,6 +205,7 @@ files:
205
205
  - Rakefile
206
206
  - bin/quicksync
207
207
  - config/.quicksync_config
208
+ - config/quicksync.yaml
208
209
  - lib/core_ext.rb
209
210
  - lib/quicksync.rb
210
211
  - lib/quicksync/cli.rb
@@ -228,7 +229,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
228
229
  version: '0'
229
230
  segments:
230
231
  - 0
231
- hash: -1428722002811476590
232
+ hash: -2741976033233478250
232
233
  required_rubygems_version: !ruby/object:Gem::Requirement
233
234
  none: false
234
235
  requirements:
@@ -237,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
238
  version: '0'
238
239
  segments:
239
240
  - 0
240
- hash: -1428722002811476590
241
+ hash: -2741976033233478250
241
242
  requirements: []
242
243
  rubyforge_project:
243
244
  rubygems_version: 1.8.24