Module: Synchrotron

Defined in:
lib/synchrotron/scanner.rb,
lib/synchrotron.rb,
lib/synchrotron/ignore.rb,
lib/synchrotron/logger.rb,
lib/synchrotron/stream.rb,
lib/synchrotron/version.rb

Overview

Not currently used.

Defined Under Namespace

Classes: Ignore, Logger, Scanner, Stream

Constant Summary collapse

APP_NAME =
'Synchrotron'
APP_VERSION =
'0.0.1'
APP_AUTHOR =
'Ryan Grove'
APP_EMAIL =
'[email protected]'
APP_URL =
'http://github.com/rgrove/synchrotron/'
'Copyright (c) 2009 Ryan Grove <[email protected]>. All ' <<
'rights reserved.'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/synchrotron.rb', line 18

def config
  @config
end

.ignoreObject (readonly)

Returns the value of attribute ignore.



18
19
20
# File 'lib/synchrotron.rb', line 18

def ignore
  @ignore
end

.logObject (readonly)

Returns the value of attribute log.



18
19
20
# File 'lib/synchrotron.rb', line 18

def log
  @log
end

.scannerObject (readonly)

Returns the value of attribute scanner.



18
19
20
# File 'lib/synchrotron.rb', line 18

def scanner
  @scanner
end

Class Method Details

.coalesce_changes(changes) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/synchrotron.rb', line 69

def coalesce_changes(changes)
  coalesced = {}

  changes.each do |path|
    next if coalesced.include?(path)

    pn = Pathname.new(path)

    coalesced[pn.to_s] = true unless catch :matched do
      pn.descend {|p| throw(:matched, true) if coalesced.include?(p.to_s) }
      false
    end
  end

  coalesced.keys.sort
end

.init(config = {}) ⇒ Object



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
# File 'lib/synchrotron.rb', line 20

def init(config = {})
  @config = {
    :dry_run      => false,
    :exclude      => [],
    :exclude_from => [],
    :local_path   => File.expand_path('.'),
    :remote_path  => nil,

    :rsync_options => [
      '--compress',
      '--human-readable',
      '--links',
      '--recursive',
      '--times',
      '--verbose'
    ],

    :rsync_path => '/usr/bin/rsync',
    :verbosity  => :info
  }.merge(config)

  @log       = Logger.new(@config[:verbosity])
  @ignore    = Ignore.new(@config[:exclude])
  @regex_rel = Regexp.new("^#{Regexp.escape(@config[:local_path].chomp('/'))}/?")

  @config[:rsync_options] << '--dry-run' if @config[:dry_run]

  local_exclude_file = File.join(@config[:local_path], '.synchrotron-exclude')
  @config[:exclude_from] << local_exclude_file if File.exist?(local_exclude_file)
  @config[:exclude_from].each {|filename| @ignore.add_file(filename) }

  @callback = proc do |stream, context, event_count, paths, marks, event_ids|
    changed = Set.new
    paths.regard_as('*')
    event_count.times {|i| changed.add(paths[i]) unless @ignore.match(paths[i]) }

    changed = coalesce_changes(changed)
    return if changed.empty?

    @log.info "Change detected"
    changed.each {|path| sync(path) }
  end

  @stream  = Stream.new(config[:local_path], @callback)

  @log.info "Local path : #{@config[:local_path]}"
  @log.info "Remote path: #{@config[:remote_path]}"
end

.monitorObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/synchrotron.rb', line 86

def monitor
  @log.info "Watching for changes"
  @stream.start

  begin
    OSX.CFRunLoopRun()
  rescue Interrupt
    @stream.release
  end
end

.relative_path(path) ⇒ Object



97
98
99
# File 'lib/synchrotron.rb', line 97

def relative_path(path)
  path.sub(@regex_rel, '')
end

.sync(path = ) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/synchrotron.rb', line 101

def sync(path = @config[:local_path])
  rsync_local   = escape_arg(path)
  rsync_options = @config[:rsync_options].join(' ')
  rsync_remote  = escape_arg(File.join(@config[:remote_path], relative_path(path)))

  # Build exclusion list.
  @config[:exclude].each {|p| rsync_options << " --exclude #{escape_arg(p)}" }
  @config[:exclude_from].each {|f| rsync_options << " --exclude-from #{escape_arg(f)}"}

  puts `#{@config[:rsync_path]} #{rsync_options} #{rsync_local} #{rsync_remote}`
  puts
end