fernyb_davclient 0.0.9

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.
@@ -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,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'davclient'
4
+
5
+ # Remove all resources (files) named ".DS_Store"
6
+
7
+ url = ARGV[0]
8
+
9
+ WebDAV.find(url, :recursive => true ) do | item |
10
+ puts item.href if(item.isCollection?)
11
+ if(item.basename == ".DS_Store")
12
+ puts "Removing: " + item.href
13
+ WebDAV.delete(item.href)
14
+ end
15
+ end
@@ -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
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'davclient/simple'
3
+
4
+ # Simply print the url of all files and collections on server
5
+
6
+ cd "https://vortex-dav.uio.no/brukere/thomasfl/"
7
+
8
+ find do |item|
9
+ puts item.href
10
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{fernyb_davclient}
5
+ s.version = "0.0.9"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Thomas Flemming"]
9
+ s.date = %q{2009-11-15}
10
+ s.default_executable = %q{dav}
11
+ s.description = %q{Command line WebDAV client and Ruby library.}
12
+ s.email = %q{thomasfl@usit.uio.no}
13
+ s.executables = ["dav"]
14
+ s.extra_rdoc_files = ["bin/dav", "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", "lib/davclient.rb", "LICENSE", "README.rdoc"]
15
+ s.files = ["bin/dav", "examples/clean_files.rb", "examples/meta_tags_in_use.rb", "examples/remove_ds_store.rb", "examples/scrape_site.rb", "examples/simple_find.rb", "fernyb_davclient.gemspec", "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", "lib/davclient.rb", "LICENSE", "Manifest", "Rakefile", "README.rdoc", "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_util.rb", "tests/tc_webdav_basic.rb", "tests/tc_webdav_publish.rb", "tests/test_helper.rb", "tests/ts_davclient.rb", "todo"]
16
+ s.homepage = %q{http://davclient.rubyforge.org/davclient}
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Fernyb_davclient", "--main", "README.rdoc"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{fernyb_davclient}
20
+ s.rubygems_version = %q{1.3.5}
21
+ s.summary = %q{Command line WebDAV client and Ruby library.}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<echoe>, [">= 0"])
29
+ else
30
+ s.add_dependency(%q<echoe>, [">= 0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<echoe>, [">= 0"])
34
+ end
35
+ end
@@ -0,0 +1,513 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'hpricot'
4
+ require 'pathname'
5
+ require 'davclient/hpricot_extensions'
6
+ require 'davclient/curl_commands'
7
+ require 'davclient/util'
8
+
9
+ # :stopdoc:
10
+
11
+ # Path to curl executable:
12
+ $curl = "curl"
13
+
14
+
15
+
16
+ # :startdoc:
17
+
18
+ # Main WebDAV client. Current working URL is stored in a tempfile named /tmp/cwurl.pid, where pid is the parent process.
19
+ # Username an password is stored in the ~/.netrc file. This means there is no need to set up any sessions or connections.
20
+ #
21
+ # If a change directory command is executed with a relative path, like for instance WebDAV.cd("../collection"), it will
22
+ # raise an exception if current working url has not been set. First a change directory command is executed it should
23
+ # use an absolute URL, like for instance WebDAV.cd("https://www.webdav.org/collection/").
24
+ #
25
+ # If using an URL with a server name not found in the ~/.netrc file, instructions for adding
26
+ # the necessary servername, username and password will be printed to stdout.
27
+ #
28
+ module WebDAV
29
+
30
+ # :stopdoc:
31
+ VERSION = '0.0.4'
32
+ # :startdoc:
33
+
34
+ # Returns the version string for the library.
35
+ #
36
+ def self.version
37
+ VERSION
38
+ end
39
+
40
+ # Returns current working url.
41
+ def self.CWURL
42
+ return $CWURL if($CWURL)
43
+ cwurl = nil
44
+ filename = DavClient.cwurl_filename
45
+ if(File.exists?(filename))
46
+ File.open(filename, 'r') {|f| cwurl = f.read() }
47
+ end
48
+ return cwurl
49
+ end
50
+
51
+ # Make relative url absolute. Returns error if no current working
52
+ # url has been set.
53
+ #
54
+ # Example:
55
+ #
56
+ # WebDAV.cd("https://example.org/subfolder")
57
+ # print WebDAV.absoluteUrl("..") => "https://example.org/"
58
+ def self.absoluteUrl(url)
59
+ if(not(url =~ /^http.?:\/\//))then
60
+ cwurl = Pathname.new(self.CWURL)
61
+ cwurl = cwurl + url
62
+ url = cwurl.to_s
63
+ # url = url + "/" if(not(url =~ /\/$/))
64
+
65
+ if(not(url =~ /^http.?:\/\//))then
66
+ raise "illegal url: " + url
67
+ end
68
+ end
69
+ return url
70
+ end
71
+
72
+ # Boolean function that returns true if url is a collection
73
+ #
74
+ # Example:
75
+ #
76
+ # WebDAV.isCollection("../test.html") => false
77
+ def self.isCollection?(url)
78
+ url = absoluteUrl(url)
79
+ url = url + "/" if(not(url =~ /\/$/))
80
+ resource = WebDAV.propfind(url)
81
+ if(resource == nil)
82
+ return false
83
+ else
84
+ return resource.isCollection?
85
+ end
86
+ end
87
+
88
+ # Change current working url. Takes relative pathnames.
89
+ # First time this method is called, an absolute URL
90
+ # must be used. Raises exception if servername is not
91
+ # found in ~/.netrc file.
92
+ #
93
+ # Examples:
94
+ #
95
+ # WebDAV.cd("http://www.example.org")
96
+ #
97
+ # WebDAV.cd("../folder")
98
+ def self.cd(url)
99
+ url = absoluteUrl(url)
100
+ url = url + "/" if(not(url =~ /\/$/))
101
+ resource = WebDAV.propfind(url)
102
+ if(resource and resource.isCollection?)then
103
+ WebDAV.CWURL = url
104
+ else
105
+ raise Exception, "#{$0} cd: #{url}: No such collection on remote server."
106
+ end
107
+ end
108
+
109
+ # Sets current working url.
110
+ #
111
+ # Current working url is stored in aa tempfile with parent process pid
112
+ # as part of the filename.
113
+ def self.CWURL=(url)
114
+ $CWURL = url
115
+ File.open(DavClient.cwurl_filename, 'w') {|f| f.write(url) }
116
+ # puts "DEBUG: writing " + DavClient.cwurl_filename
117
+ end
118
+
119
+
120
+ # Get content of resource as string
121
+ #
122
+ # Example:
123
+ #
124
+ # html = WebDAV.get(url)
125
+ #
126
+ # html = WebDAV.get("file_in_current_working_folder.html")
127
+ def self.get(url)
128
+ url = absoluteUrl(url)
129
+
130
+ curl_command = url
131
+ response = DavClient.exec_curl(curl_command)
132
+ return response
133
+ end
134
+
135
+ # Set WebDAV properties for url as xml.
136
+ #
137
+ # Example:
138
+ #
139
+ # WebDAV.proppatch("https://dav.webdav.org/folder","<contentLastModified>2007-12-12 12:00:00 GMT</contentLastModified>
140
+ def self.proppatch(url, property)
141
+ url = absoluteUrl(url)
142
+ curl_command = CURL_PROPPATCH + " \""+url+"\""
143
+ curl_command = curl_command.gsub("<!--property-and-value-->",property)
144
+ response = DavClient.exec_curl(curl_command)
145
+ headers = response[:head]
146
+ response = response[:body]
147
+
148
+ if(not(headers =~ /200/)) then
149
+ puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
150
+ exit(0)
151
+ end
152
+ end
153
+
154
+ # Get WebDAV properties
155
+ #
156
+ # Examples:
157
+ #
158
+ # item = propfind(url) - Returns an Hpricot::Elem object
159
+ #
160
+ # xml = propfind(url, :xml => true) - Returns xml for debugging.
161
+ def self.propfind(*args)
162
+ url = args[0]
163
+ url = absoluteUrl(url)
164
+ options = args[1]
165
+
166
+ curl_command = CURL_PROPFIND + " \"" + url + "\""
167
+ response = DavClient.exec_curl(curl_command)
168
+ headers = response[:head]
169
+ response = response[:body]
170
+
171
+
172
+ return nil if response == ""
173
+
174
+ if headers.match(/404/) then
175
+ return nil
176
+ elsif headers.match(/301/) then
177
+ return nil
178
+ elsif headers.match(/207/) then
179
+ return parse_xml_response(response)
180
+ elsif headers.match(/200/) then
181
+ # puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
182
+ # exit(0)
183
+ raise "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
184
+ end
185
+
186
+ if(options and options[:xml])then
187
+ return response
188
+ end
189
+
190
+ doc = Hpricot( response )
191
+ items_filtered = Array.new()
192
+ items = doc.search("//d:response").reverse
193
+ items.each do |item|
194
+
195
+ # Only return root item if folder
196
+ if(item.href == url or item.href == url + "/" ) then
197
+ return item
198
+ end
199
+ end
200
+ return nil
201
+ end
202
+
203
+ def self.parse_xml_response(xml)
204
+ #doc = Hpricot( xml )
205
+ #items_filtered = []
206
+ #items = doc.search("//d:response").reverse
207
+ #items.collect {|item| item.href }
208
+ xml
209
+ end
210
+
211
+ # Find files and folders.
212
+ #
213
+ # Examples:
214
+ #
215
+ # result = find( url [:type => "collection"|"resource"] [:recursive => true])
216
+ #
217
+ # result = find( url, :type => "collection" ,:recursive => true)
218
+ #
219
+ # You can also pass a block of code:
220
+ #
221
+ # find( url, :type => "collection" ,:recursive => true) do |folder|
222
+ # puts folder.href
223
+ # end
224
+ #
225
+ # If no url is specified, current working url is used.
226
+ #
227
+ # cd("https://webdav.org")
228
+ # find() do |item|
229
+ # print item.title
230
+ # end
231
+ def self.find(*args, &block)
232
+
233
+ if(args.size == 0)
234
+ href = self.CWURL
235
+ elsif(args.size == 1)
236
+ if(args[0].class == String)
237
+ href = args[0]
238
+ else
239
+ options = args[0]
240
+ end
241
+ elsif(args.size == 2)
242
+ href = args[0]
243
+ options = args[1]
244
+ else
245
+ raise "Illegal number of arguments."
246
+ end
247
+
248
+ if(href == nil )
249
+ if(WebDAV.CWURL == nil)
250
+ raise "no current working url set"
251
+ else
252
+ href = WebDAV.CWURL
253
+ end
254
+ end
255
+
256
+ type = nil
257
+ recursive = false
258
+ if(options)then
259
+
260
+ if(options[:type])then
261
+ type = options[:type]
262
+ end
263
+ if(options[:recursive])then
264
+ recursive = options[:recursive]
265
+ end
266
+ end
267
+ begin
268
+ dav_xml_output = propfind(href, :xml => true)
269
+ rescue Exception => exception
270
+ $stderr.puts "Warning: " + href + " : " + exception
271
+ # raise "er
272
+ return nil
273
+ end
274
+ if(not(dav_xml_output))then
275
+ return nil
276
+ end
277
+
278
+ doc = Hpricot( dav_xml_output )
279
+ items_filtered = Array.new()
280
+ items = doc.search("//d:response").reverse
281
+
282
+ # TODO: handle recursive
283
+
284
+ return items.collect do |item|
285
+ {
286
+ :href => item.href,
287
+ :content_type => item.getcontenttype,
288
+ :resource_type => item.resourcetype
289
+ }
290
+ end
291
+
292
+ # I Don't think we need the below
293
+ =begin
294
+ # filter items
295
+ items.each do |item|
296
+
297
+ # Ignore info about root item (file or folder)
298
+ if(item.href != href) then
299
+
300
+ if(type == nil)then
301
+ # No filters
302
+ items_filtered.push(item)
303
+ if(block) then
304
+ yield item
305
+ end
306
+
307
+ else
308
+ # Filter result set
309
+ if((type == "collection" or type == "folder" or type == "directory") and item.isCollection? )then
310
+ items_filtered.push(item)
311
+ if(block) then
312
+ yield item
313
+ end
314
+ end
315
+
316
+ # TODO BUG: Item is not yielded if :type => "resource" is given...
317
+ # puts "DEBUG: " + type + " " + item.isCollection?.to_s
318
+ if( (type == "file" or type == "resource") and item.isCollection? == false )then
319
+ items_filtered.push(item)
320
+ if(block) then
321
+ yield item
322
+ end
323
+ end
324
+ end
325
+
326
+ end
327
+ end
328
+ =end
329
+
330
+ if(recursive) then
331
+ items_filtered.each do |item|
332
+ if(item.collection && item.href != args[0])then
333
+ result = find(item.href, args[1], &block)
334
+ if(result != nil)
335
+ items_filtered.concat( result)
336
+ end
337
+ end
338
+ end
339
+ end
340
+
341
+ return items_filtered
342
+ end
343
+
344
+ # Make collection
345
+ # Accepts relative url's
346
+ def self.mkcol(*args) # url, props)
347
+ url = args[0]
348
+ props = args[3]
349
+ url = absoluteUrl(url)
350
+ curl_command = CURL_MKCOL + " " + url
351
+ resp = DavClient.exec_curl(curl_command)
352
+ headers = resp[:head]
353
+ response = resp[:body]
354
+
355
+ return true if headers.to_s.match(/201/)
356
+
357
+ if(props) then
358
+ proppatch(url,props)
359
+ end
360
+ if(response =~ />Created</)then
361
+ return true
362
+ end
363
+ return response
364
+ end
365
+
366
+ # Returns true if resource exists
367
+ def self.exists?(url)
368
+ url = absoluteUrl(url)
369
+ props = WebDAV.propfind(url)
370
+ props.size > 0 unless props.nil?
371
+ end
372
+
373
+ # Copy resources
374
+ #
375
+ # Examples:
376
+ #
377
+ # WebDAV.cp("src.html","https://example.org/destination/destination.html"
378
+ def self.cp(src,dest)
379
+ srcUrl = absoluteUrl(src)
380
+ destUrl = absoluteUrl(dest)
381
+
382
+ # puts "DEBUG: " + srcUrl + " => " + destUrl
383
+ curl_command = CURL_COPY.sub("<!--destination-->", destUrl) + " " + srcUrl
384
+ response = DavClient.exec_curl(curl_command)
385
+
386
+ if(response == "")then
387
+ return destUrl
388
+ end
389
+ return false
390
+ end
391
+
392
+
393
+ # Move resources
394
+ #
395
+ # Examples:
396
+ #
397
+ # WebDAV.mv("src.html","https://example.org/destination/destination.html"
398
+ def self.mv(src,dest)
399
+ srcUrl = absoluteUrl(src)
400
+ destUrl = absoluteUrl(dest)
401
+
402
+ # puts "DEBUG: " + srcUrl + " => " + destUrl
403
+ curl_command = CURL_MOVE.sub("<!--destination-->", destUrl) + " " + srcUrl
404
+ resp = DavClient.exec_curl(curl_command)
405
+ headers = resp[:head]
406
+ response = resp[:body]
407
+
408
+ if headers.match(/201 Created/)
409
+ true
410
+ elsif headers.to_s.match(/301 Moved Permanently/)
411
+ false
412
+ elsif response == ""
413
+ destUrl
414
+ else
415
+ false
416
+ end
417
+ end
418
+
419
+
420
+ # Delete resource
421
+ #
422
+ # Examples:
423
+ #
424
+ # WebDAV.cd("https://example.org/folder")
425
+ # WebDAV.mkcol("subfolder")
426
+ # WebDAV.delete("subfolder")
427
+ def self.delete(url)
428
+ url = absoluteUrl(url)
429
+
430
+ curl_command = CURL_DELETE + url
431
+ response_data = DavClient.exec_curl(curl_command)
432
+ headers = response_data[:head]
433
+ response = response_data[:body]
434
+
435
+ if(response == "")then
436
+ return url
437
+ end
438
+
439
+ if headers.to_s.match(/204 No Content/)
440
+ return true
441
+ end
442
+
443
+ if(not(response =~ /200 OK/)) then
444
+ puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response
445
+ return false
446
+ end
447
+ return url
448
+ end
449
+
450
+
451
+ # Low level WebDAV publish
452
+ #
453
+ # Example:
454
+ #
455
+ # WebDAV.publish("https://dav.example.org/index.html","<h1>Hello</h1>",nil)
456
+ def self.publish(url, string, props)
457
+ self.put_string(url, string)
458
+ if(props) then
459
+ self.proppatch(url,props)
460
+ end
461
+ end
462
+
463
+
464
+
465
+ # Puts content of string to file on server with url
466
+ #
467
+ # Example:
468
+ #
469
+ # WebDAV.put("https://dav.webdav.org/file.html", "<html><h1>Test</h1></html>"
470
+ def self.put_string(url,str)
471
+ url = absoluteUrl(url)
472
+
473
+ if(url =~ /\/$/)then
474
+ raise "Error: WebDAV.put_html: url can not be a collection (folder)."
475
+ end
476
+
477
+ filename = DavClient.string2tempfile(str)
478
+ put(url,filename)
479
+ end
480
+
481
+
482
+ # Upload local file to webserver
483
+ #
484
+ # Example:
485
+ #
486
+ # WebDAV.put("https://example.org/myfile.html", "myfile.html")
487
+ def self.put(url, file_name)
488
+ url = absoluteUrl(url)
489
+
490
+ curl_command = CURL_UPLOAD + " " + file_name + " " + url
491
+ response = DavClient.exec_curl(curl_command)
492
+ if response.kind_of?(Hash)
493
+ headers = response[:head]
494
+ response = response[:body]
495
+ true if headers.match(/201/)
496
+ end
497
+
498
+ if response.match(/201/) then
499
+ true
500
+ elsif(response != "" and not(response =~ /200 OK/)) then
501
+ raise "Error:\n WebDAV.put: WebDAV Request:\n" + curl_command + "\n\nResponse: " + response
502
+ end
503
+ end
504
+
505
+ # Returns a string with the webservers WebDAV options (PUT, PROPFIND, etc.)
506
+ def self.options(url)
507
+ if(not(url))
508
+ url = self.CWURL
509
+ end
510
+ return DavClient.exec_curl(CURL_OPTIONS + url )
511
+ end
512
+
513
+ end