openstudiocore/src/cli/openstudio_cli.rb
# Executes code to update and compute arguments for measures
#
# @param [Array] sub_argv Options passed to the e command from the user input
# @return [Fixnum] Return status
#
Executes code to update and compute arguments for measures
Executes code to update and compute arguments for measures
def execute(sub_argv)
    $logger.info "Measure, sub_argv = #{sub_argv}"
    require_relative 'measure_manager'
    options = {}
    options[:update] = false
    options[:compute_arguments] = nil
    saved_subargv = []
    if sub_argv[0] == '-r' || sub_argv[0] == '--run_tests'
      saved_subargv = sub_argv[2..-1]
      sub_argv = sub_argv[0...2]
    end
    directory = nil
    if sub_argv.size > 1
      unless (sub_argv[0] == '-s' || sub_argv[0] == '--start_server')
        directory = sub_argv.pop
        $logger.debug("Directory to examine is #{directory}")
        $logger.debug("Remaining args are #{sub_argv}")
      end
    end
    opts = OptionParser.new do |o|
      o.banner = 'Usage: openstudio measure [options] DIRECTORY'
      o.separator ''
      o.separator 'Options:'
      o.separator ''
      o.on('-t', '--update_all', 'Update all measures in a directory') do
        options[:update_all] = true
      end
      o.on('-u', '--update', 'Update the measure.xml') do
        options[:update] = true
      end
      o.on('-a', '--compute_arguments [MODEL]', 'Compute arguments for the given OSM or IDF') do |model_file|
        options[:compute_arguments] = true
        options[:compute_arguments_model] = model_file
      end
      o.on('-r', '--run_tests', 'Run all tests recursively found in a directory, additional arguments are passed to minitest') do
        options[:run_tests] = true
      end
      o.on('-s', '--start_server [PORT]', 'Start a measure manager server') do |port|
        options[:start_server] = true
        options[:start_server_port] = port
      end
    end
    argv = parse_options(opts, sub_argv)
    return 0 if argv == nil
    $logger.debug("Measure command: #{argv.inspect} #{options.inspect}")
    if !options[:start_server]
      if directory.nil?
        $logger.error 'No directory provided'
        return 1
      end
      directory = File.expand_path(directory)
    end
    $logger.debug("Directory to examine is #{directory}")
    if options[:update_all]
      measure_manager = MeasureManager.new($logger)
      result = []
      Dir.glob("#{directory}/*/").each do |measure_dir|
        if File.directory?(measure_dir) && File.exists?(File.join(measure_dir, "measure.xml"))
          measure = measure_manager.get_measure(measure_dir, true)
          if measure.nil?
            $logger.debug("Directory #{measure_dir} is not a measure")
          else
            result << measure_manager.measure_hash(measure_dir, measure)
          end
        end
      end
      safe_puts JSON.generate(result)
    elsif options[:update]
      measure_manager = MeasureManager.new($logger)
      measure = measure_manager.get_measure(directory, true)
      if measure.nil?
        $logger.error("Cannot load measure from '#{directory}'")
        return 1
      end
      hash = measure_manager.measure_hash(directory, measure)
      safe_puts JSON.generate(hash)
    elsif options[:compute_arguments]
      measure_manager = MeasureManager.new($logger)
      measure = measure_manager.get_measure(directory, true)
      if measure.nil?
        $logger.error("Cannot load measure from '#{directory}'")
        return 1
      end
      model_path = options[:compute_arguments_model]
      model = OpenStudio::Model::OptionalModel.new()
      workspace = OpenStudio::OptionalWorkspace.new()
      if model_path
        measure_type = measure.measureType.valueName
        if measure_type == 'ModelMeasure'
          value = measure_manager.get_model(model_path, true)
          if value.nil?
            $logger.error("Cannot load model from '#{model_path}'")
            return 1
          else
            model = value[0].clone(true).to_Model
            workspace = value[1].clone(true)
          end
        elsif measure_type == 'EnergyPlusMeasure'
          value = measure_manager.get_idf(model_path, true)
          if value.nil?
            $logger.error("Cannot load workspace from '#{model_path}'")
            return 1
          else
            workspace = value.clone(true)
          end
        else
          $logger.error("Measure type '#{measure_type}' does not take a model path")
          return 1
        end
      else
        model_path = ""
      end
      measure_info = measure_manager.get_measure_info(directory, measure, model_path, model, workspace)
      hash = measure_manager.measure_hash(directory, measure, measure_info)
      safe_puts JSON.generate(hash)
    elsif options[:run_tests]
      ARGV.clear
      saved_subargv.each do |arg|
        ARGV << arg
      end
      $logger.debug("Minitest arguments are '#{saved_subargv}'")
        require 'minitest'
        require 'minitest/reporters'
        require 'minitest/minitest_reporter_plugin'
        Minitest.extensions << 'minitest_reporter'
        require 'openstudio_measure_tester'
      runner = OpenStudioMeasureTester::Runner.new(directory)
      result = runner.run_all(Dir.pwd) 
      if result != 0
        $logger.error("Measure tester returned errors")
        return 1
      end
    elsif options[:start_server]
      require_relative 'measure_manager_server'
      port = options[:start_server_port]
      if port.nil?
        port = 1234
      end
      server = WEBrick::HTTPServer.new(:Port => port)
      server.mount "/", MeasureManagerServlet
      trap("INT") {
          server.shutdown
      }
      server.start
    else
      $logger.error("Unknown measure command")
      return 1
    end
    0
  end
def execute ( sub_argv ) $logger . info "Measure, sub_argv = #{sub_argv}" require_relative 'measure_manager' options = { } options [ :update ] = false options [ :compute_arguments ] = nil saved_subargv = [ ] if sub_argv [ 0 ] == '-r' || sub_argv [ 0 ] == '--run_tests' saved_subargv = sub_argv [ 2 .. - 1 ] sub_argv = sub_argv [ 0 ... 2 ] end directory = nil if sub_argv . size > 1 unless ( sub_argv [ 0 ] == '-s' || sub_argv [ 0 ] == '--start_server' ) directory = sub_argv . pop $logger . debug ( "Directory to examine is #{directory}" ) $logger . debug ( "Remaining args are #{sub_argv}" ) end end opts = OptionParser . new do | o | o . banner = 'Usage: openstudio measure [options] DIRECTORY' o . separator '' o . separator 'Options:' o . separator '' o . on ( '-t' , '--update_all' , 'Update all measures in a directory' ) do options [ :update_all ] = true end o . on ( '-u' , '--update' , 'Update the measure.xml' ) do options [ :update ] = true end o . on ( '-a' , '--compute_arguments [MODEL]' , 'Compute arguments for the given OSM or IDF' ) do | model_file | options [ :compute_arguments ] = true options [ :compute_arguments_model ] = model_file end o . on ( '-r' , '--run_tests' , 'Run all tests recursively found in a directory, additional arguments are passed to minitest' ) do options [ :run_tests ] = true end o . on ( '-s' , '--start_server [PORT]' , 'Start a measure manager server' ) do | port | options [ :start_server ] = true options [ :start_server_port ] = port end end argv = parse_options ( opts , sub_argv ) return 0 if argv == nil $logger . debug ( "Measure command: #{argv.inspect} #{options.inspect}" ) if ! options [ :start_server ] if directory . nil? $logger . error 'No directory provided' return 1 end directory = File . expand_path ( directory ) end $logger . debug ( "Directory to examine is #{directory}" ) if options [ :update_all ] measure_manager = MeasureManager . new ( $logger ) result = [ ] Dir . glob ( "#{directory}/*/" ) . each do | measure_dir | if File . directory? ( measure_dir ) && File . exists? ( File . join ( measure_dir , "measure.xml" ) ) measure = measure_manager . get_measure ( measure_dir , true ) if measure . nil? $logger . debug ( "Directory #{measure_dir} is not a measure" ) else result << measure_manager . measure_hash ( measure_dir , measure ) end end end safe_puts JSON . generate ( result ) elsif options [ :update ] measure_manager = MeasureManager . new ( $logger ) measure = measure_manager . get_measure ( directory , true ) if measure . nil? $logger . error ( "Cannot load measure from '#{directory}'" ) return 1 end hash = measure_manager . measure_hash ( directory , measure ) safe_puts JSON . generate ( hash ) elsif options [ :compute_arguments ] measure_manager = MeasureManager . new ( $logger ) measure = measure_manager . get_measure ( directory , true ) if measure . nil? $logger . error ( "Cannot load measure from '#{directory}'" ) return 1 end model_path = options [ :compute_arguments_model ] model = OpenStudio :: Model :: OptionalModel . new ( ) workspace = OpenStudio :: OptionalWorkspace . new ( ) if model_path measure_type = measure . measureType . valueName if measure_type == 'ModelMeasure' value = measure_manager . get_model ( model_path , true ) if value . nil? $logger . error ( "Cannot load model from '#{model_path}'" ) return 1 else model = value [ 0 ] . clone ( true ) . to_Model workspace = value [ 1 ] . clone ( true ) end elsif measure_type == 'EnergyPlusMeasure' value = measure_manager . get_idf ( model_path , true ) if value . nil? $logger . error ( "Cannot load workspace from '#{model_path}'" ) return 1 else workspace = value . clone ( true ) end else $logger . error ( "Measure type '#{measure_type}' does not take a model path" ) return 1 end else model_path = "" end measure_info = measure_manager . get_measure_info ( directory , measure , model_path , model , workspace ) hash = measure_manager . measure_hash ( directory , measure , measure_info ) safe_puts JSON . generate ( hash ) elsif options [ :run_tests ] ARGV . clear saved_subargv . each do | arg | ARGV << arg end $logger . debug ( "Minitest arguments are '#{saved_subargv}'" ) require 'minitest' require 'minitest/reporters' require 'minitest/minitest_reporter_plugin' Minitest . extensions << 'minitest_reporter' require 'openstudio_measure_tester' runner = OpenStudioMeasureTester :: Runner . new ( directory ) result = runner . run_all ( Dir . pwd ) if result != 0 $logger . error ( "Measure tester returned errors" ) return 1 end elsif options [ :start_server ] require_relative 'measure_manager_server' port = options [ :start_server_port ] if port . nil? port = 1234 end server = WEBrick :: HTTPServer . new ( :Port => port ) server . mount "/" , MeasureManagerServlet trap ( "INT" ) { server . shutdown } server . start else $logger . error ( "Unknown measure command" ) return 1 end 0 end
Executes code to update and compute arguments for measures
Executes code to update and compute arguments for measures
# save some arguments to pass to minitest # find the directory # TODO: run unit tests # Parse the options # loop over all directories # restore saved arguments for minitest # load openstudio_measure_tester gem #begin # Minitest Reports use a plugin that is normally found by Minitest::load_plugins using Gem.find # until Gem.find is overloaded to find embedded gems, we will manually load the plugin here #rescue LoadError #puts "Cannot load 'openstudio_measure_tester'" #return 1 #end
None
Fixnum
sub_argv
<not_specific>
<not_specific>
sub_argv Options passed to the e command from the user input
sub_argv Options passed to the e command from the user input