bcl 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bcl/base_xml.rb +33 -16
- data/lib/bcl/component.rb +388 -390
- data/lib/bcl/component_from_spreadsheet.rb +34 -37
- data/lib/bcl/component_methods.rb +864 -790
- data/lib/bcl/component_spreadsheet.rb +295 -309
- data/lib/bcl/core_ext.rb +38 -6
- data/lib/bcl/master_taxonomy.rb +533 -552
- data/lib/bcl/tar_ball.rb +78 -80
- data/lib/bcl/version.rb +2 -2
- data/lib/bcl.rb +41 -34
- metadata +84 -14
@@ -1,309 +1,295 @@
|
|
1
|
-
######################################################################
|
2
|
-
# Copyright (c) 2008-
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# This library is free software; you can redistribute it and/or
|
6
|
-
# modify it under the terms of the GNU Lesser General Public
|
7
|
-
# License as published by the Free Software Foundation; either
|
8
|
-
# version 2.1 of the License, or (at your option) any later version.
|
9
|
-
#
|
10
|
-
# This library is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
-
# Lesser General Public License for more details.
|
14
|
-
#
|
15
|
-
# You should have received a copy of the GNU Lesser General Public
|
16
|
-
# License along with this library; if not, write to the Free Software
|
17
|
-
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
-
######################################################################
|
19
|
-
|
20
|
-
# Converts a custom Excel spreadsheet format to BCL components for upload
|
21
|
-
# Format of the Excel spreadsheet is documented in /doc/ComponentSpreadsheet.docx
|
22
|
-
|
23
|
-
if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
|
24
|
-
begin
|
25
|
-
# apparently this is not a gem (todo: need to remove and replace with roo)
|
26
|
-
require 'win32ole'
|
27
|
-
mod = WIN32OLE
|
28
|
-
$have_win32ole = true
|
29
|
-
rescue NameError
|
30
|
-
# do not have win32ole
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
module BCL
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
datetime
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
component_xml.
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
#
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
value2
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
# get
|
278
|
-
component.
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
component.headers = headers
|
297
|
-
component.values = xlsx_worksheet.Range("A#{i}:#{max_col}#{i}").value
|
298
|
-
worksheet.components << component
|
299
|
-
end
|
300
|
-
|
301
|
-
@worksheets << worksheet
|
302
|
-
|
303
|
-
puts "[ComponentSpreadsheet] Finished parsing components of type #{worksheet.name}"
|
304
|
-
|
305
|
-
end
|
306
|
-
|
307
|
-
end
|
308
|
-
|
309
|
-
end # module BCL
|
1
|
+
######################################################################
|
2
|
+
# Copyright (c) 2008-2014, Alliance for Sustainable Energy.
|
3
|
+
# All rights reserved.
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
# Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public
|
16
|
+
# License along with this library; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
######################################################################
|
19
|
+
|
20
|
+
# Converts a custom Excel spreadsheet format to BCL components for upload
|
21
|
+
# Format of the Excel spreadsheet is documented in /doc/ComponentSpreadsheet.docx
|
22
|
+
|
23
|
+
if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
|
24
|
+
begin
|
25
|
+
# apparently this is not a gem (todo: need to remove and replace with roo)
|
26
|
+
require 'win32ole'
|
27
|
+
mod = WIN32OLE
|
28
|
+
$have_win32ole = true
|
29
|
+
rescue NameError
|
30
|
+
# do not have win32ole
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module BCL
|
35
|
+
class ComponentSpreadsheet
|
36
|
+
public
|
37
|
+
|
38
|
+
# WINDOWS ONLY SECTION BECAUSE THIS USES WIN32OLE
|
39
|
+
if $have_win32ole
|
40
|
+
|
41
|
+
# initialize with Excel spreadsheet to read
|
42
|
+
def initialize(xlsx_path, worksheet_names = ['all'])
|
43
|
+
@xlsx_path = Pathname.new(xlsx_path).realpath.to_s
|
44
|
+
@worksheets = []
|
45
|
+
|
46
|
+
begin
|
47
|
+
|
48
|
+
excel = WIN32OLE.new('Excel.Application')
|
49
|
+
|
50
|
+
xlsx = excel.Workbooks.Open(@xlsx_path)
|
51
|
+
|
52
|
+
# by default, operate on all worksheets
|
53
|
+
if worksheet_names == ['all']
|
54
|
+
xlsx.Worksheets.each do |xlsx_worksheet|
|
55
|
+
parse_xlsx_worksheet(xlsx_worksheet)
|
56
|
+
end
|
57
|
+
else # if specific worksheets are specified, operate on them
|
58
|
+
worksheet_names.each do |worksheet_name|
|
59
|
+
parse_xlsx_worksheet(xlsx.Worksheets(worksheet_name))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# save spreadsheet if changes have been made
|
64
|
+
if xlsx.saved == true
|
65
|
+
# puts "[ComponentSpreadsheet] Spreadsheet unchanged; not saving"
|
66
|
+
else
|
67
|
+
xlsx.Save
|
68
|
+
puts '[ComponentSpreadsheet] Spreadsheet changes saved'
|
69
|
+
end
|
70
|
+
|
71
|
+
ensure
|
72
|
+
|
73
|
+
excel.Quit
|
74
|
+
WIN32OLE.ole_free(excel)
|
75
|
+
excel.ole_free
|
76
|
+
xlsx = nil
|
77
|
+
excel = nil
|
78
|
+
GC.start
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
else # if $have_win32ole
|
84
|
+
|
85
|
+
# parse the master taxonomy document
|
86
|
+
def initialize(_xlsx_path)
|
87
|
+
puts "ComponentSpreadsheet class requires 'win32ole' to parse the component spreadsheet."
|
88
|
+
puts 'ComponentSpreadsheet may also be stored and loaded from JSON if your platform does not support win32ole.'
|
89
|
+
end
|
90
|
+
|
91
|
+
end # if $have_win32ole
|
92
|
+
|
93
|
+
def save(save_path, chunk_size = 1000, delete_old_gather = false)
|
94
|
+
# load master taxonomy to validate components
|
95
|
+
taxonomy = BCL::MasterTaxonomy.new
|
96
|
+
|
97
|
+
# FileUtils.rm_rf(save_path) if File.exists?(save_path) and File.directory?(save_path)
|
98
|
+
|
99
|
+
@worksheets.each do |worksheet|
|
100
|
+
worksheet.components.each do |component|
|
101
|
+
|
102
|
+
component_xml = Component.new("#{save_path}/components")
|
103
|
+
component_xml.name = component.name
|
104
|
+
component_xml.uid = component.uid
|
105
|
+
component_xml.comp_version_id = component.version_id
|
106
|
+
|
107
|
+
# this tag is how we know where this goes in the taxonomy
|
108
|
+
component_xml.add_tag(worksheet.name)
|
109
|
+
|
110
|
+
values = component.values[0]
|
111
|
+
component.headers.each do |header|
|
112
|
+
|
113
|
+
if /description/i.match(header.name)
|
114
|
+
|
115
|
+
name = values.delete_at(0)
|
116
|
+
uid = values.delete_at(0)
|
117
|
+
version_id = values.delete_at(0)
|
118
|
+
description = values.delete_at(0)
|
119
|
+
fidelity_level = values.delete_at(0).to_int
|
120
|
+
# name, uid, and version_id already processed
|
121
|
+
component_xml.description = description
|
122
|
+
component_xml.fidelity_level = fidelity_level
|
123
|
+
|
124
|
+
elsif /provenance/i.match(header.name)
|
125
|
+
|
126
|
+
author = values.delete_at(0)
|
127
|
+
datetime = values.delete_at(0)
|
128
|
+
if datetime.nil?
|
129
|
+
# puts "[ComponentSpreadsheet] WARNING missing the date in the datetime column in the spreadsheet - assuming today"
|
130
|
+
datetime = DateTime.new
|
131
|
+
else
|
132
|
+
datetime = DateTime.parse(datetime)
|
133
|
+
end
|
134
|
+
|
135
|
+
comment = values.delete_at(0)
|
136
|
+
component_xml.add_provenance(author.to_s, datetime.to_s, comment.to_s)
|
137
|
+
|
138
|
+
elsif /tag/i.match(header.name)
|
139
|
+
|
140
|
+
value = values.delete_at(0)
|
141
|
+
component_xml.add_tag(value)
|
142
|
+
|
143
|
+
elsif /attribute/i.match(header.name)
|
144
|
+
|
145
|
+
value = values.delete_at(0)
|
146
|
+
name = header.children[0]
|
147
|
+
units = ''
|
148
|
+
if match_data = /(.*)\((.*)\)/.match(name)
|
149
|
+
name = match_data[1].strip
|
150
|
+
units = match_data[2].strip
|
151
|
+
end
|
152
|
+
component_xml.add_attribute(name, value, units)
|
153
|
+
|
154
|
+
elsif /source/i.match(header.name)
|
155
|
+
|
156
|
+
manufacturer = values.delete_at(0)
|
157
|
+
model = values.delete_at(0)
|
158
|
+
serial_no = values.delete_at(0)
|
159
|
+
year = values.delete_at(0)
|
160
|
+
url = values.delete_at(0)
|
161
|
+
component_xml.source_manufacturer = manufacturer
|
162
|
+
component_xml.source_model = model
|
163
|
+
component_xml.source_serial_no = serial_no
|
164
|
+
component_xml.source_year = year
|
165
|
+
component_xml.source_url = url
|
166
|
+
|
167
|
+
elsif /file/i.match(header.name)
|
168
|
+
|
169
|
+
software_program = values.delete_at(0)
|
170
|
+
version = values.delete_at(0)
|
171
|
+
filename = values.delete_at(0)
|
172
|
+
filetype = values.delete_at(0)
|
173
|
+
filepath = values.delete_at(0)
|
174
|
+
# not all components(rows) have all files; skip if filename "" or nil
|
175
|
+
next if filename == '' or filename.nil?
|
176
|
+
# skip the file if it doesn't exist at the specified location
|
177
|
+
unless File.exist?(filepath)
|
178
|
+
puts "[ComponentSpreadsheet] ERROR #{filepath} -> File does not exist, will not be included in component xml"
|
179
|
+
next # go to the next file
|
180
|
+
end
|
181
|
+
component_xml.add_file(software_program, version, filepath, filename, filetype)
|
182
|
+
|
183
|
+
else
|
184
|
+
fail "Unknown section #{header.name}"
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
189
|
+
|
190
|
+
taxonomy.check_component(component_xml)
|
191
|
+
|
192
|
+
component_xml.save_tar_gz(false)
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
BCL.gather_components(save_path, chunk_size, delete_old_gather)
|
199
|
+
end
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
def parse_xlsx_worksheet(xlsx_worksheet)
|
204
|
+
worksheet = WorksheetStruct.new
|
205
|
+
worksheet.name = xlsx_worksheet.Range('A1').Value
|
206
|
+
worksheet.components = []
|
207
|
+
puts "[ComponentSpreadsheet] Starting parsing components of type #{worksheet.name}"
|
208
|
+
|
209
|
+
# find number of rows, first column should be name, should not be empty
|
210
|
+
num_rows = 1
|
211
|
+
while true
|
212
|
+
test = xlsx_worksheet.Range("A#{num_rows}").Value
|
213
|
+
if test.nil? or test.empty?
|
214
|
+
num_rows -= 1
|
215
|
+
break
|
216
|
+
end
|
217
|
+
num_rows += 1
|
218
|
+
end
|
219
|
+
|
220
|
+
# scan number of columns
|
221
|
+
headers = []
|
222
|
+
header = nil
|
223
|
+
max_col = nil
|
224
|
+
xlsx_worksheet.Columns.each do |col|
|
225
|
+
value1 = col.Rows('1').Value
|
226
|
+
value2 = col.Rows('2').Value
|
227
|
+
|
228
|
+
if not value1.nil? and not value1.empty?
|
229
|
+
unless header.nil?
|
230
|
+
headers << header
|
231
|
+
end
|
232
|
+
header = HeaderStruct.new
|
233
|
+
header.name = value1
|
234
|
+
header.children = []
|
235
|
+
end
|
236
|
+
|
237
|
+
if not value2.nil? and not value2.empty?
|
238
|
+
unless header.nil?
|
239
|
+
header.children << value2
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
if (value1.nil? or value1.empty?) and (value2.nil? or value2.empty?)
|
244
|
+
break
|
245
|
+
end
|
246
|
+
|
247
|
+
matchdata = /^\$(.+):/.match(col.Address)
|
248
|
+
max_col = matchdata[1]
|
249
|
+
end
|
250
|
+
|
251
|
+
unless header.nil?
|
252
|
+
headers << header
|
253
|
+
end
|
254
|
+
|
255
|
+
unless headers.empty?
|
256
|
+
headers[0].name = 'description'
|
257
|
+
end
|
258
|
+
|
259
|
+
puts " Found #{num_rows - 2} components"
|
260
|
+
|
261
|
+
components = []
|
262
|
+
for i in 3..num_rows do
|
263
|
+
component = ComponentStruct.new
|
264
|
+
component.row = i
|
265
|
+
|
266
|
+
# get name
|
267
|
+
component.name = xlsx_worksheet.Range("A#{i}").value
|
268
|
+
|
269
|
+
# get uid, if empty set it
|
270
|
+
component.uid = xlsx_worksheet.Range("B#{i}").value
|
271
|
+
if component.uid.nil? or component.uid.empty?
|
272
|
+
component.uid = UUID.new.generate
|
273
|
+
puts "#{component.name} uid missing; creating new one"
|
274
|
+
xlsx_worksheet.Range("B#{i}").value = component.uid
|
275
|
+
end
|
276
|
+
|
277
|
+
# get version_id, if empty set it
|
278
|
+
component.version_id = xlsx_worksheet.Range("C#{i}").value
|
279
|
+
if component.version_id.nil? or component.version_id.empty?
|
280
|
+
component.version_id = UUID.new.generate
|
281
|
+
puts "#{component.name} version id missing; creating new one"
|
282
|
+
xlsx_worksheet.Range("C#{i}").value = component.version_id
|
283
|
+
end
|
284
|
+
|
285
|
+
component.headers = headers
|
286
|
+
component.values = xlsx_worksheet.Range("A#{i}:#{max_col}#{i}").value
|
287
|
+
worksheet.components << component
|
288
|
+
end
|
289
|
+
|
290
|
+
@worksheets << worksheet
|
291
|
+
|
292
|
+
puts "[ComponentSpreadsheet] Finished parsing components of type #{worksheet.name}"
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end # module BCL
|