davclient 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest CHANGED
@@ -3,8 +3,9 @@ Manifest
3
3
  README.rdoc
4
4
  Rakefile
5
5
  bin/dav
6
- davclient.gemspec
6
+ examples/meta_tags_in_use.rb
7
7
  examples/remove_ds_store.rb
8
+ examples/scrape_site.rb
8
9
  examples/simple_find.rb
9
10
  lib/davclient.rb
10
11
  lib/davclient/curl_commands.rb
@@ -14,6 +15,8 @@ lib/davclient/dav-put.rb
14
15
  lib/davclient/davcli.rb
15
16
  lib/davclient/hpricot_extensions.rb
16
17
  lib/davclient/simple.rb
18
+ lib/davclient/termutil.rb
19
+ lib/davclient/util.rb
17
20
  tests/dav.rb
18
21
  tests/tc_dav-cat.rb
19
22
  tests/tc_dav-cd.rb
@@ -25,6 +28,8 @@ tests/tc_dav-mkcol.rb
25
28
  tests/tc_dav-mv.rb
26
29
  tests/tc_dav-propfind.rb
27
30
  tests/tc_dav-put.rb
31
+ tests/tc_property_file.rb
32
+ tests/tc_util.rb
28
33
  tests/tc_webdav_basic.rb
29
34
  tests/tc_webdav_publish.rb
30
35
  tests/test_helper.rb
data/Rakefile CHANGED
@@ -12,7 +12,7 @@
12
12
  require 'rake/rdoctask'
13
13
  require 'rake/gempackagetask'
14
14
 
15
- GEM_VERSION = "0.0.6"
15
+ GEM_VERSION = "0.0.7"
16
16
 
17
17
  spec = Gem::Specification.new do |s|
18
18
  s.name = "davclient"
@@ -29,7 +29,7 @@ spec = Gem::Specification.new do |s|
29
29
  s.files = ["lib/davclient.rb","lib/davclient/hpricot_extensions.rb",
30
30
  "lib/davclient/curl_commands.rb", "bin/dav", "lib/davclient/davcli.rb",
31
31
  "lib/davclient/dav-put.rb", "lib/davclient/dav-ls.rb",
32
- "lib/davclient/dav-propfind.rb"]
32
+ "lib/davclient/dav-propfind.rb","lib/davclient/util.rb","lib/davclient/termutil.rb"]
33
33
  s.executables = ["dav"]
34
34
  s.require_path = "lib"
35
35
  s.rubyforge_project = "davclient"
@@ -37,6 +37,7 @@ spec = Gem::Specification.new do |s|
37
37
  s.has_rdoc = true
38
38
  s.extra_rdoc_files = ["README.rdoc"]
39
39
  s.add_dependency("hpricot", ">= 0.6")
40
+ s.add_dependency("termios", ">= 0.9") # For password prompt
40
41
  s.add_dependency("ZenTest", ">= 3.5") # For tests
41
42
  end
42
43
 
data/davclient.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{davclient}
5
- s.version = "0.0.6"
5
+ s.version = "0.0.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Thomas Flemming"]
9
- s.date = %q{2009-09-22}
9
+ s.date = %q{2009-10-14}
10
10
  s.default_executable = %q{dav}
11
11
  s.description = %q{Command line WebDAV client and Ruby library.}
12
12
  s.email = %q{thomasfl@usit.uio.no}
13
13
  s.executables = ["dav"]
14
- s.extra_rdoc_files = ["LICENSE", "README.rdoc", "bin/dav", "lib/davclient.rb", "lib/davclient/curl_commands.rb", "lib/davclient/dav-ls.rb", "lib/davclient/dav-propfind.rb", "lib/davclient/dav-put.rb", "lib/davclient/davcli.rb", "lib/davclient/hpricot_extensions.rb", "lib/davclient/simple.rb"]
15
- s.files = ["LICENSE", "Manifest", "README.rdoc", "Rakefile", "bin/dav", "davclient.gemspec", "examples/remove_ds_store.rb", "examples/simple_find.rb", "lib/davclient.rb", "lib/davclient/curl_commands.rb", "lib/davclient/dav-ls.rb", "lib/davclient/dav-propfind.rb", "lib/davclient/dav-put.rb", "lib/davclient/davcli.rb", "lib/davclient/hpricot_extensions.rb", "lib/davclient/simple.rb", "tests/dav.rb", "tests/tc_dav-cat.rb", "tests/tc_dav-cd.rb", "tests/tc_dav-cp.rb", "tests/tc_dav-delete.rb", "tests/tc_dav-get.rb", "tests/tc_dav-ls.rb", "tests/tc_dav-mkcol.rb", "tests/tc_dav-mv.rb", "tests/tc_dav-propfind.rb", "tests/tc_dav-put.rb", "tests/tc_webdav_basic.rb", "tests/tc_webdav_publish.rb", "tests/test_helper.rb", "tests/ts_davclient.rb"]
14
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc", "bin/dav", "lib/davclient.rb", "lib/davclient/curl_commands.rb", "lib/davclient/dav-ls.rb", "lib/davclient/dav-propfind.rb", "lib/davclient/dav-put.rb", "lib/davclient/davcli.rb", "lib/davclient/hpricot_extensions.rb", "lib/davclient/simple.rb", "lib/davclient/termutil.rb", "lib/davclient/util.rb"]
15
+ s.files = ["LICENSE", "Manifest", "README.rdoc", "Rakefile", "bin/dav", "examples/meta_tags_in_use.rb", "examples/remove_ds_store.rb", "examples/scrape_site.rb", "examples/simple_find.rb", "lib/davclient.rb", "lib/davclient/curl_commands.rb", "lib/davclient/dav-ls.rb", "lib/davclient/dav-propfind.rb", "lib/davclient/dav-put.rb", "lib/davclient/davcli.rb", "lib/davclient/hpricot_extensions.rb", "lib/davclient/simple.rb", "lib/davclient/termutil.rb", "lib/davclient/util.rb", "tests/dav.rb", "tests/tc_dav-cat.rb", "tests/tc_dav-cd.rb", "tests/tc_dav-cp.rb", "tests/tc_dav-delete.rb", "tests/tc_dav-get.rb", "tests/tc_dav-ls.rb", "tests/tc_dav-mkcol.rb", "tests/tc_dav-mv.rb", "tests/tc_dav-propfind.rb", "tests/tc_dav-put.rb", "tests/tc_property_file.rb", "tests/tc_util.rb", "tests/tc_webdav_basic.rb", "tests/tc_webdav_publish.rb", "tests/test_helper.rb", "tests/ts_davclient.rb", "davclient.gemspec"]
16
16
  s.homepage = %q{http://davclient.rubyforge.org/davclient}
17
17
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Davclient", "--main", "README.rdoc"]
18
18
  s.require_paths = ["lib"]
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'davclient/simple'
3
+
4
+ # Prints out content of <meta > tags found in files
5
+
6
+ def print_html_meta_elements(url)
7
+ doc = Hpricot(get(url))
8
+ if(doc.search("//meta").size > 0 )
9
+ puts " <tr>"
10
+ puts " <td><b>URL:</td><td><b>#{url}</b></td>"
11
+ puts " </tr>"
12
+ # puts url
13
+ doc.search("//meta").each do |elem|
14
+ puts " <tr>"
15
+ name,content = ""
16
+ if(elem.attributes.key?("name") )
17
+ name = elem.attributes["name"]
18
+ end
19
+ if(elem.attributes.key?("content") )
20
+ content = elem.attributes["content"]
21
+ end
22
+ puts " <td>#{name}</td><td>#{content}</td>"
23
+ puts " </tr>"
24
+ end
25
+ puts
26
+ end
27
+ end
28
+
29
+
30
+
31
+ def print_html_meta_elements_as_plaintext(url)
32
+ doc = Hpricot(get(url))
33
+ if(doc.search("//meta").size > 0 )
34
+
35
+ puts url
36
+ doc.search("//meta").each do |elem|
37
+ if(elem.attributes.key?("name") )
38
+ print " Name: " + elem.attributes["name"].ljust(30)
39
+ end
40
+ if(elem.attributes.key?("content") )
41
+ print " Content: " + elem.attributes["content"]
42
+ end
43
+ puts
44
+ end
45
+ puts
46
+ end
47
+ end
48
+
49
+
50
+ cd "https://www-dav.jus.uio.no/it/"
51
+
52
+ puts "<table>"
53
+ puts " <tr>"
54
+ puts " <td><b>Meta- name</td><td><b>Meta-content</b></td>"
55
+ puts " </tr>"
56
+
57
+ find :recursive => true do |item|
58
+ url = item.href.to_s
59
+ if(url =~ /htm?|\.xml$/ )
60
+ print_html_meta_elements(url)
61
+ end
62
+ end
63
+
64
+ puts "</table>"
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'davclient/simple'
3
+
4
+ # Simple utility to extract all meta tags in use by html files
5
+
6
+ def print_meta_elements(url)
7
+ doc = Hpricot(get(url))
8
+ if(doc.search("//meta").size > 0 )
9
+ doc.search("//meta").each do |elem|
10
+ name,content = ""
11
+ if(elem.attributes.key?("name") )
12
+ name = elem.attributes["name"]
13
+ end
14
+ if(elem.attributes.key?("content") )
15
+ content = elem.attributes["content"]
16
+ end
17
+ puts "#{url}\t#{name}\t#{content}"
18
+ end
19
+ end
20
+ end
21
+
22
+
23
+ url = ARGV[0]
24
+ if(!url)
25
+ puts "Missing url"
26
+ exit
27
+ end
28
+
29
+ cd url
30
+
31
+ find :recursive => false do |item|
32
+ url = item.href.to_s
33
+ if(url =~ /htm?|\.xml$/ )
34
+ print_meta_elements(url)
35
+ end
36
+ end
@@ -1,23 +1,27 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
+ #
4
+ # Templates for curl commands
5
+ #
6
+
7
+ # Used to improve readability of curl commands that always
8
+ # needs to be on one line
3
9
  def remove_newlines(string)
4
10
  string.gsub("\n","").gsub(/ +/," ") + " "
5
11
  end
6
12
 
7
13
  # Templates for curl commands:
8
14
  curl_propfind_cmd = <<EOF
9
- #{$curl}
10
15
  --request PROPFIND
16
+ --max-redirs 1
11
17
  --header 'Content-Type: text/xml; charset="utf-8"'
12
18
  --header "Depth: 1"
13
19
  --data-ascii '<?xml version="1.0" encoding="utf-8"?>
14
20
  <DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>'
15
- --netrc
16
21
  EOF
17
22
  CURL_PROPFIND = remove_newlines(curl_propfind_cmd)
18
23
 
19
24
  curl_proppatch_cmd = <<EOF
20
- #{$curl}
21
25
  --request PROPPATCH
22
26
  --header 'Content-Type: text/xml; charset="utf-8"'
23
27
  --header "Depth: 1"
@@ -29,45 +33,38 @@ curl_proppatch_cmd = <<EOF
29
33
  </d:prop>
30
34
  </d:set>
31
35
  </d:propertyupdate>'
32
- --netrc
33
36
  EOF
34
37
  CURL_PROPPATCH = remove_newlines(curl_proppatch_cmd)
35
38
 
36
39
  curl_delete_cmd = <<EOF
37
- #{$curl}
38
40
  --request DELETE
39
41
  --header 'Content-Type: text/xml; charset="utf-8"'
40
- --netrc
41
42
  EOF
42
43
  CURL_DELETE = remove_newlines(curl_delete_cmd)
43
44
 
44
45
  curl_mkcol_cmd = <<EOF
45
- #{$curl}
46
46
  --request MKCOL
47
47
  --header 'Content-Type: text/xml; charset="utf-8"'
48
- --netrc
49
48
  EOF
50
49
  CURL_MKCOL = remove_newlines(curl_mkcol_cmd)
51
50
 
52
- CURL_OPTIONS = "#{$curl} -i -X OPTIONS --netrc "
51
+ CURL_OPTIONS = "-i -X OPTIONS "
53
52
 
54
53
  curl_copy = <<EOF
55
- #{$curl}
56
54
  --request COPY
57
55
  --header 'Content-Type: text/xml; charset="utf-8"'
58
56
  --header 'Destination: <!--destination-->'
59
- --netrc
60
57
  EOF
61
58
 
62
59
  CURL_COPY = remove_newlines(curl_copy)
63
60
 
64
61
 
65
62
  curl_move = <<EOF
66
- #{$curl}
67
63
  --request MOVE
68
64
  --header 'Content-Type: text/xml; charset="utf-8"'
69
65
  --header 'Destination: <!--destination-->'
70
- --netrc
71
66
  EOF
72
67
 
73
68
  CURL_MOVE = remove_newlines(curl_move)
69
+
70
+ CURL_UPLOAD = "--upload-file"
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
-
3
- require 'rubygems'
2
+ # require 'rubygems'
4
3
  require 'hpricot'
4
+ require 'davclient'
5
5
 
6
6
  # Extensions to the Hpricot XML parser.
7
7
  module Hpricot
@@ -31,26 +31,31 @@ module Hpricot
31
31
 
32
32
  end
33
33
 
34
- # TODO Not used. Delete???
35
- def type_convert_value(value)
36
- if(returnValue == "true")then
37
- return true
34
+
35
+ # Get content from resources on server
36
+ # Example:
37
+ # webpage = WebDAV.find("http://example.org/index.html")
38
+ # print "html src: " + page.content
39
+ def content
40
+ if(!isCollection?)
41
+ WebDAV.get(self.at("d:href").innerText)
38
42
  end
39
- if(returnValue == "false")then
40
- return false
43
+ end
44
+
45
+ def content=(string)
46
+ if(!isCollection?)
47
+ WebDAV.put_string(href,string)
41
48
  end
42
- # Number format???
43
- ## Dato format
44
- return returnValue
45
49
  end
46
50
 
47
- # TODO: Make list of recognized namespace prefixes configurable
48
- # Get property.
51
+ # Get property for resource or collection.
49
52
  # Example:
50
53
  # page = WebDAV.find(url)
51
54
  # print page.property("published-date")
52
55
  def property(name)
53
56
 
57
+ # TODO: Make list of recognized namespace prefixes configurable
58
+
54
59
  property = property = self.at(name)
55
60
  if(property)then
56
61
  returnValue = property.innerText
@@ -89,6 +94,21 @@ module Hpricot
89
94
  File.basename(self.at("d:href").innerText)
90
95
  end
91
96
 
97
+
98
+ # Set the items WebDAV properties. Properties must be a string with XML.
99
+ # Example:
100
+ #
101
+ # find(url) do |item|
102
+ # if(item.href =~ /html$/) then
103
+ # item.proppatch("<d:getcontenttype>text/html</d:getcontenttype>")
104
+ # end
105
+ # end
106
+ def proppatch(properties)
107
+ WebDAV.proppatch(href, properties)
108
+ end
109
+
110
+ # :stopdoc:
111
+
92
112
  # TODO: Move to vortex_lib.rb
93
113
  def dateProperty(name)
94
114
  date = self.property(name)
@@ -108,9 +128,21 @@ module Hpricot
108
128
  return time
109
129
  end
110
130
 
111
- # Set the items WebDAV properties. Properties must be a string with XML.
112
- def proppatch(properties)
113
- WebDAV.proppatch(href, properties)
131
+
132
+ # TODO Not used. Delete???
133
+ def type_convert_value(value)
134
+ if(returnValue == "true")then
135
+ return true
136
+ end
137
+ if(returnValue == "false")then
138
+ return false
139
+ end
140
+ # Number format???
141
+ ## Dato format
142
+ return returnValue
114
143
  end
115
144
 
145
+
146
+ # :startdoc:
147
+
116
148
  end
@@ -0,0 +1,55 @@
1
+ # require 'rubygems'
2
+ require 'termios'
3
+
4
+ # Prompt for password
5
+ # Extracted from Ian Macdonald's Ruby/Password gem.
6
+ #
7
+ # Example:
8
+ #
9
+ # password = getc(message="Password: ", mask='*')
10
+ # puts "It's:" + password
11
+
12
+ class TermUtil
13
+
14
+ def self.echo(on=true, masked=false)
15
+ term = Termios::getattr( $stdin )
16
+
17
+ if on
18
+ term.c_lflag |= ( Termios::ECHO | Termios::ICANON )
19
+ else # off
20
+ term.c_lflag &= ~Termios::ECHO
21
+ term.c_lflag &= ~Termios::ICANON if masked
22
+ end
23
+
24
+ Termios::setattr( $stdin, Termios::TCSANOW, term )
25
+ end
26
+
27
+ def self.getc(message="Password: ", mask='*')
28
+ # Save current buffering mode
29
+ buffering = $stdout.sync
30
+
31
+ # Turn off buffering
32
+ $stdout.sync = true
33
+
34
+ begin
35
+ echo(false, true)
36
+ print message if message
37
+ pw = ""
38
+
39
+ while ( char = $stdin.getc ) != 10 # break after [Enter]
40
+ putc mask
41
+ pw << char
42
+ end
43
+
44
+ ensure
45
+ echo true
46
+ print "\n"
47
+ end
48
+
49
+ # Restore original buffering mode
50
+ $stdout.sync = buffering
51
+
52
+ return pw
53
+ end
54
+
55
+ end
@@ -0,0 +1,241 @@
1
+ require 'tempfile'
2
+ require 'davclient/termutil'
3
+ require 'open3'
4
+
5
+ # DavClient Utilitis
6
+
7
+ module DavClient
8
+
9
+ # Loads contents of property file into an array
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
42
+
43
+
44
+ # Returns options for an url read from .davclientrc file
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
55
+
56
+
57
+
58
+ # Returns filename /tmp/cwurl.#pid that holds the current working directory
59
+ # for the shell's pid
60
+ def self.cwurl_filename
61
+ return DavClient.tmp_folder + "cwurl." + Process.ppid.to_s
62
+ end
63
+
64
+
65
+ # Returns name of temp folder we're using
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
71
+
72
+ # Write string to tempfile and returns filename
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
80
+
81
+
82
+ # Display instructions for adding credentials to .netrc file
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
93
+
94
+ # Prompts user for username and password
95
+ # Prints hostname to console if set.
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
108
+
109
+
110
+ # Spawns a new process. Gives curl password if password
111
+ # is not nil.
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
123
+
124
+ def self.exctract_host(url)
125
+ result = url.match(/http.*\/\/([^\/\?]*)/)
126
+ if(result)
127
+ return result[1]
128
+ end
129
+ end
130
+
131
+
132
+ # Run 'curl' as a subprocess
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
169
+
170
+ # Run 'curl' as a subprocess with pty
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
240
+
241
+ end
data/lib/davclient.rb CHANGED
@@ -1,17 +1,17 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'hpricot'
4
- require 'tempfile'
5
- require 'open3'
6
4
  require 'pathname'
7
5
  require 'davclient/hpricot_extensions'
6
+ require 'davclient/curl_commands'
7
+ require 'davclient/util'
8
8
 
9
9
  # :stopdoc:
10
10
 
11
11
  # Path to curl executable:
12
12
  $curl = "curl"
13
13
 
14
- require 'davclient/curl_commands'
14
+
15
15
 
16
16
  # :startdoc:
17
17
 
@@ -41,10 +41,8 @@ module WebDAV
41
41
  def self.CWURL
42
42
  return $CWURL if($CWURL)
43
43
  cwurl = nil
44
- filename = cwurl_filename
45
- if(File.exists?(filename))
46
- File.open(filename, 'r') {|f| cwurl = f.read() }
47
- end
44
+ filename = DavClient.cwurl_filename
45
+
48
46
  return cwurl
49
47
  end
50
48
 
@@ -112,7 +110,7 @@ module WebDAV
112
110
  # as part of the filename.
113
111
  def self.CWURL=(url)
114
112
  $CWURL = url
115
- File.open(cwurl_filename, 'w') {|f| f.write(url) }
113
+ File.open(DavClient.cwurl_filename, 'w') {|f| f.write(url) }
116
114
  end
117
115
 
118
116
 
@@ -126,8 +124,8 @@ module WebDAV
126
124
  def self.get(url)
127
125
  url = absoluteUrl(url)
128
126
 
129
- curl_command = "#{$curl} --netrc " + url
130
- return exec_curl(curl_command)
127
+ curl_command = url
128
+ return DavClient.exec_curl(curl_command)
131
129
  end
132
130
 
133
131
  # Set WebDAV properties for url as xml.
@@ -139,7 +137,7 @@ module WebDAV
139
137
  url = absoluteUrl(url)
140
138
  curl_command = CURL_PROPPATCH + " \""+url+"\""
141
139
  curl_command = curl_command.gsub("<!--property-and-value-->",property)
142
- response = exec_curl(curl_command)
140
+ response = DavClient.exec_curl(curl_command)
143
141
  if(not(response =~ /200 OK/)) then
144
142
  puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
145
143
  exit(0)
@@ -159,15 +157,16 @@ module WebDAV
159
157
  options = args[1]
160
158
 
161
159
  curl_command = CURL_PROPFIND + " \"" + url + "\""
162
- response = exec_curl(curl_command)
160
+ response = DavClient.exec_curl(curl_command)
163
161
 
164
162
  if(response == "")then
165
163
  return nil
166
164
  end
167
165
 
168
166
  if(not(response =~ /200 OK/)) then
169
- puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
170
- exit(0)
167
+ # puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
168
+ # exit(0)
169
+ raise "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
171
170
  end
172
171
 
173
172
  if(options and options[:xml])then
@@ -242,7 +241,13 @@ module WebDAV
242
241
  recursive = options[:recursive]
243
242
  end
244
243
  end
245
- dav_xml_output = propfind(href, :xml => true)
244
+ begin
245
+ dav_xml_output = propfind(href, :xml => true)
246
+ rescue Exception => exception
247
+ $stderr.puts "Warning: " + href + " : " + exception
248
+ # raise "er
249
+ return nil
250
+ end
246
251
  if(not(dav_xml_output))then
247
252
  return nil
248
253
  end
@@ -304,7 +309,7 @@ module WebDAV
304
309
  props = args[3]
305
310
  url = absoluteUrl(url)
306
311
  curl_command = CURL_MKCOL + " " + url
307
- response = exec_curl(curl_command)
312
+ response = DavClient.exec_curl(curl_command)
308
313
 
309
314
  if(props)then
310
315
  proppatch(url,props)
@@ -337,7 +342,7 @@ module WebDAV
337
342
 
338
343
  # puts "DEBUG: " + srcUrl + " => " + destUrl
339
344
  curl_command = CURL_COPY.sub("<!--destination-->", destUrl) + " " + srcUrl
340
- response = exec_curl(curl_command)
345
+ response = DavClient.exec_curl(curl_command)
341
346
 
342
347
  if(response == "")then
343
348
  return destUrl
@@ -357,7 +362,7 @@ module WebDAV
357
362
 
358
363
  # puts "DEBUG: " + srcUrl + " => " + destUrl
359
364
  curl_command = CURL_MOVE.sub("<!--destination-->", destUrl) + " " + srcUrl
360
- response = exec_curl(curl_command)
365
+ response = DavClient.exec_curl(curl_command)
361
366
 
362
367
  if(response == "")then
363
368
  return destUrl
@@ -378,7 +383,7 @@ module WebDAV
378
383
  url = absoluteUrl(url)
379
384
 
380
385
  curl_command = CURL_DELETE + url
381
- response = exec_curl(curl_command)
386
+ response = DavClient.exec_curl(curl_command)
382
387
 
383
388
  if(response == "")then
384
389
  return url
@@ -417,17 +422,21 @@ module WebDAV
417
422
  raise "Error: WebDAV.put_html: url can not be a collection (folder)."
418
423
  end
419
424
 
420
- filename = string2tempfile(str)
425
+ filename = DavClient.string2tempfile(str)
421
426
  put(url,filename)
422
427
  end
423
428
 
424
429
 
425
- # Upload local file
430
+ # Upload local file to webserver
431
+ #
432
+ # Example:
433
+ #
434
+ # WebDAV.put("https://example.org/myfile.html", "myfile.html")
426
435
  def self.put(url, file_name)
427
436
  url = absoluteUrl(url)
428
437
 
429
- curl_command = "#{$curl} --netrc --silent --upload-file #{file_name} #{url}"
430
- response = exec_curl(curl_command)
438
+ curl_command = CURL_UPLOAD + " " + file_name + " " + url
439
+ response = DavClient.exec_curl(curl_command)
431
440
  if(response != "" and not(response =~ /200 OK/)) then
432
441
  raise "Error:\n WebDAV.put: WebDAV Request:\n" + curl_command + "\n\nResponse: " + response
433
442
  end
@@ -438,83 +447,7 @@ module WebDAV
438
447
  if(not(url))
439
448
  url = self.CWURL
440
449
  end
441
- return self.exec_curl(CURL_OPTIONS + url )
450
+ return DavClient.exec_curl(CURL_OPTIONS + url )
442
451
  end
443
452
 
444
- # Returns name of temp folder we're using
445
- # TODO: Move this to utility library
446
- def self.tmp_folder
447
- tmp_file = Tempfile.new("dummy").path
448
- basename = File.basename(tmp_file)
449
- return tmp_file.gsub(basename, "")
450
- end
451
-
452
-
453
- # TODO: Move this to utility library
454
- # Write string to tempfile and returns filename
455
- def self.string2tempfile(str)
456
- tmp_dir = tmp_folder + rand.to_s[2..10] + "/"
457
- FileUtils.mkdir_p tmp_dir
458
- tmp_file = tmp_dir + "webdav.tmp"
459
- File.open(tmp_file, 'w') {|f| f.write(str) }
460
- return tmp_file
461
- end
462
-
463
-
464
-
465
- # Returns filename /tmp/cwurl.#pid that holds the current working directory
466
- # for the shell's pid
467
- def self.cwurl_filename
468
- return tmp_folder + "cwurl." + Process.ppid.to_s
469
- end
470
-
471
- private
472
-
473
- # Display instructions for adding credentials to .netrc file
474
- def self.display_unauthorized_message(href)
475
- puts "Error: 401 Unauthorized: " + href
476
- href.match(/^http.*\/\/([^\/]*)/)
477
- puts "\nTry adding the following to your ~/.netrc file:"
478
- puts ""
479
- puts "machine #{$1}"
480
- puts " login " + ENV['USER']
481
- puts " password ********"
482
- puts ""
483
- end
484
-
485
-
486
- # Run 'curl' as a subprocess
487
- def self.exec_curl(curl_command)
488
- response = ""
489
-
490
- puts curl_command if($DEBUG)
491
-
492
- Open3.popen3(curl_command) do |stdin, stdout, stderr|
493
-
494
- response = stdout.readlines.join("")
495
-
496
- if(response == "")
497
- stderr = stderr.readlines.join("").sub(/^\W/,"")
498
- if(stderr =~ /command/)
499
- puts "Error: " + stderr
500
- exit
501
- end
502
- if(stderr =~ /^curl:/)
503
- puts "Error: " + stderr
504
- puts
505
- puts curl_command
506
- puts
507
- exit
508
- end
509
- end
510
- end
511
- if(response =~ /401 Unauthorized/)then
512
- href = curl_command.match( /"(http[^\"]*)"$/ )[0].gsub(/"/,"")
513
- self.display_unauthorized_message(href)
514
- exit
515
- end
516
- return response
517
- end
518
-
519
-
520
453
  end
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'davclient/util'
6
+
7
+ class TestPropertyFile < Test::Unit::TestCase
8
+
9
+ def setup
10
+ $settings = DavClient.load_davclientrc_file
11
+ # url = "https://vortex-dav.uio.no/brukere/thomasfl/"
12
+ # settings = DavClient.site_settings(url,$settings)
13
+ end
14
+
15
+ def settings(url)
16
+ DavClient.site_options(url,$settings)
17
+ end
18
+
19
+ def test_test
20
+ assert true
21
+ end
22
+ def test_property_file
23
+ assert_equal "password-prompt", settings("https://vortex-dav.uio.no/brukere/thomasfl/testseminar/")
24
+ assert_equal "--netrc", settings("https://vortex-dav.uio.no/brukere/thomasfl/")
25
+ assert_equal "--netrc", settings("https://www.uio.no/brukere/")
26
+ # assert_equal "--netrc", settings("https://www-dav.test.no")
27
+ assert_equal "", settings("https://www-dav.test.com/homepage")
28
+ assert_equal "", settings("https://www-dav.test.com/homepage")
29
+ end
30
+
31
+ end
data/tests/tc_util.rb ADDED
@@ -0,0 +1,39 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'davclient/util'
6
+ require 'davclient/curl_commands'
7
+
8
+ $cmd = <<EOF
9
+ curl --user thomasfl --request PROPFIND --header 'Content-Type: text/xml; charset="utf-8"' --header "Depth: 1" --data-ascii '<?xml version="1.0" encoding="utf-8"?><DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>' "https://vortex-dav.uio.no/brukere/thomasfl/testseminar/"
10
+ EOF
11
+
12
+ class TestUtil < Test::Unit::TestCase
13
+
14
+ def test_extract_host
15
+ assert_equal "host.host.com", DavClient.exctract_host("https://host.host.com/lots-of-rubbish")
16
+ assert_equal "host.host.com", DavClient.exctract_host("http://host.host.com?lots-of-rubbish")
17
+ assert_equal nil, DavClient.exctract_host("//not-an-url")
18
+ end
19
+
20
+ # Interactiv tests
21
+ def zzz_test_01_password_prompt
22
+ $username = nil
23
+ $password = nil
24
+ DavClient.prompt_for_username_and_password("host.host.com")
25
+ assert $username
26
+ assert $password
27
+ end
28
+
29
+ def zzz_test_02_spawn_curl
30
+ result = DavClient.spawn_curl($cmd, $password)
31
+ assert result =~ /d:multistatus/
32
+ result = DavClient.spawn_curl($cmd, "feil")
33
+ assert result =~ /401 Unauthorized/
34
+ cmd = $cmd.sub("--user thomasfl","--netrc")
35
+ result = DavClient.spawn_curl(cmd, nil)
36
+ assert result =~ /d:multistatus/
37
+ end
38
+
39
+ end
@@ -8,6 +8,30 @@ $curl = "/usr/local/bin/curl"
8
8
 
9
9
  class TestWebDAVLib < Test::Unit::TestCase
10
10
 
11
+ def test_01_hpricot_extensions
12
+ url = "https://vortex-dav.uio.no/brukere/thomasfl/"
13
+ test_content = "<html><head><title>title</title></head><body>one two three</body></html>"
14
+ WebDAV.delete(url + "testcase")
15
+ WebDAV.mkcol(url + "testcase")
16
+ WebDAV.publish(url + "testcase/test_page.html", test_content,nil)
17
+
18
+ webpage = nil
19
+ WebDAV.find(url + "testcase/") do |item|
20
+ webpage = item
21
+ end
22
+
23
+ # puts webpage.href
24
+ # puts "content: " + webpage.content
25
+
26
+ webpage.content = webpage.content.gsub(/two/,"2")
27
+
28
+ webpage = nil
29
+ WebDAV.find(url + "testcase/") do |item|
30
+ webpage = item
31
+ end
32
+ assert webpage.content =~ /one 2 three/
33
+
34
+ end
11
35
 
12
36
  def test_find
13
37
  url = "https://vortex-dav.uio.no/prosjekter/it-avisa/nyheter/"
@@ -40,45 +64,6 @@ class TestWebDAVLib < Test::Unit::TestCase
40
64
  assert(counter > 9 )
41
65
  end
42
66
 
43
- end
44
-
45
- class BullShit
46
-
47
- def test_find_recursive
48
- count = 0
49
- url = "https://vortex-dav.uio.no/brukere/thomasfl/anniken_2007/sophos/"
50
- WebDAV.find(url, :recursive => true ) do | item |
51
- if(item.basename == ".DS_Store")
52
- count += 1
53
- end
54
- end
55
- assert_equal(1, count)
56
- end
57
-
58
- def test_get
59
- url = "https://vortex-dav.uio.no/prosjekter/it-avisa/nyheter/2008/02/nynorsk-paa-nett.html"
60
- item = WebDAV.propfind(url)
61
- assert(item)
62
-
63
- content = WebDAV.get(url)
64
- assert( content =~ /\<html/ )
65
-
66
- end
67
-
68
- def test_mkcol
69
- url = "https://vortex-dav.uio.no/prosjekter/it-avisa/nyheter/testcol/"
70
-
71
- WebDAV.delete(url)
72
-
73
- result = WebDAV.mkcol(url,nil)
74
- assert(result)
75
- col = WebDAV.propfind(url)
76
- assert(col)
77
-
78
- # TODO Set properties på collection
79
-
80
- end
81
-
82
67
  def test_propfind
83
68
  url = "https://vortex-dav.uio.no/prosjekter/it-avisa/nyheter/"
84
69
  item = WebDAV.propfind(url, :xml => true)
@@ -98,5 +83,4 @@ class BullShit
98
83
 
99
84
  end
100
85
 
101
-
102
86
  end
@@ -3,9 +3,10 @@ require 'tc_webdav_basic.rb'
3
3
  require 'tc_webdav_publish.rb'
4
4
  require 'tc_dav-cat.rb'
5
5
  # require 'tc_dav-cd.rb'
6
- # require 'tc_dav-cp.rb'
7
- # require 'tc_dav-delete.rb'
6
+ require 'tc_dav-cp.rb'
7
+ require 'tc_dav-delete.rb'
8
8
  # require 'tc_dav-ls.rb'
9
- # require 'tc_dav-mkcol.rb'
10
- # require 'tc_dav-mv.rb'
11
- # require 'tc_dav-propfind.rb'
9
+ require 'tc_dav-mkcol.rb'
10
+ require 'tc_dav-mv.rb'
11
+ require 'tc_dav-propfind.rb'
12
+ require 'tc_property_file.rb'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: davclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Flemming
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-22 00:00:00 +02:00
12
+ date: 2009-10-14 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,14 +31,17 @@ extra_rdoc_files:
31
31
  - lib/davclient/davcli.rb
32
32
  - lib/davclient/hpricot_extensions.rb
33
33
  - lib/davclient/simple.rb
34
+ - lib/davclient/termutil.rb
35
+ - lib/davclient/util.rb
34
36
  files:
35
37
  - LICENSE
36
38
  - Manifest
37
39
  - README.rdoc
38
40
  - Rakefile
39
41
  - bin/dav
40
- - davclient.gemspec
42
+ - examples/meta_tags_in_use.rb
41
43
  - examples/remove_ds_store.rb
44
+ - examples/scrape_site.rb
42
45
  - examples/simple_find.rb
43
46
  - lib/davclient.rb
44
47
  - lib/davclient/curl_commands.rb
@@ -48,6 +51,8 @@ files:
48
51
  - lib/davclient/davcli.rb
49
52
  - lib/davclient/hpricot_extensions.rb
50
53
  - lib/davclient/simple.rb
54
+ - lib/davclient/termutil.rb
55
+ - lib/davclient/util.rb
51
56
  - tests/dav.rb
52
57
  - tests/tc_dav-cat.rb
53
58
  - tests/tc_dav-cd.rb
@@ -59,10 +64,13 @@ files:
59
64
  - tests/tc_dav-mv.rb
60
65
  - tests/tc_dav-propfind.rb
61
66
  - tests/tc_dav-put.rb
67
+ - tests/tc_property_file.rb
68
+ - tests/tc_util.rb
62
69
  - tests/tc_webdav_basic.rb
63
70
  - tests/tc_webdav_publish.rb
64
71
  - tests/test_helper.rb
65
72
  - tests/ts_davclient.rb
73
+ - davclient.gemspec
66
74
  has_rdoc: true
67
75
  homepage: http://davclient.rubyforge.org/davclient
68
76
  licenses: []