wmls 0.1.1

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.
Files changed (5) hide show
  1. data/LICENSE +2 -0
  2. data/README +25 -0
  3. data/lib/wmls.rb +176 -0
  4. data/main.rb +94 -0
  5. metadata +59 -0
data/LICENSE ADDED
@@ -0,0 +1,2 @@
1
+ Copyright 2010 Wellstorm Development.
2
+ Dual licensed under the Ruby License, or the GPLv2 or later.
data/README ADDED
@@ -0,0 +1,25 @@
1
+ wmls.rb -- WITSML command line tool in Ruby.
2
+
3
+ Use this script to call GetFromStore, AddToStore, UpdateInStore, or DeleteFromStore on
4
+ a WITSML server.
5
+
6
+ Usage: wmls.rb [options]
7
+ -v, --verbose Run verbosely
8
+ -r, --url url URL of the WITSML service
9
+ -u, --username USER HTTP user name
10
+ -p, --password PASS HTTP password
11
+ -q, --query QUERYFILE Path to file containing query, delete, add or update template
12
+ -a get|add|update|delete WITSML action; default is 'get'
13
+ --action
14
+ -h, --help Show this message
15
+
16
+ Example:
17
+ ruby wmls.rb -q query_v1311/get_all_wells.xml -r https://witsml.wellstorm.com/witsml/services/store -u username -p mypassword -a get
18
+
19
+ I've included a bunch of sample query templates originally created by Gary Masters of Energistics.
20
+
21
+ License: Apache 2.0
22
+
23
+ History:
24
+
25
+ 10 Mar 2011 -- initial commit.
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env ruby -w
2
+ # Copyright 2011 Wellstorm Development LLC
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+
17
+ require 'net/http'
18
+ require 'net/https'
19
+ require 'uri'
20
+ require 'stringio'
21
+ require 'rexml/document'
22
+
23
+ class Wmls
24
+
25
+ attr_accessor :timeout
26
+
27
+ def initialize (url, user_name, password)
28
+ @url = url
29
+ @user_name = user_name
30
+ @password = password
31
+
32
+ @optionsIn = ''
33
+ @capabilitiesIn = ''
34
+ @timeout = 60
35
+
36
+ @envelope_begin = <<END
37
+ <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
38
+ xmlns:ns0="http://www.witsml.org/message/120">
39
+ <SOAP-ENV:Header/>
40
+ <SOAP-ENV:Body>
41
+ END
42
+
43
+ @envelope_end = <<END
44
+ </SOAP-ENV:Body>
45
+ </SOAP-ENV:Envelope>
46
+ END
47
+ end
48
+
49
+
50
+
51
+ # Replace special xml chartacters '&' and '<'
52
+ def escape_xml(xml_in)
53
+ return xml_in.gsub(/&/,'&amp;').gsub(/</,'&lt;')
54
+ end
55
+
56
+ def pretty_xml(xml_data)
57
+ s = ''
58
+ doc = REXML::Document.new(xml_data)
59
+ doc.write(s, 2)
60
+ return s
61
+ end
62
+
63
+ # parse the xml and return the singular of the root element name.
64
+ def extract_type(xml_data)
65
+ doc = REXML::Document.new(xml_data)
66
+ plural = doc.root.name
67
+ return plural[0..plural.length-2]
68
+ end
69
+
70
+ #extract the witsml response: status_code and xml_out
71
+ def extract_response(xml_data)
72
+ doc = REXML::Document.new(xml_data)
73
+ r = 0
74
+ x = ''
75
+ s = ''
76
+ doc.root.each_element('//Result') { |elt| r = elt.text}
77
+ doc.root.each_element('//XMLout') { |elt| x = pretty_xml(elt.text)}
78
+ doc.root.each_element('//SuppMsgOut') { |elt| s = elt.text }
79
+ return [r.to_i,s,x];
80
+ end
81
+
82
+
83
+ def post(io, url, user, pass, soap_action)
84
+ url = URI.parse(url) if url.is_a? String
85
+ io = StringIO.new(io) if io.is_a? String
86
+
87
+ req = Net::HTTP::Post.new(url.path)
88
+ req.basic_auth user, pass if user && user.length > 0
89
+ req.body_stream = io
90
+ req.add_field('SOAPAction', soap_action)
91
+ req.content_type = 'application/soap+xml'
92
+ #req.content_length = io.stat.size
93
+ req.content_length = io.size # specific to StringIO class ? why no stat on that class?
94
+ http = Net::HTTP.new(url.host, url.port)
95
+ http.use_ssl = true
96
+ http.read_timeout = @timeout # secs
97
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
98
+ res = http.start {|http2| http2.request(req) }
99
+
100
+ case res
101
+ when Net::HTTPSuccess, Net::HTTPRedirection
102
+ # OK
103
+ res
104
+ else
105
+ res.error!
106
+ end
107
+ end
108
+
109
+
110
+ def send(envelope_middle, soap_action)
111
+ envelope = @envelope_begin + envelope_middle + @envelope_end
112
+ response = post(envelope, @url, @user_name, @password, soap_action)
113
+ status, supp_msg, witsml = extract_response(response.body)
114
+ end
115
+
116
+ def add_to_store(template)
117
+ wmlTypeIn = extract_type(template)
118
+ queryIn = escape_xml(template)
119
+ soap_action = 'http://www.witsml.org/action/120/Store.WMLS_AddToStore'
120
+ envelope_middle = <<END
121
+ <ns0:WMLS_AddToStore SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
122
+ <WMLtypeIn>#{wmlTypeIn}</WMLtypeIn>
123
+ <XMLin>#{queryIn}</XMLin>
124
+ <OptionsIn>#{@optionsIn}</OptionsIn>
125
+ <CapabilitiesIn>#{@capabilitiesIn}</CapabilitiesIn>
126
+ </ns0:WMLS_AddToStore>
127
+ END
128
+ return send envelope_middle, soap_action
129
+ end
130
+
131
+ def delete_from_store(template)
132
+ wmlTypeIn = extract_type(template)
133
+ queryIn = escape_xml(template)
134
+ soap_action = 'http://www.witsml.org/action/120/Store.WMLS_DeleteFromStore'
135
+ envelope_middle = <<END
136
+ <ns0:WMLS_DeleteFromStore SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
137
+ <WMLtypeIn>#{wmlTypeIn}</WMLtypeIn>
138
+ <QueryIn>#{queryIn}</QueryIn>
139
+ <OptionsIn>#{@optionsIn}</OptionsIn>
140
+ <CapabilitiesIn>#{@capabilitiesIn}</CapabilitiesIn>
141
+ </ns0:WMLS_DeleteFromStore>
142
+ END
143
+ return send envelope_middle, soap_action
144
+ end
145
+ def update_in_store(template)
146
+ wmlTypeIn = extract_type(template)
147
+ queryIn = escape_xml(template)
148
+ soap_action = 'http://www.witsml.org/action/120/Store.WMLS_UpdateInStore'
149
+ envelope_middle = <<END
150
+ <ns0:WMLS_UpdateInStore SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
151
+ <WMLtypeIn>#{wmlTypeIn}</WMLtypeIn>
152
+ <XMLin>#{queryIn}</XMLin>
153
+ <OptionsIn>#{@optionsIn}</OptionsIn>
154
+ <CapabilitiesIn>#{@capabilitiesIn}</CapabilitiesIn>
155
+ </ns0:WMLS_UpdateInStore>
156
+ END
157
+ return send envelope_middle, soap_action
158
+ end
159
+
160
+ def get_from_store(template)
161
+ wmlTypeIn = extract_type(template)
162
+ queryIn = escape_xml(template)
163
+ soap_action = 'http://www.witsml.org/action/120/Store.WMLS_GetFromStore'
164
+ envelope_middle = <<END
165
+ <ns0:WMLS_GetFromStore SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
166
+ <WMLtypeIn>#{wmlTypeIn}</WMLtypeIn>
167
+ <QueryIn>#{queryIn}</QueryIn>
168
+ <OptionsIn>#{@optionsIn}</OptionsIn>
169
+ <CapabilitiesIn>#{@capabilitiesIn}</CapabilitiesIn>
170
+ </ns0:WMLS_GetFromStore>
171
+ END
172
+ return send envelope_middle, soap_action
173
+ end
174
+
175
+
176
+ end
data/main.rb ADDED
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby -w
2
+ # Copyright 2011 Wellstorm Development LLC
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'rubygems'
17
+ require 'optparse'
18
+ require 'wmls'
19
+ options = {}
20
+
21
+ opts =OptionParser.new do |o|
22
+ o.banner = "Usage: wmls.rb [options]"
23
+ # o.on("-v", "--verbose", "Run verbosely") do |v|
24
+ # options[:verbose] = v
25
+ # end
26
+ o.on("-r", "--url url", "URL of the WITSML service") do |v|
27
+ options[:url] = v
28
+ end
29
+ o.on("-u", "--username USER", "HTTP user name (optional)") do |v|
30
+ options[:user_name] = v
31
+ end
32
+ o.on("-p", "--password PASS", "HTTP password (optional)") do |v|
33
+ options[:password] = v
34
+ end
35
+ o.on("-q", "--query QUERYFILE", "Path to file containing query, delete, add or update template. (optional, default stdin)") do |v|
36
+ options[:query] = v
37
+ end
38
+ o.on("-a", "--action ACTION", [:add,:get,:update,:delete], "WITSML action: add, get, update, or delete (optional, default 'get')") do |v|
39
+ options[:action] = v || :get
40
+ end
41
+ o.on_tail("-h", "--help", "Show this message") do
42
+ puts o
43
+ exit
44
+ end
45
+ end
46
+
47
+
48
+ # Load the named file and return its contents as a string
49
+ def get_file_as_string(filename)
50
+ data = ''
51
+ if(filename)
52
+ f = File.open(filename, "r")
53
+ else
54
+ f = $stdin
55
+ end
56
+
57
+ f.each_line do |line|
58
+ data += line
59
+ end
60
+ return data
61
+ end
62
+
63
+
64
+ opts.parse!
65
+ if ( !options[:url] )
66
+ puts(opts.help)
67
+ exit 1
68
+ end
69
+
70
+
71
+ template= get_file_as_string(options[:query] )
72
+
73
+ wmls = Wmls.new options[:url], options[:user_name], options[:password]
74
+
75
+ case options[:action]
76
+ when :add
77
+ result = wmls.add_to_store(template)
78
+ when :delete
79
+ result = wmls.delete_from_store(template)
80
+ when :update
81
+ result = wmls.update_in_store(template)
82
+ when :get,nil
83
+ result = wmls.get_from_store(template)
84
+ else
85
+ raise "unsupported action #{options[:action]}"
86
+ end
87
+
88
+
89
+ status, supp_msg, witsml = result
90
+ if (status != 1)
91
+ $stderr.puts "Error #{status}: #{supp_msg}"
92
+ else
93
+ $stdout.puts witsml
94
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wmls
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Hugh Winkler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-29 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Wmls calls GetFromStore, AddToStore, UpdateInStore, or DeleteFromStore on a WITSML server.
18
+ email: hugh.winkler@wellstorm.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - README
27
+ - LICENSE
28
+ - main.rb
29
+ - lib/wmls.rb
30
+ has_rdoc: true
31
+ homepage: https://github.com/wellstorm/wmls.rb/
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.6.2
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Calls GetFromStore, AddToStore, UpdateInStore, or DeleteFromStore on a WITSML server.
58
+ test_files: []
59
+