Class: Net::FTP

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.crack_file_line(aString) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/buzzville/ftp_extra.rb', line 32

def self.crack_file_line(aString)
	values = aString.scan(/(.{10}).{28}(.{13})(.*)$/).flatten
	{
		:bits => values[0],
		:date => values[1],
		:name => values[2]
	}
end

.with_connect(aHost, aUsername, aPassword, aDir = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/buzzville/ftp_extra.rb', line 24

def FTP.with_connect(aHost,aUsername,aPassword,aDir=nil)
	open(aHost,aUsername,aPassword) do |f|
		f.passive = true
		f.chdir(aDir) if aDir
		yield f
	end
end

Instance Method Details

#dir_exists?(aPath) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/buzzville/ftp_extra.rb', line 73

def dir_exists?(aPath)
	aPath = expand_dir(aPath)
	return true if aPath=='/'
	dirname = File.basename(aPath)
	parent = MiscUtils.path_parent(aPath)
	dirname!='' && nlst(parent).include?(dirname)
end

#ensure_dir(aPath, aThorough = false) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/buzzville/ftp_extra.rb', line 137

def ensure_dir(aPath,aThorough=false)
	if !aThorough
		mkdir(aPath) unless dir_exists?(aPath)
	else
		return if dir_exists?(aPath)
		path = expand_dir(aPath)
		hi_existing = highest_existing(path)
		# path to create under hi_existing
		to_create = MiscUtils::path_debase(path,hi_existing)
		parts = MiscUtils::path_parts(to_create)
		curr_path = hi_existing
		
		parts.each do |part|
			curr_path = File.join(curr_path,part)
			mkdir(curr_path)
		end
	end
end

#expand_dir(aPath, aBase = nil) ⇒ Object



68
69
70
71
# File 'lib/buzzville/ftp_extra.rb', line 68

def expand_dir(aPath,aBase=nil)
	return aPath if aPath=='/'
	return MiscUtils::path_relative?(aPath) ? File.expand_path(aPath,aBase || pwd()) : File.expand_path(aPath)
end

#file_exists?(aPath) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/buzzville/ftp_extra.rb', line 81

def file_exists?(aPath)
	aPath = expand_dir(aPath)
	filename = File.basename(aPath)
	parent = File.dirname(aPath)
	filename!='' && nlst(parent).include?(filename)
end

#filelist_recurse(aPath = nil, aResult = nil, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/buzzville/ftp_extra.rb', line 88

def filelist_recurse(aPath=nil,aResult=nil,&block)
	#puts "filelist_recurse: #{aPath.to_s}  #{aResult.inspect}"
	orig_dir = !aResult ? pwd : nil	# assigned if called at top with aResult=nil
	aResult ||= []
	aPath ||= ''
	chdir(aPath)
	list('*').each do |f| 
		is_dir = f[0,1]=='d'
		details = FTP::crack_file_line(f)
		full = File.join(aPath,details[:name])
		if !block_given? || yield(full)
			if is_dir
				filelist_recurse(full,aResult)
			else
				aResult << full
			end
		end
	end
	chdir(orig_dir) if orig_dir
	return aResult
end

#files(aPath) ⇒ Object



63
64
65
66
# File 'lib/buzzville/ftp_extra.rb', line 63

def files(aPath)
	list.delete_if {|line| line[0,1]!='d'}
	return list
end

#get_dir(aRemoteDir, aLocalDir, aOptions = nil, &block) ⇒ Object



120
121
122
123
# File 'lib/buzzville/ftp_extra.rb', line 120

def get_dir(aRemoteDir,aLocalDir,aOptions=nil,&block)
	remote_files = block_given? ? filelist_recurse(aRemoteDir,nil,&block) : filelist_recurse(aRemoteDir)
	get_files(aRemoteDir,aLocalDir,remote_files,aOptions)
end

#get_files(aRemoteDir, aLocalDir, aFiles, aOptions = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/buzzville/ftp_extra.rb', line 110

def get_files(aRemoteDir,aLocalDir,aFiles,aOptions=nil)
	aOptions = {:overwrite => true}.merge(aOptions || {})
	aFiles.each do |r|
		relative = r.bite(MiscUtils::append_slash(aRemoteDir))
		d = File.join(aLocalDir,relative)
		puts "getting #{relative}"
		getbinaryfile(r, d) unless !aOptions[:overwrite] && File.exists?(d)
	end
end

#highest_existing(aPath) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/buzzville/ftp_extra.rb', line 125

def highest_existing(aPath)
	sep = MiscUtils::sniff_seperator(aPath)
	path = MiscUtils::path_parts(File.expand_path(aPath)) if aPath.is_a?(String)
	# now assume path is an Array
	depth = path.length-1
	depth.downto(0) do |i|	# from full path up to root
		curr = (path[0]=='' && i==0) ? '/' : path[0..i].join(sep)
		return curr if dir_exists?(curr)
	end
	return sep	# root
end

#put_dir(aLocalDir, aRemoteDir, &block) ⇒ Object



181
182
183
184
# File 'lib/buzzville/ftp_extra.rb', line 181

def put_dir(aLocalDir,aRemoteDir,&block)
	local_files = block_given? ? MiscUtils::recursive_file_list(aLocalDir,true,&block) : MiscUtils::recursive_file_list(aLocalDir) 
	put_files(aLocalDir,aRemoteDir,local_files)
end

#put_files(aLocalDir, aRemoteDir, aFiles, aOptions = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/buzzville/ftp_extra.rb', line 156

def put_files(aLocalDir,aRemoteDir,aFiles,aOptions=nil)
	aOptions = {:overwrite => true}.merge(aOptions || {})

	# convert all files to relative to aLocalDir
	aFiles = aFiles.map { |f| f.bite(MiscUtils::append_slash(aLocalDir)) }.sort
	
	filelist = nil
	this_dir = last_dir = nil
	aFiles.each do |r|
		d = File.expand_path(r,aRemoteDir)
		this_dir = File.dirname(d)
		if this_dir!=last_dir
			ensure_dir(this_dir,true)
			filelist = files(this_dir) - ['.','..','.svn']
		end
		if aOptions[:overwrite] || !filelist.member?(File.basename(r))
			puts "Putting #{r}"
			putbinaryfile(File.expand_path(r,aLocalDir), d)
		else
			puts "Skipping #{relative}"
		end
		last_dir = this_dir
	end
end

#size(filename) ⇒ Object

Returns the size of the given (remote) filename.



46
47
48
49
50
51
52
53
54
# File 'lib/buzzville/ftp_extra.rb', line 46

def size(filename)
  voidcmd("TYPE I")
  resp = sendcmd("SIZE " + filename)
			code = resp[0, 3]
  if code != "213" && code != "220"
	raise FTPReplyError, resp
  end
  return resp[3..-1].strip.to_i
end

#subdirs(aPath) ⇒ Object

END BUGFIXES



58
59
60
61
# File 'lib/buzzville/ftp_extra.rb', line 58

def subdirs(aPath)
	list.delete_if {|line| line[0,1]=='d'}
	return list
end