Class: Fig::Protocol::FTP
- Inherits:
-
Object
- Object
- Fig::Protocol::FTP
- Includes:
- Fig::Protocol, NetRCEnabled
- Defined in:
- lib/fig/protocol/ftp.rb
Overview
File transfers via FTP
Instance Method Summary collapse
-
#download(uri, path, prompt_for_login) ⇒ Object
Returns whether the file was not downloaded because the file already exists and is already up-to-date.
- #download_list(uri) ⇒ Object
-
#initialize(login) ⇒ FTP
constructor
A new instance of FTP.
-
#path_up_to_date?(uri, path, prompt_for_login) ⇒ Boolean
Determine whether we need to update something.
- #upload(local_file, uri) ⇒ Object
Constructor Details
#initialize(login) ⇒ FTP
Returns a new instance of FTP.
18 19 20 21 |
# File 'lib/fig/protocol/ftp.rb', line 18 def initialize(login) @login = login initialize_netrc end |
Instance Method Details
#download(uri, path, prompt_for_login) ⇒ Object
Returns whether the file was not downloaded because the file already exists and is already up-to-date.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fig/protocol/ftp.rb', line 60 def download(uri, path, prompt_for_login) begin ftp = Net::FTP.new(uri.host) ftp_login(ftp, uri.host, prompt_for_login) if ::File.exist?(path) && ftp.mtime(uri.path) <= ::File.mtime(path) Fig::Logging.debug "#{path} is up to date." return false else log_download(uri, path) ftp.getbinaryfile(uri.path, path, 256*1024) return true end rescue Net::FTPPermError => error Fig::Logging.debug error. raise Fig::FileNotFoundError.new error., uri rescue SocketError => error Fig::Logging.debug error. raise Fig::FileNotFoundError.new error., uri rescue Errno::ETIMEDOUT => error Fig::Logging.debug error. raise Fig::FileNotFoundError.new error., uri end end |
#download_list(uri) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/fig/protocol/ftp.rb', line 23 def download_list(uri) ftp = Net::FTP.new(uri.host) ftp_login(ftp, uri.host, :prompt_for_login) ftp.chdir(uri.path) dirs = ftp.nlst ftp.close download_ftp_list(uri, dirs) end |
#path_up_to_date?(uri, path, prompt_for_login) ⇒ Boolean
Determine whether we need to update something. Returns nil to indicate “don’t know”.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/fig/protocol/ftp.rb', line 35 def path_up_to_date?(uri, path, prompt_for_login) begin ftp = Net::FTP.new(uri.host) ftp_login(ftp, uri.host, prompt_for_login) if ftp.size(uri.path) != ::File.size(path) return false end if ftp.mtime(uri.path) <= ::File.mtime(path) return true end return false rescue Net::FTPPermError => error Fig::Logging.debug error. raise Fig::FileNotFoundError.new error., uri rescue SocketError => error Fig::Logging.debug error. raise Fig::FileNotFoundError.new error., uri end end |
#upload(local_file, uri) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fig/protocol/ftp.rb', line 85 def upload(local_file, uri) ftp_uri = Fig::URL.parse(ENV['FIG_REMOTE_URL']) ftp_root_path = ftp_uri.path ftp_root_dirs = ftp_uri.path.split('/') remote_publish_path = uri.path[0, uri.path.rindex('/')] remote_publish_dirs = remote_publish_path.split('/') # Use array subtraction to deduce which project/version folder to upload # to, i.e. [1,2,3] - [2,3,4] = [1] remote_project_dirs = remote_publish_dirs - ftp_root_dirs Net::FTP.open(uri.host) do |ftp| ftp_login(ftp, uri.host, :prompt_for_login) # Assume that the FIG_REMOTE_URL path exists. ftp.chdir(ftp_root_path) remote_project_dirs.each do |dir| # Can't automatically create parent directories, so do it manually. if ftp.nlst().index(dir).nil? ftp.mkdir(dir) ftp.chdir(dir) else ftp.chdir(dir) end end ftp.putbinaryfile(local_file) end end |