Method: NERA::SimulatorLayerController#dump_simulator

Defined in:
lib/nera/nera_simulator_layer_controller.rb

#dump_simulator(sim_name, job_records = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
# File 'lib/nera/nera_simulator_layer_controller.rb', line 138

def dump_simulator( sim_name, job_records = nil)
  flag = true
  sim_class = get_class_by_name( sim_name)
  return nil unless sim_class
  sim_id = (list.find do |rec| rec[:name] == sim_name end)[:id]
  return nil unless sim_id
  job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table ) unless job_records.is_a?(NERA::JobRecords)
  job_records.transaction {
    @sim_records.transaction {
      # check uncompleted jobs
      found = job_records.list_all.find do |rec| rec[:simulator] == sim_name end
      if found
        flag = nil
        return
      end
      # destroy the record
      # f = @sim_records.destroy( sim_id)
      # raise "Couldn't destroy the record #{sim_id} in simulators table" unless f
      # compression
      path1 = @db_folders.path_to_parameter_layer( sim_class)
      path2 = File.dirname( @db_folders.path_to_parameters_table( sim_class) )
      path3 = File.join( @db_folders.path_to_simulator_layer, "Simulator_classes", "#{sim_name}.rb")
      raise "Couldn't find #{path1}" unless File.directory?(path1)
      raise "Couldn't find #{path2}" unless File.directory?(path2)
      raise "Couldn't find #{path3}" unless File.exist?(path3)
      base_path = Pathname.new( @db_folders.path_to_simulator_layer ).realpath
      path1 = Pathname.new( path1).realpath.relative_path_from( base_path).to_s
      path2 = Pathname.new( path2).realpath.relative_path_from( base_path).to_s
      path3 = Pathname.new( path3).realpath.relative_path_from( base_path).to_s
      Dir.chdir(base_path) {
        tar_file = "#{sim_id}_#{sim_name}.tar"
        cmd = "tar cf #{tar_file} #{path1} #{path2} #{path3}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
        bz2_file = tar_file + '.bz2'
        FileUtils.rm( bz2_file) if File.exist?( bz2_file )
        cmd = "bzip2 #{tar_file}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
        # FileUtils.rm_r( path1)
        # FileUtils.rm_r( path2)
        # FileUtils.rm( path3)
      }
      # remove inherited classes
      # NERA::Simulator.remove_simulator( sim_class)
      # keep a log
      @db_folders.logger.info(self.class.to_s) {
        "dumped the simulator (#{sim_class.to_s})"
      }
    }
  }
  return flag
end