bcl 0.2.3 → 0.2.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.
@@ -27,7 +27,9 @@ require 'base64'
27
27
  # required gems
28
28
  require 'json/pure'
29
29
  require 'bcl/tar_ball'
30
- require 'rest-client'
30
+ #require 'rest-client'
31
+ require 'net/https'
32
+ require 'libxml'
31
33
 
32
34
  module BCL
33
35
 
@@ -35,10 +37,12 @@ module BCL
35
37
 
36
38
  attr_accessor :config
37
39
  attr_accessor :session
40
+ attr_accessor :http
38
41
 
39
42
  def initialize()
40
43
  @config = nil
41
44
  @session = nil
45
+ @http = nil
42
46
  @api_version = 2.0
43
47
  config_path = File.expand_path('~') + '/.bcl'
44
48
  config_name = 'config.yml'
@@ -68,26 +72,106 @@ module BCL
68
72
  username = @config[:server][:admin_user][:username]
69
73
  password = @config[:server][:admin_user][:password]
70
74
  end
71
-
72
- data = {"username" => username, "password" => password}
73
- res = RestClient.post "#{@config[:server][:url]}/api/user/login", data.to_json, :content_type => :json, :accept => :json
74
-
75
- if res.code == 200
75
+
76
+ #figure out what time to use
77
+ url = @config[:server][:url]
78
+ #look for http vs. https
79
+ if url.include? "https"
80
+ port = 443
81
+ else
82
+ port = 80
83
+ end
84
+ #strip out http(s)
85
+ url = url.gsub('http://', '')
86
+ url = url.gsub('https://', '')
87
+
88
+ puts "Connecting to #{url} on port #{port}"
89
+
90
+ @http = Net::HTTP.new(url, port)
91
+ @http.use_ssl = true
92
+
93
+ data = %Q({"username":"#{username}","password":"#{password}"})
94
+ #data = {"username" => username, "password" => password}
95
+
96
+ path = "/api/user/login.json?"
97
+ headers = {'Content-Type' => 'application/json'}
98
+
99
+ res, data = @http.post(path, data, headers)
100
+
101
+ #restClient wasn't working
102
+ #res = RestClient.post "#{@config[:server][:url]}/api/user/login", data.to_json, :content_type => :json, :accept => :json
103
+
104
+ if res.code == '200'
105
+
106
+ =begin
107
+ #OLD RESTCLIENT CODE
76
108
  #pull out the session key
77
109
  res_j = JSON.parse(res.body)
78
-
79
110
  sessid = res_j["sessid"]
80
111
  session_name = res_j["session_name"]
81
112
 
82
- @session = { session_name => sessid }
83
-
113
+ puts "**** RETURNED COOKIES: #{res.cookies.inspect}"
114
+ #pull out the BNES key and BNI key
115
+ bnes_name = ""
116
+ bnesid = ""
117
+ bni_name = ""
118
+ bni_id = ""
119
+ junkout = res.cookies
120
+ junkout.each do |key, val|
121
+ if key.include?("BNES_SESS")
122
+ bnes_name = key.to_s
123
+ bnesid = val.to_s
124
+ end
125
+ end
126
+ junkout.each do |key, val|
127
+ if key.include?("BNI_bcl")
128
+ bni_name = key.to_s
129
+ bni_id = val.to_s
130
+ end
131
+ end
132
+ #@session = { session_name => sessid, bnes_name => bnesid }
133
+ #@session = {session_name => URI.unescape(sessid), bnes_name => URI.unescape(bnesid)}
134
+ @session = {session_name => sessid,bnes_name => bnesid,bni_name => bni_id}
135
+
136
+ =end
137
+
138
+ bnes = ""
139
+ bni = ""
140
+ junkout = res["set-cookie"].split(";")
141
+ junkout.each do |line|
142
+ if line =~ /BNES_SESS/
143
+ bnes = line.match(/(BNES_SESS.*)/)[0]
144
+ end
145
+ end
146
+
147
+ junkout.each do |line|
148
+ if line =~ /BNI/
149
+ bni = line.match(/(BNI.*)/)[0]
150
+ end
151
+ end
152
+
153
+ #puts "DATA: #{data}"
154
+ session_name = ""
155
+ sessid = ""
156
+ json = JSON.parse(data)
157
+ json.each do |key, val|
158
+ if key == 'session_name'
159
+ session_name = val
160
+ elsif key == 'sessid'
161
+ sessid = val
162
+ end
163
+ end
164
+
165
+ @session = session_name + '=' + sessid + ';' + bni + ";" + bnes
166
+
167
+ puts "SESSION COOKIE: #{@session}"
84
168
  end
85
-
169
+
86
170
  res
87
171
  end
88
172
 
89
173
 
90
- # pushes component to the bcl and sets to published (for now). Username and password and
174
+ # pushes component to the bcl and publishes them (if updated as admin user). Username and password and
91
175
  # set in ~/.bcl/config.yml file which determines the permissions and the group to which
92
176
  # the component will be uploaded
93
177
  def push_content(filename_and_path, write_receipt_file, content_type)
@@ -108,26 +192,34 @@ module BCL
108
192
  },
109
193
  "node" =>
110
194
  {
111
- "type" => "#{content_type}"#,
112
- #"status" => 1 #NOTE THIS ONLY WORKS IF YOU ARE ADMIN
195
+ "type" => "#{content_type}",
196
+ "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE ADMIN
113
197
  }
114
198
  }
115
-
116
- res = RestClient.post "#{@config[:server][:url]}/api/content", @data.to_json, :content_type => :json, :cookies => @session, :accept => :json
117
-
118
- if res.code == 200
199
+
200
+ #restclient not working
201
+ #res = RestClient.post "#{@config[:server][:url]}/api/content.json", @data.to_json, :content_type => :json, :cookies => @session
202
+
203
+ path = "/api/content.json"
204
+ headers = {'Content-Type' => 'application/json', 'Cookie' => @session}
205
+
206
+ res, data = @http.post(path, @data.to_json, headers)
207
+
208
+ if res.code == '200'
119
209
  res_j = JSON.parse(res.body)
120
210
 
121
- if res.code == 200
211
+ if res.code == '200'
122
212
  valid = true
123
- elsif res.code == 500
213
+ elsif res.code == '500'
124
214
  raise "server exception"
125
215
  valid = false
126
216
  else
127
217
  valid = false
218
+ puts "error #{res.code}"
128
219
  end
129
220
  else
130
- res = nil
221
+ puts "error code: #{res.code}"
222
+ res = nil
131
223
  end
132
224
 
133
225
  if valid
@@ -161,6 +253,105 @@ module BCL
161
253
 
162
254
  logs
163
255
  end
256
+
257
+ # pushes updated component to the bcl and publishes them (if updated as admin user). Username and password and
258
+ # set in ~/.bcl/config.yml file which determines the permissions and the group to which
259
+ # the component will be uploaded
260
+ def update_content(filename_and_path, write_receipt_file, uuid)
261
+ raise "Please login before pushing components" if @session.nil?
262
+
263
+ valid = false
264
+ res_j = nil
265
+ filename = File.basename(filename_and_path)
266
+ filepath = File.dirname(filename_and_path) + "/"
267
+
268
+ file = File.open(filename_and_path, 'rb')
269
+ file_b64 = Base64.encode64(file.read)
270
+ @data = {"file" =>
271
+ {
272
+ "file" => "#{file_b64}",
273
+ "filesize" => "#{File.size(filename_and_path)}",
274
+ "filename" => filename
275
+ },
276
+ "node" =>
277
+ {
278
+ "uuid" => "#{uuid}",
279
+ "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE ADMIN
280
+ }
281
+ }
282
+
283
+ #restclient not working
284
+ #res = RestClient.post "#{@config[:server][:url]}/api/content", @data.to_json, :content_type => :json, :cookies => @session, :accept => :json
285
+
286
+ path = "/api/content.json"
287
+ headers = {'Content-Type' => 'application/json', 'Cookie' => @session}
288
+
289
+ res, data = @http.post(path, @data.to_json, headers)
290
+
291
+ if res.code == '200'
292
+ res_j = JSON.parse(res.body)
293
+
294
+ if res.code == '200'
295
+ valid = true
296
+ elsif res.code == '500'
297
+ raise "server exception"
298
+ valid = false
299
+ else
300
+ valid = false
301
+ end
302
+ else
303
+ puts "error code: #{res.code}"
304
+ res = nil
305
+ end
306
+
307
+ if valid
308
+ #write out a receipt file into the same directory of the component with the same file name as
309
+ #the component
310
+ if write_receipt_file
311
+ File.open(filepath + File.basename(filename, '.tar.gz') + ".receipt", 'w') do |file|
312
+ file << Time.now.to_s
313
+ end
314
+ end
315
+ end
316
+
317
+ [valid, res_j]
318
+ end
319
+
320
+ def update_contents(array_of_components, skip_files_with_receipts)
321
+ logs = []
322
+ array_of_components.each do |comp|
323
+ receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
324
+ log_message = ""
325
+ if skip_files_with_receipts && File.exists?(receipt_file)
326
+ log_message = "skipping component because found receipt for #{comp}"
327
+ else
328
+ #extract uuid
329
+ if File.exists?(File.dirname(comp) + "/component.xml")
330
+ xml_filename = File.dirname(comp) + "/component.xml"
331
+ elsif File.exists?("#{Dir.pwd}/instances/#{File.basename(comp, '.tar.gz')}/measure.xml")
332
+ xml_filename = "#{Dir.pwd}/instances/#{File.basename(comp, '.tar.gz')}/measure.xml"
333
+ else
334
+ puts "could not find component.xml or measure.xml"
335
+ next
336
+ end
337
+ parser = LibXML::XML::Parser.file(xml_filename)
338
+ xml_file = parser.parse
339
+ uid_node = xml_file.find('uid').first
340
+ uuid = uid_node.content
341
+ if uuid == nil
342
+ log_message "ERROR: uuid not found for #{File.basename(comp)}"
343
+ puts log_message
344
+ else
345
+ log_message = "pushing updated component #{uuid}: #{File.basename(comp)}"
346
+ puts log_message
347
+ valid, res = update_content(comp, true, uuid)
348
+ log_message += " #{valid} #{res.inspect.chomp}"
349
+ end
350
+ end
351
+ logs << log_message
352
+ end
353
+ logs
354
+ end
164
355
 
165
356
  # Simple method to search bcl and return the result as an XML object
166
357
  def search(search_str)
@@ -170,12 +361,24 @@ module BCL
170
361
 
171
362
  xml
172
363
  end
364
+
365
+ # Delete receipt files
366
+ def delete_receipts(array_of_components)
367
+ array_of_components.each do |comp|
368
+ receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
369
+ if File.exists?(receipt_file)
370
+ FileUtils.remove_file(receipt_file)
371
+
372
+ end
373
+ end
374
+ end
375
+
173
376
 
174
377
  end
175
378
 
176
379
  # TODO make this extend the component_xml class (or create a super class around components)
177
380
 
178
- def BCL.gather_components(component_dir, chunk_size = 0, delete_previous_gather = false)
381
+ def BCL.gather_components(component_dir, chunk_size = 0, delete_previousgather = false)
179
382
  @dest_filename = "components"
180
383
  @dest_file_ext = "tar.gz"
181
384
 
@@ -189,7 +392,7 @@ module BCL
189
392
  Dir.chdir(component_dir)
190
393
 
191
394
  # delete any old versions of the component chunks
192
- FileUtils.rm_rf("./_gather") if delete_previous_gather
395
+ FileUtils.rm_rf("./gather") if delete_previousgather
193
396
 
194
397
  #gather all the components into array
195
398
  targzs = Pathname.glob("./**/*.tar.gz")
@@ -201,29 +404,30 @@ module BCL
201
404
  end
202
405
  tar_cnt += 1
203
406
 
204
- destination_path = "./_gather/#{chunk_cnt}"
407
+ destination_path = "./gather/#{chunk_cnt}"
205
408
  FileUtils.mkdir_p(destination_path)
206
409
  destination_file = "#{destination_path}/#{File.basename(targz.to_s)}"
207
- puts "copying #{targz.to_s} to #{destination_file}"
410
+ #puts "copying #{targz.to_s} to #{destination_file}"
208
411
  FileUtils.cp(targz.to_s, destination_file)
209
412
  end
210
413
 
211
414
  #gather all the zip files into a single tar.gz
212
415
  (1..chunk_cnt).each do |cnt|
213
416
  currentdir = Dir.pwd
214
-
417
+
215
418
  paths = []
216
- Pathname.glob("./_gather/#{cnt}/*.tar.gz").each do |pt|
419
+ Pathname.glob("./gather/#{cnt}/*.tar.gz").each do |pt|
217
420
  paths << File.basename(pt.to_s)
218
421
  end
219
422
 
220
- Dir.chdir("./_gather/#{cnt}")
423
+ Dir.chdir("./gather/#{cnt}")
221
424
  destination = "#{@dest_filename}_#{cnt}.#{@dest_file_ext}"
425
+ puts "tarring batch #{cnt} of #{chunk_cnt} to #{@dest_filename}_#{cnt}.#{@dest_file_ext}"
222
426
  BCL.tarball(destination, paths)
223
427
  Dir.chdir(currentdir)
224
428
 
225
429
  #move the tarball back a directory
226
- FileUtils.move("./_gather/#{cnt}/#{destination}", "./_gather/#{destination}")
430
+ FileUtils.move("./gather/#{cnt}/#{destination}", "./gather/#{destination}")
227
431
  end
228
432
 
229
433
  Dir.chdir(current_dir)
@@ -103,17 +103,17 @@ else # if $have_win32ole
103
103
 
104
104
  end # if $have_win32ole
105
105
 
106
- def save(save_path)
106
+ def save(save_path, chunk_size = 1000, delete_old_gather = false)
107
107
 
108
108
  # load master taxonomy to validate components
109
109
  taxonomy = BCL::MasterTaxonomy.new
110
110
 
111
- FileUtils.rm_rf(save_path) if File.exists?(save_path) and File.directory?(save_path)
111
+ #FileUtils.rm_rf(save_path) if File.exists?(save_path) and File.directory?(save_path)
112
112
 
113
113
  @worksheets.each do |worksheet|
114
114
  worksheet.components.each do |component|
115
115
 
116
- component_xml = Component.new(save_path)
116
+ component_xml = Component.new("#{save_path}/components")
117
117
  component_xml.name = component.name
118
118
  component_xml.uid = component.uid
119
119
  component_xml.comp_version_id = component.version_id
@@ -202,7 +202,7 @@ end # if $have_win32ole
202
202
 
203
203
  end
204
204
 
205
- BCL.gather_components(save_path)
205
+ BCL.gather_components(save_path,chunk_size,delete_old_gather)
206
206
 
207
207
  end
208
208
 
@@ -278,7 +278,6 @@ end # if $have_win32ole
278
278
  if component.uid.nil? or component.uid.empty?
279
279
  component.uid = UUID.new.generate
280
280
  xlsx_worksheet.Range("B#{i}").value = component.uid
281
- exit
282
281
  end
283
282
 
284
283
  # always write new version id
@@ -42,7 +42,17 @@ module BCL
42
42
  attr_accessor :source_year
43
43
  attr_accessor :source_url
44
44
  attr_accessor :objects
45
-
45
+ attr_accessor :name
46
+ attr_accessor :uid
47
+ attr_accessor :comp_version_id
48
+ attr_accessor :description
49
+ attr_accessor :fidelity_level
50
+ attr_accessor :source_manufacturer
51
+ attr_accessor :source_model
52
+ attr_accessor :source_serial_no
53
+ attr_accessor :source_year
54
+ attr_accessor :source_url
55
+
46
56
  #the save path is where the component will be saved
47
57
  def initialize(save_path)
48
58
  super(save_path)
@@ -100,8 +110,8 @@ module BCL
100
110
  #the directory
101
111
 
102
112
  Dir.chdir("#{resolve_path}")
103
- destination = "#{@name.gsub(" ","_").gsub(".","").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}.tar.gz"
104
-
113
+ destination = "#{@name.gsub(/\W/,'_').gsub(/___/,'_').gsub(/__/,'_').chomp('_').strip}.tar.gz"
114
+
105
115
  File.delete(destination) if File.exists?(destination)
106
116
 
107
117
  BCL.tarball(destination, paths)
@@ -150,21 +160,21 @@ module BCL
150
160
 
151
161
  def resolve_path
152
162
  FileUtils.mkdir_p(@path) unless File.directory?(@path)
153
- new_path = "#{@path}/#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
163
+ new_path = "#{@path}/#{name.gsub(/\W/,'_').gsub(/___/,'_').gsub(/__/,'_').chomp('_').strip}"
154
164
  FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
155
165
  result = new_path
156
166
  end
157
167
 
158
168
  def osm_resolve_path
159
169
  FileUtils.mkdir_p(@path) unless File.directory?(@path)
160
- new_path = "#{@path}/osm_#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
170
+ new_path = "#{@path}/osm_#{name.gsub(/\W/,'_').gsub(/___/,'_').gsub(/__/,'_').chomp('_').strip}"
161
171
  FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
162
172
  result = new_path
163
173
  end
164
174
 
165
175
  def osc_resolve_path
166
176
  FileUtils.mkdir_p(@path) unless File.directory?(@path)
167
- new_path = "#{@path}/osc_#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
177
+ new_path = "#{@path}/osc_#{name.gsub(/\W/,'_').gsub(/___/,'_').gsub(/__/,'_').chomp('_').strip}"
168
178
  FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
169
179
  result = new_path
170
180
  end
@@ -180,7 +190,7 @@ module BCL
180
190
 
181
191
  def tmp_resolve_path
182
192
  FileUtils.mkdir_p(@path) unless File.directory?(@path)
183
- new_path = "#{@path}/tmp_#{name.gsub(" ","_").gsub("/","_").gsub("-","_").gsub("___","_").gsub("__","_").gsub("in.","in").gsub(",","").strip}"
193
+ new_path = "#{@path}/tmp_#{name.gsub(/\W/,'_').gsub(/___/,'_').gsub(/__/,'_').chomp('_').strip}"
184
194
  FileUtils.mkdir_p(new_path) unless File.directory?(new_path)
185
195
  result = new_path
186
196
  end