Method: NERA::SimulatorLayerController#import_simulator

Defined in:
lib/nera/nera_simulator_layer_controller.rb

#import_simulator(sim_file) ⇒ Object



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
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/nera/nera_simulator_layer_controller.rb', line 208

def import_simulator( sim_file)
  flag = true
  return nil unless sim_file =~ /^([0-9]+)_([A-Z]\w*)\.tar\.bz2/
  return nil unless File.exist?( File.join(@db_folders.path_to_simulator_layer,sim_file) )
  sim_name = $2
  job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table )

  job_records.transaction {
    @sim_records.transaction {
      # check name overlap
      if @sim_records.list.find do |rec| rec[:name] == sim_name end
        flag = nil
        return
      end
      raise "must not happen" if File.directory?( File.join( @db_folders.path_to_simulator_layer, sim_name ) )
      raise "must not happen" if File.directory?( File.join( File.dirname(@db_folders.path_to_simulators_table), sim_name) )
      raise "must not happen" if File.exist?( File.join( @db_folders.path_to_simulator_layer, "Simulator_classes", "#{sim_name}.rb") )
      # decompress the file
      Dir.chdir(@db_folders.path_to_simulator_layer) {
        # cmd = "bunzip2 #{sim_file}"
        # system cmd
        # raise "command \"#{cmd}\" failed." unless $? == 0
        tar_file = sim_file.sub(/\.bz2$/,'')
        cmd = "tar xjf #{sim_file}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
      }
      # load simulator definition
      require File.join( @db_folders.path_to_simulator_layer, "Simulator_classes", "#{sim_name}.rb")
      sim_class = sim_name.split(/::/).inject(Object) {|c,name| c.const_get(name) }
      raise "invalid definition of simulator class #{sim_name}" unless sim_class.is_a?(Class)
      NERA::Simulator.add_simulator( sim_class)
      flag = @sim_records.add( sim_class)
      # keep a log
      @db_folders.logger.info(self.class.to_s) {
        "imported the simulator (#{sim_class.to_s})"
      }
    }
  }
  return flag
end