Class: SyncFTP
- Inherits:
-
Object
- Object
- SyncFTP
- Defined in:
- lib/syncftp.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#catalogFileName ⇒ Object
:nodoc.
-
#getCatalog ⇒ Object
:nodoc.
-
#initialize(host, options = {}) ⇒ SyncFTP
constructor
Create a new SyncFTP object for
host
. -
#saveCatalog ⇒ Object
:nodoc.
-
#sync(options = {}) ⇒ Object
Sync local to remote.
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, = {}) = { :username => "anonymous", :password => nil, :logfile => STDOUT, :loglevel => Logger::UNKNOWN, :catalog => :remote }.merge() @host, @port = host, [:port]||21 @username, @password = [:username], [:password] @catalog = [:catalog] @remote_md5s = {} @local_md5s = {} @log = Logger.new( [:logfile] ) @log.level = [:loglevel] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
102 103 104 |
# File 'lib/syncftp.rb', line 102 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
102 103 104 |
# File 'lib/syncftp.rb', line 102 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
102 103 104 |
# File 'lib/syncftp.rb', line 102 def port @port end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
102 103 104 |
# File 'lib/syncftp.rb', line 102 def username @username end |
Instance Method Details
#catalogFileName ⇒ Object
:nodoc
193 194 |
# File 'lib/syncftp.rb', line 193 def catalogFileName #:nodoc end |
#getCatalog ⇒ Object
:nodoc
187 188 |
# File 'lib/syncftp.rb', line 187 def getCatalog #:nodoc end |
#saveCatalog ⇒ Object
: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( = {} ) = { :local => ".", :remote => "." , :passive => false}.merge( ) local, remote , passive = [:local], [:remote], [: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., 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 |