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,286 @@
1
+ require 'davclient/dav-ls'
2
+ require 'davclient/dav-put'
3
+ require 'davclient/dav-propfind'
4
+
5
+ # DavClient Command Line Utility
6
+
7
+ class DavCLI
8
+
9
+ def self.print_edit_usage
10
+ puts "Usage: #{$0} edit [url|resource-name]"
11
+ puts
12
+ puts "Edit remote file in editor. File is transfered back to "
13
+ puts "server after execution of editor is ended. "
14
+ puts
15
+ end
16
+
17
+ def self.edit(args)
18
+ if(show_help?(args))
19
+ print_edit_usage
20
+ else
21
+ if(args.size == 1)
22
+ url = args[0]
23
+ content = WebDAV.get(url)
24
+ tmp_filename = WebDAV.tmp_folder + File.basename(url)
25
+ File.open(tmp_filename, 'w') {|f| f.write(content) }
26
+ system("emacs --quick -nw " + tmp_filename)
27
+ new_content = nil
28
+ File.open(tmp_filename, 'r') {|f| new_content = f.read() }
29
+ WebDAV.put_string(url, new_content)
30
+ else
31
+ puts "Illegal arguments: " + args[1..100].join(" ")
32
+ print_edit_usage
33
+ end
34
+ end
35
+ end
36
+
37
+ # TODO
38
+ # - Handle glob (ie *.gif) => Tell user to quote to avoid shell glob: dav get "*.html"
39
+ def self.get(args)
40
+
41
+ puts "DEBUG:"
42
+ if(args.size == 1 or args.size == 2 )
43
+ url = args[0]
44
+ content = WebDAV.get(url)
45
+ filename = ""
46
+ if(args.size == 1)
47
+ filename = File.basename(url)
48
+ else
49
+ # Handle relative paths in local filenames or local dir
50
+ path = Pathname.new(Pathname.pwd)
51
+ path = path + args[1]
52
+ filename = path.to_s
53
+ if(args[1] =~ /\/$/ or args[1] =~ /\.$/)
54
+ path = path + filename = filename + "/" + File.basename(url)
55
+ end
56
+ end
57
+ File.open(filename, 'w') {|f| f.write(content) }
58
+ else
59
+ puts "Illegal arguments: " + args[1..100].join(" ")
60
+ puts "#{$0}: usage '#{$0} get remote-url [local]"
61
+ end
62
+ end
63
+
64
+ def self.cat(args)
65
+ if(show_help?(args))
66
+ puts "Usage: #{$0} cat [url|filename]"
67
+ puts
68
+ puts "Concatenate and print remote files to local terminal."
69
+ end
70
+ if(args.size == 1)
71
+ url = args[0]
72
+ puts WebDAV.get(url)
73
+ else
74
+ puts "Illegal arguments: " + args[1..100].join(" ")
75
+ puts "Usage: #{$0} cat [url|filename]"
76
+ end
77
+ end
78
+
79
+ def self.show_help?(args)
80
+ return (args.grep("-?").size > 0 or
81
+ args.grep("-h").size > 0 or
82
+ args.grep("--help").size > 0 )
83
+ end
84
+
85
+ def self.pwd(args)
86
+ if(show_help?(args))
87
+ puts "Usage: #{$0} pwd"
88
+ puts ""
89
+ puts "Print current working url."
90
+ exit
91
+ end
92
+ cwurl = WebDAV.CWURL
93
+ if(cwurl)
94
+ puts cwurl
95
+ else
96
+ puts "#{$0}: No working url set. Use 'dav cd url' to set url"
97
+ end
98
+
99
+ end
100
+
101
+ def self.cd(args)
102
+ if(show_help?(args))
103
+ puts "Usage: #{$0} cd [url|remote-path]"
104
+ puts
105
+ puts "Change current working working url."
106
+ puts
107
+ puts "Examples: "
108
+ puts " #{$0} cd https://www.webdav.org/"
109
+ puts " #{$0} cd ../test"
110
+ puts
111
+ exit
112
+ end
113
+ url = args[0]
114
+ if(url == nil)then
115
+ puts "#{$0} cd: Missing mandatory url."
116
+ exit
117
+ end
118
+ begin
119
+ WebDAV.cd(url)
120
+ puts "Changing WebDAV URL to: " + WebDAV.CWURL
121
+ rescue Exception => exception
122
+ puts exception
123
+ end
124
+ end
125
+
126
+ def self.mkcol(args)
127
+ if(show_help?(args))
128
+ puts "Usage: #{$0} mkcol [url|remote-path]"
129
+ puts
130
+ puts "Create collection (folder) on remote server."
131
+ puts "The command 'mkdir' is an alias for 'mkcol'."
132
+ puts
133
+ puts "Examples: "
134
+ puts " #{$0} mkcol new_collection"
135
+ puts " #{$0} mkcol https://www.webdav.org/new_collection/"
136
+ puts " #{$0} mkcol ../new_collection"
137
+ puts
138
+ exit
139
+ end
140
+ if(args.size == 1 )
141
+ if( args[0] =~ /^http/ || WebDAV.CWURL )
142
+ WebDAV.mkcol(args[0])
143
+ else
144
+ puts "Error: #{$0} mkcol: No working url set. Use '#{$0} cd url' to set url."
145
+ end
146
+ else
147
+ puts "#{$0}: usage '#{$0} mkcol [url|path]"
148
+ end
149
+ end
150
+
151
+ def self.delete(args)
152
+ if(show_help?(args) or args.size != 1)
153
+ puts "Usage: #{$0} delete [url|path]"
154
+ puts
155
+ puts "Delete remote collection (folder) or file."
156
+ else
157
+ url = WebDAV.delete(args[0])
158
+ end
159
+ end
160
+
161
+ def self.options(args)
162
+ if((args.size == 0 or args.size == 1) and !show_help?(args))
163
+ puts WebDAV.options(args[0])
164
+ else
165
+ puts "Usage: #{$0} options [url]"
166
+ puts
167
+ puts "Prints remote server options and http headers. "
168
+ end
169
+ end
170
+
171
+ def self.propfind(args)
172
+ PropfindCLI.propfind(args)
173
+ end
174
+
175
+ def self.ls(args)
176
+ LsCLI.ls(args)
177
+ end
178
+
179
+ def self.cp(args)
180
+ if(args.size == 2 )
181
+ WebDAV.cp(args[0], args[1])
182
+ else
183
+ puts "Usage '#{$0} cp src dest"
184
+ puts
185
+ puts "Copy resources on remote server."
186
+ end
187
+ end
188
+
189
+
190
+ def self.mv(args)
191
+ if(args.size == 2 )
192
+ WebDAV.mv(args[0], args[1])
193
+ else
194
+ puts "Usage '#{$0} copy mv dest"
195
+ puts
196
+ puts "Move resources on remote server."
197
+ end
198
+ end
199
+
200
+ def self.print_dav_usage
201
+ puts "usage: #{$0} COMMAND [ARGS]"
202
+ puts ""
203
+ puts "Available #{$0} commands:"
204
+ puts " ls List files on webdav server"
205
+ puts " pwd Print current working url"
206
+ puts " cd Change current working url"
207
+ puts " cp Copy resource"
208
+ puts " mv Move resource"
209
+ puts " rm Remove resource"
210
+ puts " cat Print content of resource"
211
+ puts " mkdir Create remote collection (directory) (mkcol alias)"
212
+ puts " get Download resource"
213
+ puts " put Upload local file"
214
+ puts " propfind Print webdav properties for url"
215
+ puts " mkcol Make collection"
216
+ puts " options Display webservers WebDAV options"
217
+ puts " edit Edit contents of remote file with editor"
218
+ puts ""
219
+ puts "See '#{$0} COMMAND -h' for more information on a specific command."
220
+ exit
221
+ end
222
+
223
+ def self.dav(args)
224
+
225
+ $0 = $0.sub(/.*\//,"").sub(/.rb$/,"")
226
+
227
+ command = args[0]
228
+
229
+ if(args.size == 0 or command == "-h" or command =~ /help/ or command =~ /\?/) then
230
+ print_dav_usage
231
+ end
232
+
233
+ if(command == "-v" or command =~ /version/ ) then
234
+ puts "#{$0} version " + WebDAV.version
235
+ exit
236
+ end
237
+
238
+ args = args[1..100]
239
+ case command
240
+ when "put" then
241
+ PutCLI.put(args)
242
+ when "get" then
243
+ get(args)
244
+ when "edit" then
245
+ edit(args)
246
+ when "cat" then
247
+ cat(args)
248
+ when "ls" then
249
+ LsCLI.ls(args)
250
+ when "pwd"
251
+ pwd(args)
252
+ when "cd"
253
+ cd(args)
254
+ when "cp"
255
+ cp(args)
256
+ when "copy"
257
+ cp(args)
258
+ when "mv"
259
+ mv(args)
260
+ when "move"
261
+ mv(args)
262
+ when "mkcol"
263
+ mkcol(args)
264
+ when "mkdir"
265
+ mkcol(args)
266
+ when "put"
267
+ PutCLI.put(args)
268
+ when "delete"
269
+ delete(args)
270
+ when "del"
271
+ delete(args)
272
+ when "rm"
273
+ delete(args)
274
+ when "propfind"
275
+ propfind(args)
276
+ when "props"
277
+ propfind(args)
278
+ when "options"
279
+ options(args)
280
+ else
281
+ puts "Unknown command :'" + command + "'"
282
+ print_dav_usage
283
+ end
284
+ end
285
+
286
+ end
@@ -0,0 +1,149 @@
1
+ # -*- coding: utf-8 -*-
2
+ # require 'rubygems'
3
+ require 'hpricot'
4
+ require 'davclient'
5
+ require 'davclient/hpricot_extensions'
6
+
7
+ # Extensions to the Hpricot XML parser.
8
+ module Hpricot
9
+
10
+ class Elem
11
+
12
+ # Makes properties available as simple method calls.
13
+ #
14
+ # Example:
15
+ # print item.creationdate()
16
+ def method_missing(method_name, *args)
17
+ if(args.size == 0) then
18
+ return property(method_name.to_s)
19
+ end
20
+ raise "Method missing"
21
+ end
22
+
23
+ # Resource url
24
+ def href()
25
+ self.at("d:href").innerText
26
+ end
27
+
28
+ # Returns true of resource is a collection, i.e. a folder and not a file.
29
+ def isCollection?()
30
+ self.at("d:collection") != nil
31
+ end
32
+
33
+ end
34
+
35
+
36
+ # Get content from resources on server
37
+ # Example:
38
+ # webpage = WebDAV.find("http://example.org/index.html")
39
+ # print "html src: " + page.content
40
+ def content
41
+ if(!isCollection?)
42
+ WebDAV.get(self.at("d:href").innerText)
43
+ end
44
+ end
45
+
46
+ def content=(string)
47
+ if(!isCollection?)
48
+ WebDAV.put_string(href,string)
49
+ end
50
+ end
51
+
52
+ # Get property for resource or collection.
53
+ # Example:
54
+ # page = WebDAV.find(url)
55
+ # print page.property("published-date")
56
+ def property(name)
57
+
58
+ # TODO: Make list of recognized namespace prefixes configurable
59
+
60
+ property = property = self.at(name)
61
+ if(property)then
62
+ returnValue = property.innerText
63
+ return returnValue
64
+ end
65
+
66
+ property = property = self.at(name.downcase)
67
+ if(property)then
68
+ return property.innerText
69
+ end
70
+
71
+ vrtx_property = self.at("v:" + name)
72
+ if(vrtx_property)then
73
+ return vrtx_property.innerText
74
+ end
75
+
76
+ vrtx_property = self.at("v:" + name.downcase)
77
+ if(vrtx_property)then
78
+ return vrtx_property.innerText
79
+ end
80
+
81
+ dav_property = self.at("d:" +name)
82
+ if( dav_property)then
83
+ return dav_property.innerText
84
+ end
85
+
86
+ dav_property = self.at("d:" +name.downcase)
87
+ if( dav_property)then
88
+ return dav_property.innerText
89
+ end
90
+
91
+ return nil
92
+ end
93
+
94
+ def basename
95
+ File.basename(self.at("d:href").innerText)
96
+ end
97
+
98
+
99
+ # Set the items WebDAV properties. Properties must be a string with XML.
100
+ # Example:
101
+ #
102
+ # find(url) do |item|
103
+ # if(item.href =~ /html$/) then
104
+ # item.proppatch("<d:getcontenttype>text/html</d:getcontenttype>")
105
+ # end
106
+ # end
107
+ def proppatch(properties)
108
+ WebDAV.proppatch(href, properties)
109
+ end
110
+
111
+ # :stopdoc:
112
+
113
+ # TODO: Move to vortex_lib.rb
114
+ def dateProperty(name)
115
+ date = self.property(name)
116
+ if(date =~ /\dZ$/)then
117
+ # Fix for bug in vortex:
118
+ #
119
+ # Some date properties are in xmlshcema datetime format, but
120
+ # all tough the time seems to be localtime the timezone is
121
+ # specified as Z not CEST. Fix is to set timezone and add
122
+ # 2 hours.
123
+ date = date.gsub(/\dZ$/," CEST")
124
+ time = Time.parse(date)
125
+ time = time + (60 * 60 * 2)
126
+ return time
127
+ end
128
+ time = Time.parse(date)
129
+ return time
130
+ end
131
+
132
+
133
+ # TODO Not used. Delete???
134
+ def type_convert_value(value)
135
+ if(returnValue == "true")then
136
+ return true
137
+ end
138
+ if(returnValue == "false")then
139
+ return false
140
+ end
141
+ # Number format???
142
+ ## Dato format
143
+ return returnValue
144
+ end
145
+
146
+
147
+ # :startdoc:
148
+
149
+ end
@@ -0,0 +1,44 @@
1
+ # A minimalistic API for interacting with WebDAV servers.
2
+ #
3
+ # If commands needs to Simple WebDAV API for use in scripts where namespacing is not necessary.
4
+
5
+ # :stopdoc:
6
+
7
+ require 'davclient'
8
+ require 'davclient/dav-ls'
9
+
10
+ # :startdoc:
11
+
12
+ # Change working directory.
13
+ #
14
+ # Examples:
15
+ #
16
+ # require 'davclient/simple'
17
+ #
18
+ # cd("https://example.webdav.org/collection/")
19
+ # content = get("index.html")
20
+ # print content
21
+ def cd(args)
22
+ WebDAV.cd(args)
23
+ end
24
+
25
+ def pwd
26
+ WebDAV.CWURL
27
+ end
28
+
29
+ def find(*args, &block)
30
+ WebDAV.find(*args, &block)
31
+ end
32
+
33
+ def ls(*args)
34
+ if(args == [])
35
+ LsCLI.ls([WebDAV.CWURL])
36
+ else
37
+ LsCLI.ls(*args)
38
+ end
39
+ end
40
+
41
+ def get(args)
42
+ WebDAV.get(args)
43
+ end
44
+