diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bae6361 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.gem +.bundle +Gemfile.lock +pkg/* +.yardoc +*.rbc +*~ diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..e094f8d --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in supportconfig.gemspec +gemspec diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..997bc5e --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2011 SUSE LINUX Products GmbH + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 0000000..c8ff2ec --- /dev/null +++ b/README.rdoc @@ -0,0 +1,34 @@ += Supportconfig + +* http://github.com/kkaempf/supportconfig + +== Introduction + +Library to access SUSE supportconfig and command line tool. + +Its main goal is to be clean and provide a command line tool +that exposes its features. + +== Features + +Main use case is easy access to data inside supportconfig. + +Plugins can be written to deal with specific needs. + +== Extending Supportconfig + +Derive your class from Supportconfig::Supportconfig. + +== Debugging + +Set DEBUG environment variable non-empty. + +== Authors + +* Klaus Kämpf + +== License + +Copyright (c) 2016 SUSE LINUX Products GmbH. + +Bicho is licensed under the MIT license. See MIT-LICENSE for details. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..d5d327d --- /dev/null +++ b/Rakefile @@ -0,0 +1,31 @@ +$:.push(File.join(File.dirname(__FILE__), 'lib')) +require 'bundler/gem_tasks' +require 'supportconfig/version' +require 'rake/testtask' + +extra_docs = ['README*', 'TODO*', 'CHANGELOG*'] + +task :default => [:test] + +Rake::TestTask.new do |t| + t.libs << File.expand_path('../test', __FILE__) + t.libs << File.expand_path('../', __FILE__) + t.test_files = FileList['test/test*.rb'] + t.verbose = true +end + +begin + require 'yard' + YARD::Rake::YardocTask.new(:doc) do |t| + t.files = ['lib/**/*.rb', *extra_docs] + t.options = ['--no-private'] + end +rescue LoadError + STDERR.puts "Install yard if you want prettier docs" + require 'rdoc/task' + Rake::RDocTask.new(:doc) do |rdoc| + rdoc.rdoc_dir = "doc" + rdoc.title = "supportconfig #{Supportconfig::VERSION}" + extra_docs.each { |ex| rdoc.rdoc_files.include ex } + end +end diff --git a/bin/supportconfig b/bin/supportconfig new file mode 100755 index 0000000..4d9ef54 --- /dev/null +++ b/bin/supportconfig @@ -0,0 +1,96 @@ +#!/usr/bin/env ruby +# +# encoding: UTF-8 +# +# Copyright (c) 2016 SUSE LINUX Products GmbH +# +# Author: Klaus Kämpf +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +#++ +$:.push(File.join(File.dirname(__FILE__), '..', 'lib')) +require 'supportconfig' +require 'highline/import' + +# Set the console color scheme +ft = HighLine::ColorScheme.new do |cs| + cs[:headline] = [ :bold ] + cs[:horizontal_line] = [ :bold, :white ] + cs[:even_row] = [ :green ] + cs[:odd_row] = [ :magenta ] + cs[:error] = [ :red ] +end + +HighLine.color_scheme = ft +# Our 'terminal' object +t = HighLine.new + +# Scan all installed pluggable commands +Dir.glob(File.join(File.dirname(__FILE__), '..', 'lib', 'bicho', 'cli', 'commands', '*.rb')).each do |cmd| + load cmd +end + +# Setup a logger and connect it with stupid libraries +# that can't use a logger +Bicho::Logging.logger = Logger.new(STDERR) +# Don't show by default anything +Bicho::Logging.logger.level = Logger::FATAL + +ret = 0 +begin + # create subcommands automatically from the available + # symbols in BzConsole module + SUB_COMMANDS = Bicho::CLI::Commands.constants.sort.map(&:to_s).map(&:downcase) + + global_opts = Trollop::options do + banner <<-EOS + Usage: #{File.basename(__FILE__)} [global options] ... + + Bugzilla url can be given as https://bugs.kde.org or bko +EOS + opt :config, "Read FILE as configuration file.", :type => :io + opt :debug, "Show debug messages", :default => false + opt :bugzilla, "Bugzilla URL or alias", :default => 'bnz' + stop_on SUB_COMMANDS + end + + Bicho::Logging.logger.level = Logger::DEBUG if global_opts[:debug] + + # get the subcommand + cmd = ARGV.shift rescue nil + + if (!cmd || !SUB_COMMANDS.include?(cmd)) + Trollop.die "available subcommands: #{SUB_COMMANDS.join(" ")}" + end + + # Create an instance for the command + mod = Bicho::CLI::Commands + cmd_class = mod.const_get(cmd.capitalize) + cmd_instance = cmd_class.new + opts = cmd_instance.parse_options + + ret = cmd_instance.do(global_opts, opts, ARGV) +rescue Exception => e + Bicho::Logging.logger.error e.message + Bicho::Logging.logger.error e.backtrace + t.say("#{t.color('ERROR: ', :error)} #{t.color(e.message, :bold)}") + ret = 1 +end +exit ret diff --git a/lib/supportconfig.rb b/lib/supportconfig.rb new file mode 100644 index 0000000..6c5ec6a --- /dev/null +++ b/lib/supportconfig.rb @@ -0,0 +1,33 @@ +#-- +# Copyright (c) 2016 SUSE LINUX Products GmbH +# +# Author: Klaus Kämpf +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +#++ +require 'rubygems' +require 'logger' +require 'supportconfig/version' +require 'supportconfig/logging' +require 'supportconfig/supportconfig' + +module Supportconfig + +end diff --git a/lib/supportconfig/logging.rb b/lib/supportconfig/logging.rb new file mode 100644 index 0000000..eeb61ab --- /dev/null +++ b/lib/supportconfig/logging.rb @@ -0,0 +1,43 @@ +#-- +# Copyright (c) 2016 SUSE LINUX Products GmbH +# +# Author: Klaus Kämpf +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +#++ +require 'logger' + +module Supportconfig + module Logging + + def self.logger=(logger) + @logger = logger + end + + def self.logger + @logger ||= Logger.new('/dev/null') + end + + def logger + Logging.logger + end + + end +end diff --git a/lib/supportconfig/supportconfig.rb b/lib/supportconfig/supportconfig.rb new file mode 100644 index 0000000..83f2a36 --- /dev/null +++ b/lib/supportconfig/supportconfig.rb @@ -0,0 +1,78 @@ +module Supportconfig + + # + # class Supportconfig + # base class + # + # contains generic file parser + # + + class Supportconfig + def initialize client, dir, fname + @client = client + @fname = fname + parse File.join(dir, fname) + end +private + # + # generic parser for supportconfig .txt files + # + # .txt files have multiple section, every section + # is named and starting with + # #==[ ]====... + # following is content until EOF or the next section + # + # this parser splits files into sections and assembles + # the section content as array of lines + # + # the section name is used as a callback name + + def parse file + File.open(file) do |f| + content = [] + section = nil + f.each do |l| + l.chomp! + next if l.empty? + if l =~ /#==\[ (.*) \]===/ + # new section start + if section + # old section present + self.send section, content + section = nil + content = [] + end + section = $1.downcase.tr(" ", "_") + elsif section + content << l + else + # skip header + end + end + # send final section + self.send section, content if section + self.close + end + end +public + # + # Example callback + # + # + # section: + # #==[ Command ]======================================# + # + def command content + # empty - derive from Supportconfig and implement there + end + + # + # close file (eof reached) + # + def close + STDERR.puts "#{self.class}.close not implemented !" + end + + end # class + +end # module diff --git a/lib/supportconfig/version.rb b/lib/supportconfig/version.rb new file mode 100644 index 0000000..55fff71 --- /dev/null +++ b/lib/supportconfig/version.rb @@ -0,0 +1,3 @@ +module Supportconfig + VERSION = "0.0.1" +end diff --git a/supportconfig.gemspec b/supportconfig.gemspec new file mode 100644 index 0000000..17e1352 --- /dev/null +++ b/supportconfig.gemspec @@ -0,0 +1,22 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "supportconfig/version" + +Gem::Specification.new do |s| + s.name = "supportconfig" + s.version = Supportconfig::VERSION + s.authors = ["Klaus Kämpf"] + s.email = ["kkaempf@suse.de"] + s.homepage = "http://github.com/kkaempf/supportconfig" + s.summary = %q{Library to access SUSE supportconfig data} + s.description = %q{Library to access SUSE supportconfig data} + + s.add_dependency("highline", ["~> 1.7.8"]) + + s.rubyforge_project = "supportconfig" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] +end diff --git a/test/helper.rb b/test/helper.rb new file mode 100644 index 0000000..1f76057 --- /dev/null +++ b/test/helper.rb @@ -0,0 +1,9 @@ +$: << File.join(File.dirname(__FILE__), "..", "lib") +require 'test/unit' +require 'supportconfig' + +if ENV["DEBUG"] + Supportconfig::Logging.logger = Logger.new(STDERR) + Supportconfig::Logging.logger.level = Logger::DEBUG +end + diff --git a/test/test_startup.rb b/test/test_startup.rb new file mode 100644 index 0000000..2e37ce5 --- /dev/null +++ b/test/test_startup.rb @@ -0,0 +1,10 @@ +require File.join(File.dirname(__FILE__), 'helper') + +class Startup_test < Test::Unit::TestCase + + def test_startup + # constructor needs 3 arguments + assert_raise ArgumentError Supportconfig::Supportconfig.new + end + +end