-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync2.rb
79 lines (64 loc) · 1.89 KB
/
sync2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'rubygems'
require 'net/scp'
require 'ruby-growl'
require 'listen'
require 'yaml'
LOCK_FILE = '.lock4autosync'
if File.exist? LOCK_FILE
p 'sync process is already running! This process will exit.'
exit
end
YAML::ENGINE.yamler = 'syck'
CONFIG = YAML::load(File.open("#{File.dirname(__FILE__)}/config.yml"))
$host = CONFIG['host']
$username = CONFIG['username']
$password = CONFIG['password']
$src_dir = CONFIG['src_dir']
$dest_dir = CONFIG['dest_dir']
$scp = Net::SCP.start($host, $username, :password => $password)
$growl = nil
#if Object.const_defined? :Growl
#class UUID
#def generate() 4 end
#end
#$growl = Growl::GNTP.new 'localhost', 'autosync4mac'
#$growl.uuid = UUID.new
#$growl.add_notification "autosync4mac", "Auto Sync 4 Mac Notification", "PNG", true
#$growl.register
#$growl.notify "autosync4mac", "welcome", "auto sync 4 mac is running now."
#end
def notify(type, msg)
if $growl
$growl.notify "autosync4mac", type, msg, 2, true
end
end
def upload(source, destination, recursive=true)
begin
$scp.upload!(source, destination, :recursive => recursive)
p "#{source} modified, synced to server!"
rescue
$scp = Net::SCP.start($host, $username, :password => $password)
$scp.upload!(source, destination, :recursive => recursive)
p "#{source} modified, synced to server!"
end
begin
#config growl listen to network connection
notify "File sync", "#{source} modified, synced to server!"
rescue
p $!
end
end
File.open(LOCK_FILE, 'w').close
Kernel.at_exit do
p "will delete lock file, exit."
File.delete LOCK_FILE
notify "error", "sync process exited!"
end
p "start listen for changes..."
Listen.to($src_dir) do |modified, added, removed|
modified.concat(added).each do |candidate|
unless /\.(git|svn|hg)/ =~ candidate
upload candidate, $dest_dir + candidate.split($src_dir)[1], File.directory?(candidate)
end
end
end