hets.rb revision ec416eab0158cfe34b77cea4a11f8b84bc194a7a
3263N/Amodule Hets
1506N/A class HetsError < Exception; end
1506N/A class HetsDeploymentError < Exception; end
1506N/A
1506N/A class Config
1506N/A attr_reader :path
1506N/A
1506N/A def initialize
1506N/A yaml = YAML.load_file(File.join(Rails.root, 'config', 'hets.yml'))
1506N/A
1506N/A @path = Hets.first_existent_of yaml['hets_path']
1506N/A
1506N/A raise HetsError, 'Could not find hets' unless @path
1506N/A
1506N/A version = `#{@path} -V`
1506N/A raise ArgumentError, "Your version of hets is too old" if version.include?("2011")
1506N/A
1506N/A yaml.each_pair do |key, value|
1506N/A ENV[key.upcase] = Hets.first_existent_of value if key != 'hets_path'
1506N/A end
1903N/A end
1506N/A end
3324N/A
1506N/A def self.first_existent_of(paths)
1506N/A paths.each do |path|
3263N/A path = File.expand_path path
3143N/A return path if File.exists? path
1506N/A end
1506N/A nil
1506N/A end
3234N/A
1506N/A # Runs hets with input_file and returns XML output file path.
1715N/A def self.parse(input_file, output_path = '')
1506N/A config = Config.new
1506N/A
1506N/A output_path = "-O \"#{output_path}\"" unless output_path.blank?
3263N/A
1506N/A command = "#{config.path} -o xml -v2 #{output_path} '#{input_file}' 2>&1"
1506N/A
1506N/A Rails.logger.debug command
1506N/A
1949N/A # nice runs the process with lower scheduling priority
1506N/A status = `nice #{command}`
1506N/A status = status.split("\n").last
1506N/A
1506N/A Rails.logger.debug status
1506N/A
1506N/A if $?.exitstatus != 0 or status.starts_with? '*** Error'
3194N/A raise HetsError.new(status)
1506N/A end
1506N/A
1506N/A status.split(': ').last
1950N/A end
1506N/A
1506N/A # The path to the ontology library path
1506N/A #
3171N/A # @return [String] the path to the ontology library of Hets
3194N/A #
3158N/A def self.library_path
3158N/A settings = YAML.load_file(File.join(Rails.root, 'config', 'hets.yml'))
3194N/A library_paths = settings['hets_lib']
1506N/A library_path = Hets.first_existent_of library_paths
1949N/A raise HetsDeploymentError.new("*** Error: Hets library not found. ***") if library_path.nil?
1506N/A return library_path
1506N/A end
1506N/A
1506N/A # Traverses the Hets Lib directory recursively calling back on every ontology file
1506N/A #
3194N/A # @param onology_handler [Method] the handler of ontology file names
3158N/A # @param library_path [String] the path to the ontology library
3194N/A #
1950N/A def self.handleOntologiesInDirectory(ontology_handler, library_path)
1506N/A ontology_handler.call(library_path)
1949N/A end
1506N/A
1506N/Aend
1506N/A