260 lines
5.3 KiB
Ruby
Executable File
260 lines
5.3 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require 'bundler/setup'
|
|
|
|
require 'Qt4'
|
|
require 'thread'
|
|
require 'atomic'
|
|
|
|
ENV['PROFILE'] ||= 'default'
|
|
|
|
class UnisonProfile
|
|
def initialize(which = ENV['PROFILE'])
|
|
@which = which
|
|
end
|
|
|
|
def local_root
|
|
roots.find { |root| root[%r{^/}] }
|
|
end
|
|
|
|
def roots
|
|
@roots ||= lines.find_all { |line| line[%r{^root}] }.collect { |line| value_of(line) }
|
|
end
|
|
|
|
def lines
|
|
return @lines if @lines
|
|
|
|
@lines = File.readlines(File.expand_path("~/.unison/#{@which}.prf"))
|
|
|
|
includes = []
|
|
|
|
@lines.each do |line|
|
|
if file = line[%r{^include (.*)}, 1]
|
|
includes += File.readlines(File.expand_path("~/.unison/#{file}"))
|
|
end
|
|
end
|
|
|
|
@lines += includes
|
|
end
|
|
|
|
def paths
|
|
@paths ||= lines.find_all { |line| line[%r{^path}] }.collect { |line| value_of(line) }
|
|
end
|
|
|
|
def value_of(line)
|
|
line[%r{=(.*)$}, 1].strip
|
|
end
|
|
end
|
|
|
|
class UnisonWatcher < Qt::Application
|
|
TRANSFER_LOG = '~/unison.log'
|
|
SYNC_CHECK_COUNT = 600
|
|
SYNC_CHECK_TIME = 0.1
|
|
|
|
def initialize(*args)
|
|
super(*args)
|
|
|
|
@queue = Atomic.new([])
|
|
@sync_now = false
|
|
@exiting = false
|
|
@active = true
|
|
end
|
|
|
|
def <<(dirs)
|
|
@queue.update { |q| q += [ dirs ].flatten ; q }
|
|
end
|
|
|
|
def profile
|
|
@profile ||= UnisonProfile.new
|
|
end
|
|
|
|
def watch
|
|
require 'rbconfig'
|
|
|
|
watcher = Thread.new do
|
|
while !Thread.current[:app]; sleep 0.1; end
|
|
|
|
begin
|
|
@watch = nil
|
|
|
|
case RbConfig::CONFIG['host_os']
|
|
when /darwin/
|
|
require 'rb-fsevent'
|
|
@watch = FSEvent.new
|
|
@watch.watch Thread.current[:paths], :latency => 0.1 do |directories|
|
|
Thread.current[:app] << directories
|
|
end
|
|
when /linux/
|
|
require 'rb-inotify'
|
|
@watch = INotify::Notifier.new
|
|
Thread.current[:paths].each do |path|
|
|
@watch.watch path, :recursive, :modify, :create, :delete do |event|
|
|
Thread.current[:app] << event.absolute_name
|
|
end
|
|
end
|
|
end
|
|
|
|
@watch.run
|
|
rescue => e
|
|
puts e.message
|
|
puts e.backtrace.join("\n")
|
|
exit
|
|
end
|
|
end
|
|
|
|
watcher[:paths] = profile.paths.collect { |path| File.join(profile.local_root, path) }
|
|
watcher[:app] = self
|
|
end
|
|
|
|
def update_status(status)
|
|
new_status = Qt::Action.new(status, @menu)
|
|
new_status.enabled = false
|
|
@menu.insertAction(@active_status, new_status)
|
|
@menu.removeAction(@status)
|
|
|
|
@status = new_status
|
|
end
|
|
|
|
def menu
|
|
@menu = Qt::Menu.new
|
|
|
|
@current_text = 'Unison watch idle.'
|
|
|
|
@status = Qt::Action.new(@status_text, @menu)
|
|
@status.enabled = false
|
|
|
|
sync_now = Qt::Action.new("Sync now", @menu)
|
|
sync_now.connect(SIGNAL :triggered) { @sync_now = true }
|
|
|
|
@active_status = Qt::Action.new("Pause syncing", @menu)
|
|
@active_status.connect(SIGNAL :triggered) { toggle_status }
|
|
|
|
@log = Qt::Action.new("View transfer log", @menu)
|
|
@log.connect(SIGNAL :triggered) { system %{open #{TRANSFER_LOG}} }
|
|
|
|
quit = Qt::Action.new("Quit", @menu)
|
|
quit.connect(SIGNAL :triggered) { @exiting = true }
|
|
|
|
@menu.addAction @active_status
|
|
@menu.addAction sync_now
|
|
@menu.addSeparator
|
|
@menu.addAction @log
|
|
@menu.addAction quit
|
|
|
|
@current_text = 'Unison watch idle.'
|
|
update_status @current_text
|
|
|
|
@menu
|
|
end
|
|
|
|
def update_ui
|
|
|
|
if @current_icon != @prior_icon
|
|
if !@icons[@current_icon]
|
|
@icons[@current_icon] = Qt::Icon.new(File.expand_path("../../assets/#{@current_icon}.png", __FILE__))
|
|
end
|
|
|
|
@icon.icon = @icons[@current_icon]
|
|
@icon.toolTip = "Unison Agent\nUsing #{ENV['PROFILE']} profile"
|
|
if !@prior_icon
|
|
@icon.show
|
|
end
|
|
|
|
@prior_icon = @current_icon
|
|
end
|
|
|
|
if @current_text!= @prior_text
|
|
update_status @current_text
|
|
|
|
@prior_text = @current_text
|
|
end
|
|
|
|
processEvents
|
|
end
|
|
|
|
def ui
|
|
@icon = Qt::SystemTrayIcon.new
|
|
@icon.contextMenu = menu
|
|
|
|
toggle_status true
|
|
@prior_icon = nil
|
|
@prior_text = nil
|
|
|
|
s = nil
|
|
|
|
@icons = {}
|
|
|
|
@remote_sync_check = SYNC_CHECK_COUNT
|
|
|
|
while !@exiting
|
|
check
|
|
|
|
update_ui
|
|
|
|
sleep SYNC_CHECK_TIME
|
|
end
|
|
end
|
|
|
|
def start
|
|
watch
|
|
ui
|
|
end
|
|
|
|
def check
|
|
begin
|
|
if @active && (@queue.value.length > 0 || @remote_sync_check == 0 || @sync_now)
|
|
dir = nil
|
|
|
|
@current_text = "Syncing..."
|
|
|
|
@done = false
|
|
|
|
runner = Thread.new do
|
|
system %{bash -c 'unison -log -logfile #{TRANSFER_LOG} -batch #{ENV['PROFILE']}'}
|
|
@done = true
|
|
end
|
|
|
|
while !@done
|
|
@current_icon = 'working-1'
|
|
update_ui
|
|
sleep 0.25
|
|
|
|
@current_icon = 'working-2'
|
|
sleep 0.25
|
|
update_ui
|
|
end
|
|
|
|
@current_icon = 'idle'
|
|
|
|
@remote_sync_check = SYNC_CHECK_COUNT
|
|
@sync_now = false
|
|
@queue.update { [] }
|
|
end
|
|
rescue => e
|
|
puts e.message
|
|
puts e.backtrace.join("\n")
|
|
exit
|
|
end
|
|
|
|
@remote_sync_check -= 1
|
|
|
|
if @active
|
|
@current_text = "Next check in #{sprintf("%.0d", SYNC_CHECK_TIME * @remote_sync_check)} secs."
|
|
else
|
|
@current_text = "Syncing paused."
|
|
|
|
@remote_sync_check = SYNC_CHECK_COUNT
|
|
end
|
|
end
|
|
|
|
def toggle_status(set = nil)
|
|
@active = set || !@active
|
|
|
|
@active_status.text = @active ? "Pause syncing" : "Resume syncing"
|
|
@current_icon = @active ? 'idle' : 'paused'
|
|
end
|
|
end
|
|
|
|
UnisonWatcher.new(ARGV).start
|
|
|