From aaa4c55a53c2f10ced785be12506934468067ec0 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Wed, 1 Oct 2014 12:57:28 +0100 Subject: Add base class for Ruby importers --- import/importer_base.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ import/omnibus.to_chunk | 2 ++ import/rubygems.to_chunk | 26 ++++++-------------------- 3 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 import/importer_base.rb diff --git a/import/importer_base.rb b/import/importer_base.rb new file mode 100644 index 00000000..aa91ff6e --- /dev/null +++ b/import/importer_base.rb @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby +# +# Base class for importers written in Ruby +# +# Copyright (C) 2014 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +module Importer + class Base + def log + @logger ||= create_logger + end + + private + + def create_logger + # Use the logger that was passed in from the 'main' import process, if + # detected. + log_fd = ENV['MORPH_LOG_FD'] + if log_fd + log_stream = IO.new(Integer(log_fd), 'w') + logger = Logger.new(log_stream) + logger.level = Logger::DEBUG + logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" } + else + logger = Logger.new('/dev/null') + end + logger + end + end +end diff --git a/import/omnibus.to_chunk b/import/omnibus.to_chunk index 67ff1175..2edafc17 100644 --- a/import/omnibus.to_chunk +++ b/import/omnibus.to_chunk @@ -25,6 +25,8 @@ require 'rubygems/commands/install_command' require 'shellwords' require 'yaml' +require_relative 'importer_base' + TARGET_PROJECT = 'chefdk' # should come from args TARGET_SOFTWARE = 'chefdk' # should come from args diff --git a/import/rubygems.to_chunk b/import/rubygems.to_chunk index bf08f7ff..6cb54908 100755 --- a/import/rubygems.to_chunk +++ b/import/rubygems.to_chunk @@ -22,21 +22,7 @@ require 'logger' require 'optparse' require 'yaml' -# Log information was passed in from the main import process, probably. -# This global constant approach seems a little ugly, but it seems to be -# recommended here: -# -# -log_fd = ENV['MORPH_LOG_FD'] -if log_fd - log_stream = IO.new(Integer(log_fd), 'w') - Log = Logger.new(log_stream) - Log.level = Logger::DEBUG - Log.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" } -else - Log = Logger.new('/dev/null') -end - +require_relative 'importer_base' class << Bundler def default_gemfile @@ -87,7 +73,7 @@ class RubyGemChunkMorphologyGenerator end def error(message) - Log.error(message) + log.error(message) STDERR.puts(message) end @@ -104,9 +90,9 @@ class RubyGemChunkMorphologyGenerator 'path' => dir, }) - Log.info "Loaded #{source.specs.count} specs from source dir." + log.info "Loaded #{source.specs.count} specs from source dir." source.specs.each do |spec| - Log.debug " * #{spec.inspect} #{spec.dependencies.inspect}" + log.debug " * #{spec.inspect} #{spec.dependencies.inspect}" end source @@ -234,7 +220,7 @@ class RubyGemChunkMorphologyGenerator def run source_dir_name, gem_name, expected_version = parse_options(ARGV) - Log.info("Creating chunk morph for #{gem_name} based on " + + log.info("Creating chunk morph for #{gem_name} based on " + "source code in #{source_dir_name}") Dir.chdir(source_dir_name) @@ -253,7 +239,7 @@ class RubyGemChunkMorphologyGenerator if not spec_is_from_current_source_tree(spec) error "Specified gem '#{spec.name}' doesn't live in the source in " + "'#{source_dir_name}'" - Log.debug "SPEC: #{spec.inspect} #{spec.source}" + log.debug "SPEC: #{spec.inspect} #{spec.source}" exit 1 end -- cgit v1.2.1