bcl 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 310fa6cd807919ce71dd05ca1a6d94f9d0b5a745
4
- data.tar.gz: 14ddfb96035844f235e012657bcf48c7d809f458
3
+ metadata.gz: 10d084384705465f95b8b328994161d836c98c83
4
+ data.tar.gz: ffdd20227e95f5b995f0c1bf0f228af53b287bc3
5
5
  SHA512:
6
- metadata.gz: 58e402c26df46cd04242857ed248138d15b8e4f39f5b7806c7cd8ffadf9eda2036b87d5fdc81d74aa350a08f972796aeadec7dfe8863e9e6aab404539f495cb4
7
- data.tar.gz: 2b6ce437ca4614ae28ad53655162aa243618967e5c5a6146fefca1e1914e7c35c7ccb81cc7129f6dae656fda18ba0877a725927e8b4f2fef5c8f3142d1a643f5
6
+ metadata.gz: 19cfe476429d53381ff893040138977fddd2a9a6aab330f965e012c7dc346ee61624fc522aa13c92e7dd9df1bd16ab5cb9b7cb278d9ef8c68e64ed5920a3f5e6
7
+ data.tar.gz: 55a8758853dd04845cfbcd9fce45187a63970d87c4f734bbca024322346d7ddc9b2051d80620e5a8d7b1448e9bdf369262e62967e2ea69150ea464bc7fdf7e86
@@ -64,15 +64,13 @@ module BCL
64
64
  end
65
65
 
66
66
  def default_yaml
67
- settings = { :server => { :url => "https://bcl.nrel.gov", :user => { :username => "ENTER_BCL_USERNAME", :password => "ENTER_BCL_PASSWORD"} } }
67
+ settings = {:server => {:url => "https://bcl.nrel.gov", :user => {:username => "ENTER_BCL_USERNAME", :password => "ENTER_BCL_PASSWORD"}}}
68
68
 
69
69
  settings
70
70
  end
71
-
72
-
73
- def login(username=nil, password=nil, url=nil)
74
71
 
75
-
72
+
73
+ def login(username=nil, password=nil, url=nil)
76
74
  #figure out what url to use
77
75
  if url.nil?
78
76
  url = @config[:server][:url]
@@ -103,7 +101,7 @@ module BCL
103
101
 
104
102
  data = %Q({"username":"#{username}","password":"#{password}"})
105
103
  #data = {"username" => username, "password" => password}
106
-
104
+
107
105
  login_path = "/api/user/login.json"
108
106
  headers = {'Content-Type' => 'application/json'}
109
107
 
@@ -118,50 +116,19 @@ module BCL
118
116
  #res = RestClient.post "#{@config[:server][:url]}/api/user/login", data.to_json, :content_type => :json, :accept => :json
119
117
  if res.code == '200'
120
118
  puts "Login Successful"
121
- =begin
122
- #OLD RESTCLIENT CODE
123
- #pull out the session key
124
- res_j = JSON.parse(res.body)
125
- sessid = res_j["sessid"]
126
- session_name = res_j["session_name"]
127
-
128
- puts "**** RETURNED COOKIES: #{res.cookies.inspect}"
129
- #pull out the BNES key and BNI key
130
- bnes_name = ""
131
- bnesid = ""
132
- bni_name = ""
133
- bni_id = ""
134
- junkout = res.cookies
135
- junkout.each do |key, val|
136
- if key.include?("BNES_SESS")
137
- bnes_name = key.to_s
138
- bnesid = val.to_s
139
- end
140
- end
141
- junkout.each do |key, val|
142
- if key.include?("BNI_bcl")
143
- bni_name = key.to_s
144
- bni_id = val.to_s
145
- end
146
- end
147
- #@session = { session_name => sessid, bnes_name => bnesid }
148
- #@session = {session_name => URI.unescape(sessid), bnes_name => URI.unescape(bnesid)}
149
- @session = {session_name => sessid,bnes_name => bnesid,bni_name => bni_id}
150
-
151
- =end
152
-
119
+
153
120
  bnes = ""
154
121
  bni = ""
155
122
  junkout = res["set-cookie"].split(";")
156
123
  junkout.each do |line|
157
124
  if line =~ /BNES_SESS/
158
- bnes = line.match(/(BNES_SESS.*)/)[0]
125
+ bnes = line.match(/(BNES_SESS.*)/)[0]
159
126
  end
160
127
  end
161
128
 
162
129
  junkout.each do |line|
163
130
  if line =~ /BNI/
164
- bni = line.match(/(BNI.*)/)[0]
131
+ bni = line.match(/(BNI.*)/)[0]
165
132
  end
166
133
  end
167
134
 
@@ -169,7 +136,7 @@ module BCL
169
136
  session_name = ""
170
137
  sessid = ""
171
138
  json = JSON.parse(res.body)
172
- json.each do |key, val|
139
+ json.each do |key, val|
173
140
  if key == 'session_name'
174
141
  session_name = val
175
142
  elsif key == 'sessid'
@@ -177,13 +144,13 @@ module BCL
177
144
  end
178
145
  end
179
146
 
180
- @session = session_name + '=' + sessid + ';' + bni + ";" + bnes
181
-
147
+ @session = session_name + '=' + sessid + ';' + bni + ";" + bnes
148
+
182
149
  #get access token
183
150
  token_path = "/services/session/token"
184
151
  token_headers = {'Content-Type' => 'application/json', 'Cookie' => @session}
185
152
  #puts "token_headers = #{token_headers.inspect}"
186
- access_token = @http.post(token_path,"",token_headers)
153
+ access_token = @http.post(token_path, "", token_headers)
187
154
  if access_token.code == '200'
188
155
  @access_token = access_token.body
189
156
  else
@@ -191,16 +158,17 @@ module BCL
191
158
  puts "error code: #{access_token.code}"
192
159
  puts "error info: #{access_token.body}"
193
160
  end
194
-
161
+
195
162
  #puts "access_token = *#{@access_token}*"
196
163
  #puts "cookie = #{@session}"
197
-
164
+
198
165
  res
199
166
  else
200
-
167
+
201
168
  puts "error code: #{res.code}"
202
169
  puts "error info: #{res.body}"
203
-
170
+ puts "continuing as unauthenticated sessions (you can still search and download)"
171
+
204
172
  res
205
173
  end
206
174
  end
@@ -219,37 +187,37 @@ module BCL
219
187
  file = File.open(filename_and_path, 'rb')
220
188
  file_b64 = Base64.encode64(file.read)
221
189
  @data = {
222
- "file" =>
223
- {
224
- "file" => "#{file_b64}",
225
- "filesize" => "#{File.size(filename_and_path)}",
226
- "filename" => filename
227
- },
228
- "node" =>
229
- {
230
- "type" => "#{content_type}",
231
- #"field_component_tags" => #TODO remove this field_component_tags once BCL is fixed
232
- # {
233
- # "und" => "1289"
234
- # },
235
- "og_group_ref" =>
236
- {
237
- "und" =>
238
- ["target_id" => @group_id],
239
-
240
- },
241
- "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE A BCL SITE ADMIN
242
- }
243
-
244
- }
190
+ "file" =>
191
+ {
192
+ "file" => "#{file_b64}",
193
+ "filesize" => "#{File.size(filename_and_path)}",
194
+ "filename" => filename
195
+ },
196
+ "node" =>
197
+ {
198
+ "type" => "#{content_type}",
199
+ #"field_component_tags" => #TODO remove this field_component_tags once BCL is fixed
200
+ # {
201
+ # "und" => "1289"
202
+ # },
203
+ "og_group_ref" =>
204
+ {
205
+ "und" =>
206
+ ["target_id" => @group_id],
207
+
208
+ },
209
+ "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE A BCL SITE ADMIN
210
+ }
211
+
212
+ }
245
213
  #restclient not working
246
214
  #res = RestClient.post "#{@config[:server][:url]}/api/content.json", @data.to_json, :content_type => :json, :cookies => @session
247
-
248
- path = "/api/content.json"
249
- headers = {'Content-Type' => 'application/json','X-CSRF-Token' => @access_token, 'Cookie' => @session}
215
+
216
+ path = "/api/content.json"
217
+ headers = {'Content-Type' => 'application/json', 'X-CSRF-Token' => @access_token, 'Cookie' => @session}
250
218
  #puts headers.inspect
251
219
  res = @http.post(path, @data.to_json, headers)
252
-
220
+
253
221
  res_j = "could not get json from http post response"
254
222
  if res.code == '200'
255
223
  res_j = JSON.parse(res.body)
@@ -260,14 +228,14 @@ module BCL
260
228
  puts " 404 - check these common causes first:"
261
229
  puts " the filename contains periods (other than the ones before the file extension)"
262
230
  puts " you are not an 'administrator member' of the group you're trying to upload to"
263
- valid = false
231
+ valid = false
264
232
  elsif res.code == '500'
265
233
  puts " error code: #{res.code} - #{res.body}"
266
234
  raise "server exception"
267
235
  valid = false
268
236
  else
269
237
  puts " error code: #{res.code} - #{res.body}"
270
- valid = false
238
+ valid = false
271
239
  end
272
240
 
273
241
  if valid
@@ -302,7 +270,7 @@ module BCL
302
270
 
303
271
  logs
304
272
  end
305
-
273
+
306
274
  # pushes updated content to the bcl and publishes it (if logged-in as BCL Website Admin user).
307
275
  # username and password set in ~/.bcl/config.yml file
308
276
  def update_content(filename_and_path, write_receipt_file, uuid)
@@ -316,36 +284,36 @@ module BCL
316
284
  file = File.open(filename_and_path, 'rb')
317
285
  file_b64 = Base64.encode64(file.read)
318
286
  @data = {
319
- "file" =>
320
- {
321
- "file" => "#{file_b64}",
322
- "filesize" => "#{File.size(filename_and_path)}",
323
- "filename" => filename
324
- },
325
- "node" =>
326
- {
327
- "uuid" => "#{uuid}",
328
- #"field_component_tags" => #TODO remove this field_component_tags once BCL is fixed
329
- # {
330
- # "und" => "1289"
331
- # },
332
- "og_group_ref" =>
333
- {
334
- "und" =>
335
- ["target_id" => @group_id],
336
- },
337
- "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE A BCL SITE ADMIN
338
- }
287
+ "file" =>
288
+ {
289
+ "file" => "#{file_b64}",
290
+ "filesize" => "#{File.size(filename_and_path)}",
291
+ "filename" => filename
292
+ },
293
+ "node" =>
294
+ {
295
+ "uuid" => "#{uuid}",
296
+ #"field_component_tags" => #TODO remove this field_component_tags once BCL is fixed
297
+ # {
298
+ # "und" => "1289"
299
+ # },
300
+ "og_group_ref" =>
301
+ {
302
+ "und" =>
303
+ ["target_id" => @group_id],
304
+ },
305
+ "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE A BCL SITE ADMIN
339
306
  }
340
-
341
- #restclient not working
342
- #res = RestClient.post "#{@config[:server][:url]}/api/content", @data.to_json, :content_type => :json, :cookies => @session, :accept => :json
307
+ }
343
308
 
344
- path = "/api/content.json"
345
- headers = {'Content-Type' => 'application/json', 'Cookie' => @session, 'X-CSRF-Token' => @access_token}
309
+ #restclient not working
310
+ #res = RestClient.post "#{@config[:server][:url]}/api/content", @data.to_json, :content_type => :json, :cookies => @session, :accept => :json
311
+
312
+ path = "/api/content.json"
313
+ headers = {'Content-Type' => 'application/json', 'Cookie' => @session, 'X-CSRF-Token' => @access_token}
314
+
315
+ res = @http.post(path, @data.to_json, headers)
346
316
 
347
- res = @http.post(path, @data.to_json, headers)
348
-
349
317
  res_j = "could not get json from http post response"
350
318
  if res.code == '200'
351
319
  res_j = JSON.parse(res.body)
@@ -356,15 +324,15 @@ module BCL
356
324
  puts " 404 - check these common causes first:"
357
325
  puts " the filename contains periods (other than the ones before the file extension)"
358
326
  puts " you are not an 'administrator member' of the group you're trying to upload to"
359
- valid = false
327
+ valid = false
360
328
  elsif res.code == '500'
361
329
  puts " error code: #{res.code} - #{res.body}"
362
330
  raise "server exception"
363
331
  valid = false
364
332
  else
365
333
  puts " error code: #{res.code} - #{res.body}"
366
- valid = false
367
- end
334
+ valid = false
335
+ end
368
336
 
369
337
  if valid
370
338
  #write out a receipt file into the same directory of the component with the same file name as
@@ -378,78 +346,93 @@ module BCL
378
346
 
379
347
  [valid, res_j]
380
348
  end
381
-
382
- def update_contents(array_of_components, skip_files_with_receipts)
383
- logs = []
384
- array_of_components.each do |comp|
385
- receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
386
- log_message = ""
387
- if skip_files_with_receipts && File.exists?(receipt_file)
388
- log_message = "skipping update because found receipt #{File.basename(comp)}"
389
- puts log_message
390
- else
391
- #extract uuid from the .tar.gz file
392
- uuid = nil
393
- tgz = Zlib::GzipReader.open(comp)
394
- Archive::Tar::Minitar::Reader.open(tgz).each do |entry|
395
- if entry.name == "component.xml" or entry.name == "measure.xml"
396
- xml_file = LibXML::XML::Document.string(entry.read)
397
- uid_node = xml_file.find('uid').first
398
- uuid = uid_node.content
399
- #vid_node = xml_file.find('version_id').first
400
- #vid = vid_node.content
401
- #puts "uuid = #{uuid}; vid = #{vid}"
402
- end
403
- end
404
- if uuid == nil
405
- log_message = "ERROR: uuid not found for #{File.basename(comp)}"
349
+
350
+ def update_contents(array_of_components, skip_files_with_receipts)
351
+ logs = []
352
+ array_of_components.each do |comp|
353
+ receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
354
+ log_message = ""
355
+ if skip_files_with_receipts && File.exists?(receipt_file)
356
+ log_message = "skipping update because found receipt #{File.basename(comp)}"
406
357
  puts log_message
407
358
  else
408
- log_message = "pushing updated content #{File.basename(comp)}"
409
- puts log_message
410
- valid, res = update_content(comp, true, uuid, @group_id)
411
- log_message += " #{valid} #{res.inspect.chomp}"
359
+ #extract uuid from the .tar.gz file
360
+ uuid = nil
361
+ tgz = Zlib::GzipReader.open(comp)
362
+ Archive::Tar::Minitar::Reader.open(tgz).each do |entry|
363
+ if entry.name == "component.xml" or entry.name == "measure.xml"
364
+ xml_file = LibXML::XML::Document.string(entry.read)
365
+ uid_node = xml_file.find('uid').first
366
+ uuid = uid_node.content
367
+ #vid_node = xml_file.find('version_id').first
368
+ #vid = vid_node.content
369
+ #puts "uuid = #{uuid}; vid = #{vid}"
370
+ end
371
+ end
372
+ if uuid == nil
373
+ log_message = "ERROR: uuid not found for #{File.basename(comp)}"
374
+ puts log_message
375
+ else
376
+ log_message = "pushing updated content #{File.basename(comp)}"
377
+ puts log_message
378
+ valid, res = update_content(comp, true, uuid, @group_id)
379
+ log_message += " #{valid} #{res.inspect.chomp}"
380
+ end
412
381
  end
382
+ logs << log_message
413
383
  end
414
- logs << log_message
415
- end
416
- logs
417
- end
418
-
419
- # Simple method to search bcl and return the result as an XML object
420
- def search(search_str=nil, filter_str=nil)
421
- full_url = "/api/search.json"
422
-
423
- #add search term
424
- if !search_str.nil?
425
- full_url = full_url + "/" + search_str
384
+ logs
426
385
  end
427
- #add api_version
428
- full_url = full_url + "?api_version=#{@api_version}"
429
- #add filters
430
- if !filter_str.nil?
431
- full_url = full_url + "&" + filter_str
386
+
387
+ # Simple method to search bcl and return the result as an XML object
388
+ def search(search_str=nil, filter_str=nil)
389
+ full_url = "/api/search.json"
390
+
391
+ #add search term
392
+ if !search_str.nil?
393
+ full_url = full_url + "/" + search_str
394
+ end
395
+ #add api_version
396
+ full_url = full_url + "?api_version=#{@api_version}"
397
+ #add filters
398
+ if !filter_str.nil?
399
+ full_url = full_url + "&" + filter_str
400
+ end
401
+
402
+ res = @http.get(full_url)
403
+
404
+ #retrieve in json
405
+ res.body
432
406
  end
433
407
 
434
- res = @http.get(full_url)
408
+ # Delete receipt files
409
+ def delete_receipts(array_of_components)
410
+ array_of_components.each do |comp|
411
+ receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
412
+ if File.exists?(receipt_file)
413
+ FileUtils.remove_file(receipt_file)
414
+
415
+ end
416
+ end
417
+ end
435
418
 
436
- #retrieve in json
437
- res.body
438
- end
439
-
440
- # Delete receipt files
441
- def delete_receipts(array_of_components)
442
- array_of_components.each do |comp|
443
- receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
444
- if File.exists?(receipt_file)
445
- FileUtils.remove_file(receipt_file)
419
+ def list_all_measures()
420
+ json = JSON.parse(search(nil, "f[0]=bundle%3Anrel_measure&show_rows=100"), :symbolize_names => true)
446
421
 
447
- end
448
- end
449
- end
450
-
422
+ json
423
+ end
424
+
425
+ def download_component(uid)
426
+ result = @http.get("/api/component/download?uids=#{uid}")
427
+
428
+ #https://bcl.nrel.gov/api/component/download?uids=a667a52f-aa04-4997-9292-c81671d75f84
429
+ result.body ? result.body : nil
430
+ end
431
+
451
432
  end #class ComponentMethods
452
433
 
434
+
435
+
453
436
  # TODO make this extend the component_xml class (or create a super class around components)
454
437
 
455
438
  def BCL.gather_components(component_dir, chunk_size = 0, delete_previousgather = false, destination=nil)
@@ -492,7 +475,7 @@ module BCL
492
475
  #gather all the .tar.gz files into a single tar.gz
493
476
  (1..chunk_cnt).each do |cnt|
494
477
  currentdir = Dir.pwd
495
-
478
+
496
479
  paths = []
497
480
  Pathname.glob("./gather/#{cnt}/*.tar.gz").each do |pt|
498
481
  paths << File.basename(pt.to_s)
data/lib/bcl/tar_ball.rb CHANGED
@@ -19,40 +19,40 @@
19
19
 
20
20
  require 'rubygems'
21
21
 
22
- require 'zlib' #gem install zliby
22
+ require 'zlib' #gem install zliby # need to convert this to another library (this is olllld)
23
23
  require 'archive/tar/minitar' #gem install archive-tar-minitar
24
24
 
25
25
  module BCL
26
26
 
27
- module_function
27
+ module_function
28
28
 
29
- def tarball(destination, paths)
30
-
31
- #check for filepath length limit
32
- full_destination = File.expand_path(destination)
33
- if full_destination.length > 259 #256 chars max; "C:\" doesn't count
34
- puts "[TarBall] ERROR cannot generate #{destination} because path exceeds 256 char limit. shorten component name by at least by #{full_destination.length - 259} chars"
35
- return
36
- end
37
-
38
- Zlib::GzipWriter.open(destination) do |gzip|
39
- out = Archive::Tar::Minitar::Output.new(gzip)
40
-
41
- paths.each do |fi|
42
- if File.exists?(fi)
43
- Archive::Tar::Minitar.pack_file(fi, out)
44
- else
45
- puts "[TarBall] ERROR Could not file file: #{fi}"
29
+ def tarball(destination, paths)
30
+
31
+ #check for filepath length limit
32
+ full_destination = File.expand_path(destination)
33
+ if full_destination.length > 259 #256 chars max; "C:\" doesn't count
34
+ puts "[TarBall] ERROR cannot generate #{destination} because path exceeds 256 char limit. shorten component name by at least by #{full_destination.length - 259} chars"
35
+ return
36
+ end
37
+
38
+ Zlib::GzipWriter.open(destination) do |gzip|
39
+ out = Archive::Tar::Minitar::Output.new(gzip)
40
+
41
+ paths.each do |fi|
42
+ if File.exists?(fi)
43
+ Archive::Tar::Minitar.pack_file(fi, out)
44
+ else
45
+ puts "[TarBall] ERROR Could not file file: #{fi}"
46
+ end
46
47
  end
48
+ out.close
47
49
  end
48
- out.close
49
50
  end
50
- end
51
51
 
52
- def extract_tarball(filename, destination)
53
- Zlib::GzipReader.open(filename) {|gz|
52
+ def extract_tarball(filename, destination)
53
+ Zlib::GzipReader.open(filename) { |gz|
54
54
  Archive::Tar::Minitar.unpack(gz, destination)
55
- }
56
- end
55
+ }
56
+ end
57
57
 
58
- end # module BCL
58
+ end # module BCL
data/lib/bcl/version.rb CHANGED
@@ -18,5 +18,5 @@
18
18
  ######################################################################
19
19
 
20
20
  module BCL
21
- VERSION = "0.3.6"
21
+ VERSION = "0.3.7"
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bcl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Macumber
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-11-15 00:00:00.000000000 Z
14
+ date: 2013-11-19 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: uuid
@@ -111,6 +111,20 @@ dependencies:
111
111
  - - '>='
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0'
114
+ - !ruby/object:Gem::Dependency
115
+ name: libxml-ruby
116
+ requirement: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :runtime
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
114
128
  description: This gem contains helper methods for generating the Component XML file
115
129
  needed to upload files to the Building Component Library. It also contains the classes
116
130
  needed for logging in via the api and uploading generating components
@@ -151,9 +165,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
165
  version: '0'
152
166
  requirements: []
153
167
  rubyforge_project:
154
- rubygems_version: 2.0.3
168
+ rubygems_version: 2.0.2
155
169
  signing_key:
156
170
  specification_version: 4
157
171
  summary: Classes for creating component XML files for the BCL
158
172
  test_files: []
159
- has_rdoc: