[Swift-commit] r3325 - in SwiftApps/adem-osg: lib lib/adem test

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Mon May 3 14:57:17 CDT 2010


Author: aespinosa
Date: 2010-05-03 14:57:17 -0500 (Mon, 03 May 2010)
New Revision: 3325

Modified:
   SwiftApps/adem-osg/lib/adem.rb
   SwiftApps/adem-osg/lib/adem/config.rb
   SwiftApps/adem-osg/test/test_offline.rb
Log:
Added configuration options and tests

Modified: SwiftApps/adem-osg/lib/adem/config.rb
===================================================================
--- SwiftApps/adem-osg/lib/adem/config.rb	2010-05-03 19:57:13 UTC (rev 3324)
+++ SwiftApps/adem-osg/lib/adem/config.rb	2010-05-03 19:57:17 UTC (rev 3325)
@@ -1,7 +1,56 @@
+require 'fileutils'
+
 class ConfigError < RuntimeError
 end
 
-def load_config(yaml_config)
+def config(args, config_file)
+  options = {}
+  conf_new = {}
+  optparse = OptionParser.new do |opts|
+    opts.banner = 'Usage: adem config [options]'
+
+    opts.on(nil, '--ress-server SERVER', 'RESS server name') do |server|
+      conf_new[:ress_server] = server
+    end
+
+    opts.on(nil, '--pacman-cache CACHE', 'Location of pacman cache') do |cache|
+      conf_new[:pacman_cache] = cache
+    end
+
+    opts.on(nil, '--virtual-organization VO', 'Name of virtual organization') do |vo|
+      conf_new[:virtual_organization] = vo
+    end
+
+    opts.on(nil, '--display', 'Display current configuration') do 
+      options[:display] = true
+    end
+
+    opts.on_tail('-h', '--help', 'Show this message') do
+      puts opts
+      exit
+    end
+  end
+  optparse.parse!
+
+  conf_yaml = nil
+  conf = nil
+  begin
+    conf_yaml = File.open(config_file).read
+    conf = config_load conf_yaml
+    if options[:display]
+      puts conf_yaml
+      exit
+    end
+  rescue  Errno::ENOENT
+    # must be complete if we are creating a new file
+    config_check conf_new
+  end
+  conf.merge!(conf_new)
+  config_update config_file if not conf_new.empty?
+  conf
+end
+
+def config_load(yaml_config)
   conf = YAML.load yaml_config
   conf.each do |key, val|
     conf.delete(key)
@@ -10,6 +59,15 @@
   conf
 end
 
-def config(args, config_file)
-  load_config File.open(config_file)
+def config_check conf
+  raise ConfigError, "RESS server missing" if not conf[:ress_server]
+  raise ConfigError, "Pacman cache missing" if not conf[:pacman_cache]
+  raise ConfigError, "Virtual organization missing" if not conf[:virtual_organization]
 end
+
+def config_update config_file
+  FileUtils.mkdir_p File.dirname(config_file)
+  File.open(config_file, "w") do |dump|
+    dump << conf.to_yaml
+  end
+end

Modified: SwiftApps/adem-osg/lib/adem.rb
===================================================================
--- SwiftApps/adem-osg/lib/adem.rb	2010-05-03 19:57:13 UTC (rev 3324)
+++ SwiftApps/adem-osg/lib/adem.rb	2010-05-03 19:57:17 UTC (rev 3325)
@@ -1,3 +1,4 @@
+require 'optparse'
 require 'yaml'
 require 'ftools'
 
@@ -12,7 +13,7 @@
 def run_command(args, config_file, sites_file)
   command = args.shift
   output = nil
-  conf = load_config File.open(config_file)
+  conf = config args, config_file
   return conf if command == "config"
   begin
     site_args = nil

Modified: SwiftApps/adem-osg/test/test_offline.rb
===================================================================
--- SwiftApps/adem-osg/test/test_offline.rb	2010-05-03 19:57:13 UTC (rev 3324)
+++ SwiftApps/adem-osg/test/test_offline.rb	2010-05-03 19:57:17 UTC (rev 3325)
@@ -32,10 +32,6 @@
     assert_equal("app", app(nil, conf))
   end
 
-  def test_config
-    assert_equal(@conf, config(nil, "config"))
-  end
-
   def test_app_avail
     pacman_cache = "test/dummy_cache"
     response = <<-eos
@@ -45,5 +41,26 @@
     assert_equal(response, app_avail(pacman_cache))
   end
 
+  def test_config
+    assert_equal(@conf, config(nil, "test/dummy_config"))
+  end
 
+  def test_config_load
+    assert_equal(@conf, config_load(File.open("test/dummy_config").read))
+  end
+
+  def test_config_check
+    exception = assert_raise ConfigError do
+      config_check({})
+    end
+    assert_match /RESS server/, exception.message
+    exception = assert_raise ConfigError do
+      config_check({:ress_server => "foo"})
+    end
+    assert_match /Pacman cache/, exception.message
+    exception = assert_raise ConfigError do
+      config_check({:ress_server => "foo", :pacman_cache => "bar"})
+    end
+    assert_match /Virtual organization/, exception.message
+  end
 end




More information about the Swift-commit mailing list