Module: DavClient
- Defined in:
- lib/davclient/util.rb
Overview
DavClient Utilitis
Class Method Summary collapse
-
.cwurl_filename ⇒ Object
Returns filename /tmp/cwurl.#pid that holds the current working directory for the shell’s pid.
-
.display_unauthorized_message(href) ⇒ Object
Display instructions for adding credentials to .netrc file.
- .exctract_host(url) ⇒ Object
-
.exec_curl(curl_command) ⇒ Object
Run ‘curl’ as a subprocess.
-
.exec_curl2(curl_command) ⇒ Object
Run ‘curl’ as a subprocess with pty.
-
.load_davclientrc_file ⇒ Object
Loads contents of property file into an array.
-
.prompt_for_username_and_password(host) ⇒ Object
Prompts user for username and password Prints hostname to console if set.
-
.site_options(url, settings) ⇒ Object
Returns options for an url read from .davclientrc file.
-
.spawn_curl(command, password) ⇒ Object
Spawns a new process.
-
.string2tempfile(str) ⇒ Object
Write string to tempfile and returns filename.
-
.tmp_folder ⇒ Object
Returns name of temp folder we’re using.
Class Method Details
.cwurl_filename ⇒ Object
Returns filename /tmp/cwurl.#pid that holds the current working directory for the shell’s pid
60 61 62 |
# File 'lib/davclient/util.rb', line 60 def self.cwurl_filename return DavClient.tmp_folder + "cwurl." + Process.ppid.to_s end |
.display_unauthorized_message(href) ⇒ Object
Display instructions for adding credentials to .netrc file
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/davclient/util.rb', line 83 def self.(href) puts "Error: 401 Unauthorized: " + href href.match(/^http.*\/\/([^\/]*)/) # puts "\nTry adding the following to your ~/.netrc file:" # puts "" # puts "machine #{$1}" # puts " login " + ENV['USER'] # puts " password ********" # puts "" end |
.exctract_host(url) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/davclient/util.rb', line 124 def self.exctract_host(url) result = url.match(/http.*\/\/([^\/\?]*)/) if(result) return result[1] end end |
.exec_curl(curl_command) ⇒ Object
Run ‘curl’ as a subprocess
133 134 135 136 137 138 139 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 |
# File 'lib/davclient/util.rb', line 133 def self.exec_curl(curl_command) response = "" puts curl_command if($DEBUG) # Common options for all curl commands = "--netrc" # --user-agent curl_command = "#{$curl} " + + " " + curl_command Open3.popen3(curl_command) do |stdin, stdout, stderr| response = stdout.readlines.join("") if(response == "") stderr = stderr.readlines.join("").sub(/^\W/,"") if(stderr =~ /command/) raise stderr # puts "Error: " + stderr # exit end if(stderr =~ /^curl:/) raise stderr # puts "Error: " + stderr # puts # puts curl_command # puts # exit end end end if(response =~ /401 Unauthorized/)then href = curl_command.match( /"(http[^\"]*)"$/ )[0].gsub(/"/,"") # self.display_unauthorized_message(href) # exit end return response end |
.exec_curl2(curl_command) ⇒ Object
Run ‘curl’ as a subprocess with pty
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/davclient/util.rb', line 171 def self.exec_curl2(curl_command) response = "" puts curl_command if($DEBUG) url = curl_command.match("http[^ ]*$").to_s if(url == nil or url == "")then puts "Curl command does not contain url." raise RuntimeError end url = url.sub(/\"$/,"") host = exctract_host(url) settings = load_davclientrc_file = (url, settings) # puts;puts "url:" + url + " => '" + options + "'"; if( =~ /password-prompt/) # no-password-prompt = .sub(/password-prompt/, "") if($username) # Is username stored in $username variable ??? else print("Username: ") $stdout.flush $username = STDIN.gets $username.strip! require 'davclient/termutil' $password = TermUtil.getc(="Password: ", mask='*') # $password.strip! puts "pass::" + $password end += " --user " + $username + " " end curl_command = "#{$curl} " + + " " + curl_command puts puts curl_command Open3.popen3(curl_command) do |stdin, stdout, stderr| stdin.puts $password # + "\n" response = stdout.readlines.join("") if(response == "") stderr = stderr.readlines.join("").sub(/^\W/,"") if(stderr =~ /command/) # puts "Error: " + stderr raise "Error: " + stderr # exit end if(stderr =~ /^curl:/) raise "Error: " + stderr # puts "Error: " + stderr # puts # puts curl_command # puts # exit end end end if(response =~ /401 Unauthorized/)then href = curl_command #.match( /"(http[^\"]*)"$/ )[0].gsub(/"/,"") # DavClient.display_unauthorized_message(href) raise "Could not execute :" + response end return response end |
.load_davclientrc_file ⇒ Object
Loads contents of property file into an array
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/davclient/util.rb', line 10 def self.load_davclientrc_file properties_filename = ENV['HOME'] + "/.davclientrc" return nil if not(File.exists?( properties_filename )) properties = [] index = 0 File.open(properties_filename, 'r') do |properties_file| properties_file.read.each_line do |line| line.strip! line = line.sub(/#.*/,"") if (line[0] != ?# and line[0] != ?= ) i = line.index('=') if (i) # properties[line[0..i - 1].strip] = line[i + 1..-1].strip key = line[0..i - 1].strip if(key != "") properties[index] = [ key, line[i + 1..-1].strip ] index += 1 end else key = line if(key != "" and not(key =~ /^\[/) ) properties[index] = [key, ''] index += 1 end end end end end return properties end |
.prompt_for_username_and_password(host) ⇒ Object
Prompts user for username and password Prints hostname to console if set.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/davclient/util.rb', line 96 def self.prompt_for_username_and_password(host) if(host) print("Enter username for host '#{host}': ") else print("Username: ") end $stdout.flush $username = STDIN.gets $username.strip! $password = TermUtil.getc(="Password: ", mask='*') $password.strip! end |
.site_options(url, settings) ⇒ Object
Returns options for an url read from .davclientrc file
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/davclient/util.rb', line 45 def self.(url, settings) settings.each_index do | index| key,value = settings[index] # puts key + "--->" + value + " " + url if(url.match(key) and key != "")then return value end end return "" end |
.spawn_curl(command, password) ⇒ Object
Spawns a new process. Gives curl password if password is not nil.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/davclient/util.rb', line 112 def self.spawn_curl(command, password) PTY.spawn(command) do |reader, writer, pid| if(password) reader.expect(/^Enter.*:/) writer.puts(password) end answer = reader.readlines.join("") reader.close return answer end end |
.string2tempfile(str) ⇒ Object
Write string to tempfile and returns filename
73 74 75 76 77 78 79 |
# File 'lib/davclient/util.rb', line 73 def self.string2tempfile(str) tmp_dir = DavClient.tmp_folder + rand.to_s[2..10] + "/" FileUtils.mkdir_p tmp_dir tmp_file = tmp_dir + "webdav.tmp" File.open(tmp_file, 'w') {|f| f.write(str) } return tmp_file end |
.tmp_folder ⇒ Object
Returns name of temp folder we’re using
66 67 68 69 70 |
# File 'lib/davclient/util.rb', line 66 def self.tmp_folder tmp_file = Tempfile.new("dummy").path basename = File.basename(tmp_file) return tmp_file.gsub(basename, "") end |