bcl 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bcl/component_methods.rb +239 -0
- data/lib/bcl/{ComponentSpreadsheet.rb → component_spreadsheet.rb} +5 -5
- data/lib/bcl/component_xml.rb +476 -0
- data/lib/bcl/current_taxonomy.json +0 -0
- data/lib/bcl/{MasterTaxonomy.rb → master_taxonomy.rb} +2 -1
- data/lib/bcl/mongo_to_component.rb +114 -0
- data/lib/bcl/{TarBall.rb → tar_ball.rb} +20 -19
- data/lib/bcl/version.rb +1 -1
- data/lib/bcl.rb +6 -6
- metadata +125 -93
- data/lib/bcl/ComponentXml.rb +0 -479
- data/lib/bcl/GatherComponents.rb +0 -94
- data/lib/bcl/MongoToComponent.rb +0 -116
@@ -0,0 +1,239 @@
|
|
1
|
+
######################################################################
|
2
|
+
# Copyright (c) 2008-2013, 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
|
+
require 'rubygems'
|
21
|
+
require 'pathname'
|
22
|
+
require 'fileutils'
|
23
|
+
require 'enumerator'
|
24
|
+
require 'yaml'
|
25
|
+
require 'base64'
|
26
|
+
|
27
|
+
# required gems
|
28
|
+
require 'json/pure'
|
29
|
+
require 'bcl/tar_ball'
|
30
|
+
require 'rest-client'
|
31
|
+
|
32
|
+
module BCL
|
33
|
+
|
34
|
+
class ComponentMethods
|
35
|
+
|
36
|
+
attr_accessor :config
|
37
|
+
attr_accessor :session
|
38
|
+
|
39
|
+
def initialize()
|
40
|
+
@config = nil
|
41
|
+
@session = nil
|
42
|
+
config_path = File.expand_path('~') + '/.bcl'
|
43
|
+
config_name = 'config.yml'
|
44
|
+
if File.exists?(config_path + "/" + config_name)
|
45
|
+
puts "loading config settings from #{config_path + "/" + config_name}"
|
46
|
+
@config = YAML.load_file(config_path + "/" + config_name)
|
47
|
+
else
|
48
|
+
#location of template file
|
49
|
+
FileUtils.mkdir_p(config_path)
|
50
|
+
File.open(config_path + "/" + config_name, 'w') do |file|
|
51
|
+
file << default_yaml.to_yaml
|
52
|
+
end
|
53
|
+
raise "******** Please fill in user credentials in #{config_path}/#{config_name} file. DO NOT COMMIT THIS FILE. **********"
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def default_yaml
|
59
|
+
settings = { :admin_user => { :username => "ENTER_BCL_USERNAME", :password => "ENTER_BCL_PASSWORD"} }
|
60
|
+
|
61
|
+
settings
|
62
|
+
end
|
63
|
+
|
64
|
+
def login(username=nil, password=nil)
|
65
|
+
if username.nil? || password.nil?
|
66
|
+
# log in via cached creditials
|
67
|
+
username = @config[:server][:admin_user][:username]
|
68
|
+
password = @config[:server][:admin_user][:password]
|
69
|
+
end
|
70
|
+
|
71
|
+
data = {"username" => username, "password" => password}
|
72
|
+
res = RestClient.post "http://#{@config[:server][:url]}/api/user/login", data.to_json, :content_type => :json, :accept => :json
|
73
|
+
|
74
|
+
if res.code == 200
|
75
|
+
#pull out the session key
|
76
|
+
res_j = JSON.parse(res.body)
|
77
|
+
|
78
|
+
sessid = res_j["sessid"]
|
79
|
+
session_name = res_j["session_name"]
|
80
|
+
|
81
|
+
@session = { session_name => sessid }
|
82
|
+
end
|
83
|
+
|
84
|
+
res
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
# pushes component to the bcl and sets to published (for now). Username and password and
|
89
|
+
# set in ~/.bcl/config.yml file which determines the permissions and the group to which
|
90
|
+
# the component will be uploaded
|
91
|
+
def push_component(filename_and_path, write_receipt_file)
|
92
|
+
raise "Please login before pushing components" if @session.nil?
|
93
|
+
|
94
|
+
valid = false
|
95
|
+
res_j = nil
|
96
|
+
filename = File.basename(filename_and_path)
|
97
|
+
filepath = File.dirname(filename_and_path) + "/"
|
98
|
+
|
99
|
+
# TODO: BCL will be moving over to single endpoint to manage the file upload and node creation (update eventually)
|
100
|
+
file = File.open(filename_and_path, 'r')
|
101
|
+
file_b64 = Base64.encode64(file.read)
|
102
|
+
@data = {"file" =>
|
103
|
+
{
|
104
|
+
"file" => "#{file_b64}",
|
105
|
+
"filesize" => "#{File.size(filename_and_path)}",
|
106
|
+
"filename" => filename
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
res = RestClient.post "http://#{@config[:server][:url]}/api/file", @data.to_json, :content_type => :json, :cookies => @session, :accept => :json
|
111
|
+
|
112
|
+
if res.code == 200
|
113
|
+
fid = JSON.parse(res.body)["fid"]
|
114
|
+
|
115
|
+
#post the node now with reference to this fid
|
116
|
+
@data = {"node" =>
|
117
|
+
{"type" => "nrel_component",
|
118
|
+
"status" => 1, #NOTE THIS ONLY WORKS IF YOU ARE ADMIN
|
119
|
+
"field_tar_file" =>
|
120
|
+
{"und" => [
|
121
|
+
{"fid" => fid}
|
122
|
+
]
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
res = RestClient.post "http://#{@config[:server][:url]}/api/node", @data.to_json, :content_type => :json, :cookies => @session, :accept => :json
|
128
|
+
|
129
|
+
res_j = JSON.parse(res.body)
|
130
|
+
|
131
|
+
if res.code == 200
|
132
|
+
valid = true
|
133
|
+
elsif res.code == 500
|
134
|
+
raise "server exception"
|
135
|
+
valid = false
|
136
|
+
else
|
137
|
+
valid = false
|
138
|
+
end
|
139
|
+
else
|
140
|
+
res = nil
|
141
|
+
end
|
142
|
+
|
143
|
+
if valid
|
144
|
+
#write out a receipt file into the same directory of the component with the same file name as
|
145
|
+
#the component
|
146
|
+
if write_receipt_file
|
147
|
+
File.open(filepath + File.basename(filename, '.tar.gz') + ".receipt", 'w') do |file|
|
148
|
+
file << Time.now.to_s
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
[valid, res_j]
|
154
|
+
end
|
155
|
+
|
156
|
+
def push_components(array_of_components, skip_files_with_receipts)
|
157
|
+
logs = []
|
158
|
+
array_of_components.each do |comp|
|
159
|
+
receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
|
160
|
+
log_message = ""
|
161
|
+
if skip_files_with_receipts && File.exists?(receipt_file)
|
162
|
+
log_message = "skipping component because found receipt for #{comp}"
|
163
|
+
else
|
164
|
+
log_message = "pushing component #{comp}: "
|
165
|
+
puts log_message
|
166
|
+
valid, res = push_component(comp, true)
|
167
|
+
log_message += " #{valid} #{res.inspect.chomp}"
|
168
|
+
end
|
169
|
+
puts log_message
|
170
|
+
logs << log_message
|
171
|
+
end
|
172
|
+
|
173
|
+
logs
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
# TODO make this extend the component_xml class (or create a super class around components)
|
179
|
+
|
180
|
+
module_function
|
181
|
+
|
182
|
+
def gather_components(component_dir, chunk_size = 0, delete_previous_gather = false)
|
183
|
+
@dest_filename = "components"
|
184
|
+
@dest_file_ext = "tar.gz"
|
185
|
+
|
186
|
+
#store the starting directory
|
187
|
+
current_dir = Dir.pwd
|
188
|
+
|
189
|
+
#an array to hold reporting info about the batches
|
190
|
+
gather_components_report = []
|
191
|
+
|
192
|
+
#go to the directory containing the components
|
193
|
+
Dir.chdir(component_dir)
|
194
|
+
|
195
|
+
# delete any old versions of the component chunks
|
196
|
+
FileUtils.rm_rf("./_gather") if delete_previous_gather
|
197
|
+
|
198
|
+
#gather all the components into array
|
199
|
+
targzs = Pathname.glob("./**/*.tar.gz")
|
200
|
+
tar_cnt = 0
|
201
|
+
chunk_cnt = 0
|
202
|
+
targzs.each do |targz|
|
203
|
+
if chunk_size != 0 && (tar_cnt % chunk_size) == 0
|
204
|
+
chunk_cnt += 1
|
205
|
+
end
|
206
|
+
tar_cnt += 1
|
207
|
+
|
208
|
+
destination_path = "./_gather/#{chunk_cnt}"
|
209
|
+
FileUtils.mkdir_p(destination_path)
|
210
|
+
destination_file = "#{destination_path}/#{File.basename(targz.to_s)}"
|
211
|
+
puts "copying #{targz.to_s} to #{destination_file}"
|
212
|
+
FileUtils.cp(targz.to_s, destination_file)
|
213
|
+
end
|
214
|
+
|
215
|
+
#gather all the zip files into a single tar.gz
|
216
|
+
(1..chunk_cnt).each do |cnt|
|
217
|
+
currentdir = Dir.pwd
|
218
|
+
|
219
|
+
paths = []
|
220
|
+
Pathname.glob("./_gather/#{cnt}/*.tar.gz").each do |pt|
|
221
|
+
paths << File.basename(pt.to_s)
|
222
|
+
end
|
223
|
+
|
224
|
+
Dir.chdir("./_gather/#{cnt}")
|
225
|
+
destination = "#{@dest_filename}_#{cnt}.#{@dest_file_ext}"
|
226
|
+
BCL.tarball(destination, paths)
|
227
|
+
Dir.chdir(currentdir)
|
228
|
+
|
229
|
+
#move the tarball back a directory
|
230
|
+
FileUtils.move("./_gather/#{cnt}/#{destination}", "./_gather/#{destination}")
|
231
|
+
end
|
232
|
+
|
233
|
+
Dir.chdir(current_dir)
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
end # module BCL
|
@@ -21,14 +21,14 @@
|
|
21
21
|
# Format of the Excel spreadsheet is documented in /doc/ComponentSpreadsheet.docx
|
22
22
|
|
23
23
|
require 'rubygems'
|
24
|
-
|
25
24
|
require 'pathname'
|
26
25
|
require 'fileutils'
|
27
|
-
require 'uuid' # gem install uuid
|
28
26
|
|
29
|
-
|
30
|
-
require '
|
31
|
-
require 'bcl/
|
27
|
+
# required gems
|
28
|
+
require 'uuid' # gem install uuid
|
29
|
+
require 'bcl/component_xml'
|
30
|
+
require 'bcl/component_methods'
|
31
|
+
require 'bcl/master_taxonomy'
|
32
32
|
|
33
33
|
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
|
34
34
|
begin
|