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