Class: SyncFTP

Inherits:
Object
  • Object
show all
Defined in:
lib/syncftp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ SyncFTP

Create a new SyncFTP object for host

you can specify :

  • :username - default = “anonymous”

  • :password - default = nil

  • :port - default = 21

  • :logfile - default = STDOUT

  • :loglevel - default = Logger::UNKNOWN (Cool if you don’t want logs)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/syncftp.rb', line 115

def initialize(host, options = {})
  options = {
    :username => "anonymous", 
    :password => nil, 
    :logfile => STDOUT, 
    :loglevel => Logger::UNKNOWN,
    :catalog => :remote
  }.merge(options)
  @host, @port = host, options[:port]||21
  @username, @password = options[:username], options[:password]
  @catalog = options[:catalog]
  @remote_md5s = {} 
  @local_md5s = {}
  @log = Logger.new( options[:logfile] )
  @log.level = options[:loglevel]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



102
103
104
# File 'lib/syncftp.rb', line 102

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



102
103
104
# File 'lib/syncftp.rb', line 102

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



102
103
104
# File 'lib/syncftp.rb', line 102

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



102
103
104
# File 'lib/syncftp.rb', line 102

def username
  @username
end

Instance Method Details

#catalogFileNameObject

:nodoc



193
194
# File 'lib/syncftp.rb', line 193

def catalogFileName #:nodoc
end

#getCatalogObject

:nodoc



187
188
# File 'lib/syncftp.rb', line 187

def getCatalog #:nodoc
end

#saveCatalogObject

:nodoc



190
191
# File 'lib/syncftp.rb', line 190

def saveCatalog #:nodoc
end

#sync(options = {}) ⇒ Object

Sync local to remote

you can specify :

  • :local : the local directory (default = “.”)

  • :remote : the remote directory (default = “.”)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/syncftp.rb', line 140

def sync( options = {} )
  options = { :local => ".", :remote => "." , :passive => false}.merge( options )
  local, remote , passive = options[:local], options[:remote], options[:passive]
  
  tmpname = tmpfilename
  connect do |ftp|
    ftp.passive = passive
    # Read remote .syncftp
    begin
      ftp.gettextfile( remote+"/"+".syncftp", tmpname )
      @remote_md5s = YAML.load( File.open( tmpname ).read )
    rescue Net::FTPPermError => e
      raise Net::FTPPermError, e.message, caller if ftp.remote_file_exist?( remote+"/"+".syncftp" )
    end
    
    # Do the job Bob !
    send_dir( ftp, local, remote )
    
    # Write new .syncftp
    File.open( tmpname, 'w' ) do |out|
      YAML.dump( @local_md5s, out )
    end
    
    # Delete files
    @delete_dirs = []
    @delete_files = []
    @remote_md5s.keys.clone.delete_if{ |f| @local_md5s.keys.include?(f) }.each do |f|
      if @remote_md5s[f] == "*"
        @delete_dirs << f
      else
        @delete_files << f
      end
    end
    @delete_files.each do |f|
      @log.info "Delete ftp://#{@host}:#{@port}/#{f}"
      ftp.delete( f )
    end      
    @delete_dirs.each do |f|
      @log.info "Delete ftp://#{@host}:#{@port}/#{f}"
      ftp.delete( f )
    end      
    
    ftp.puttextfile( tmpname, remote+"/"+".syncftp" )
  end
  File.delete( tmpname )
end