<commit_before>#!/usr/bin/env ruby
# Copyright 2012, Dell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: RobHirschfeld
#
require 'getoptlong'
require 'rubygems'
require 'yaml'
require 'json'
require 'fileutils'
require 'pp'

def debug(msg)
  STDERR.puts "DEBUG: " + msg if ENV['DEBUG'] == "true"
end

def fatal(msg, log=nil)
  m = "ERROR: #{msg}  Aborting"
  m ="#{m}; examine #{log} for more info."  if log
  STDERR.puts m
  exit 1
end

def camelize(str)
  str.split('_').map {|w| w.capitalize}.join
end

def knife(args)
  system("knife #{args} -k /etc/chef/webui.pem -u chef-webui")
end

class BarclampFS
  attr_reader :source, :metadata, :name
  attr_accessor :installed
  @@groups = Hash.new
  def initialize(src)
    @installed = false
    @chroot = false
    @skip_files=false
    @root = '/'
    @source = src
    begin
      @metadata=YAML.load_file(File.join(@source,"crowbar.yml"))
    rescue
      fatal("#{@source} is not an unpacked barclamp!")
    end
    @name = @metadata['barclamp']['name']
    if (@metadata['barclamp']['member'] rescue nil)
      @metadata['barclamp']['member'].each do |grp|
        @@groups[grp] ||= Array.new
        @@groups[grp] << @name
      end
    end
  end

  def root=(rootdir)
    @chroot=true
    FileUtils.mkdir_p(rootdir)
    @root=rootdir
  end

  def target=(tgt)
    @target=tgt
  end

  def target
    File.expand_path(File.join(@root,@target))
  end

  def dir
    File.join(target,"barclamps",@name)
  end

  def skip_chef=(t)
    @skip_chef = !!t | @chroot
  end

  def skip_chef
    @skip_chef | @chroot
  end

  def skip_files=(t)
    @skip_files = !!t
  end

  def skip_files
    @skip_files
  end

  def skip_engines=(t)
    @skip_engines = !!t
  end

  def skip_engines
    @skip_engines
  end

  def skip_migrations=(t)
    @skip_migrations = !!t | @chroot
  end

  def skip_migrations
    @skip_migrations | @chroot
  end

  def skip_install_actions=(t)
    @skip_install_actions = !!t | @chroot
  end

  def skip_install_actions
    @skip_install_actions | @chroot
  end

  def requires
    (@metadata['barclamp']['requires'] rescue []).map do |d|
      if /^@/ =~ d then @@groups[d[1..-1]] else d end
    end.flatten.uniq
  end

  def [](k)
    @metadata[k]
  end

  def install_app
    STDERR.puts "Installing #{@name} from #{@source} into #{target}"
    dirs = Dir.entries(@source)
    use_engine = dirs.include?("crowbar_engine")
    dirs.sort.each do |ent|
      next if /^\./ =~ ent or ent == "debian"
      case ent
      when "crowbar_framework"
        next if use_engine || skip_files
        debug("Copying crowbar_framework files over for #{@name}")
        FileUtils.cp_r(File.join(@source,'crowbar_framework'),target)
      when "crowbar_engine"
        FileUtils.cp_r(File.join(@source,ent),dir) unless @source == dir
        if FileTest.exists?(File.join(dir,ent,"barclamp_"+@name+"/test/unit"))
          FileUtils.mkdir_p(File.join(target,"crowbar_framework/test/unit"))
          FileUtils.cp_r(File.join(dir,ent,"barclamp_"+@name+"/test/unit"),File.join(target,"crowbar_framework/test/"))
        end
        if FileTest.exists?(File.join(dir,ent,"barclamp_"+@name+"/test/"+@name+"_test_helper.rb"))
          FileUtils.cp(File.join(dir,ent,"barclamp_"+@name+"/test/"+@name+"_test_helper.rb"),File.join(target,"crowbar_framework/test/"))
        end
        debug("#{@name} is implemented using a Rails Engine.")
        debug("Linking in routes and Gemfile entries.")
        gem_name       = "barclamp_#{@name}"
        engine_class   = "Barclamp%s::Engine" % camelize(@name)
        # N.B. the run-time path to the engine could be different from
        # the install-time path referenced by our 'dir' variable, so
        # instead we use a 'crowbar_path' variable set at runtime by
        # the main Gemfile.
        engine_path    = File.join('crowbar_engine', gem_name)
        framework_dir  = File.join(target, 'crowbar_framework')
        routes_dir     = File.join(framework_dir, 'config', 'routes.d')
        gemfile_dir    = File.join(framework_dir, 'Gemfile.d')
        FileUtils.mkdir_p(routes_dir)
        FileUtils.mkdir_p(gemfile_dir)
        routes_plugin  = File.join(routes_dir,  "barclamp-#{@name}-engine.routes")
        gemfile_plugin = File.join(gemfile_dir, "barclamp-#{@name}.gemfile")
        File.new(routes_plugin,  'w') do |f|
          f.puts("mount #{engine_class}, :at => '#{@name}'")
        end unless File.exists?(routes_plugin)
        File.open(gemfile_plugin, 'w') do |f|
          f.puts("gem '#{gem_name}', :path => File.join(crowbar_path, '..', 'barclamps', '#{@name}', '#{engine_path}')")
        end unless File.exists?(gemfile_plugin)
      when 'bin'<commit_msg>File::new() does not take block; use File::open() instead<commit_after>#!/usr/bin/env ruby
# Copyright 2012, Dell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: RobHirschfeld
#
require 'getoptlong'
require 'rubygems'
require 'yaml'
require 'json'
require 'fileutils'
require 'pp'

def debug(msg)
  STDERR.puts "DEBUG: " + msg if ENV['DEBUG'] == "true"
end

def fatal(msg, log=nil)
  m = "ERROR: #{msg}  Aborting"
  m ="#{m}; examine #{log} for more info."  if log
  STDERR.puts m
  exit 1
end

def camelize(str)
  str.split('_').map {|w| w.capitalize}.join
end

def knife(args)
  system("knife #{args} -k /etc/chef/webui.pem -u chef-webui")
end

class BarclampFS
  attr_reader :source, :metadata, :name
  attr_accessor :installed
  @@groups = Hash.new
  def initialize(src)
    @installed = false
    @chroot = false
    @skip_files=false
    @root = '/'
    @source = src
    begin
      @metadata=YAML.load_file(File.join(@source,"crowbar.yml"))
    rescue
      fatal("#{@source} is not an unpacked barclamp!")
    end
    @name = @metadata['barclamp']['name']
    if (@metadata['barclamp']['member'] rescue nil)
      @metadata['barclamp']['member'].each do |grp|
        @@groups[grp] ||= Array.new
        @@groups[grp] << @name
      end
    end
  end

  def root=(rootdir)
    @chroot=true
    FileUtils.mkdir_p(rootdir)
    @root=rootdir
  end

  def target=(tgt)
    @target=tgt
  end

  def target
    File.expand_path(File.join(@root,@target))
  end

  def dir
    File.join(target,"barclamps",@name)
  end

  def skip_chef=(t)
    @skip_chef = !!t | @chroot
  end

  def skip_chef
    @skip_chef | @chroot
  end

  def skip_files=(t)
    @skip_files = !!t
  end

  def skip_files
    @skip_files
  end

  def skip_engines=(t)
    @skip_engines = !!t
  end

  def skip_engines
    @skip_engines
  end

  def skip_migrations=(t)
    @skip_migrations = !!t | @chroot
  end

  def skip_migrations
    @skip_migrations | @chroot
  end

  def skip_install_actions=(t)
    @skip_install_actions = !!t | @chroot
  end

  def skip_install_actions
    @skip_install_actions | @chroot
  end

  def requires
    (@metadata['barclamp']['requires'] rescue []).map do |d|
      if /^@/ =~ d then @@groups[d[1..-1]] else d end
    end.flatten.uniq
  end

  def [](k)
    @metadata[k]
  end

  def install_app
    STDERR.puts "Installing #{@name} from #{@source} into #{target}"
    dirs = Dir.entries(@source)
    use_engine = dirs.include?("crowbar_engine")
    dirs.sort.each do |ent|
      next if /^\./ =~ ent or ent == "debian"
      case ent
      when "crowbar_framework"
        next if use_engine || skip_files
        debug("Copying crowbar_framework files over for #{@name}")
        FileUtils.cp_r(File.join(@source,'crowbar_framework'),target)
      when "crowbar_engine"
        FileUtils.cp_r(File.join(@source,ent),dir) unless @source == dir
        if FileTest.exists?(File.join(dir,ent,"barclamp_"+@name+"/test/unit"))
          FileUtils.mkdir_p(File.join(target,"crowbar_framework/test/unit"))
          FileUtils.cp_r(File.join(dir,ent,"barclamp_"+@name+"/test/unit"),File.join(target,"crowbar_framework/test/"))
        end
        if FileTest.exists?(File.join(dir,ent,"barclamp_"+@name+"/test/"+@name+"_test_helper.rb"))
          FileUtils.cp(File.join(dir,ent,"barclamp_"+@name+"/test/"+@name+"_test_helper.rb"),File.join(target,"crowbar_framework/test/"))
        end
        debug("#{@name} is implemented using a Rails Engine.")
        debug("Linking in routes and Gemfile entries.")
        gem_name       = "barclamp_#{@name}"
        engine_class   = "Barclamp%s::Engine" % camelize(@name)
        # N.B. the run-time path to the engine could be different from
        # the install-time path referenced by our 'dir' variable, so
        # instead we use a 'crowbar_path' variable set at runtime by
        # the main Gemfile.
        engine_path    = File.join('crowbar_engine', gem_name)
        framework_dir  = File.join(target, 'crowbar_framework')
        routes_dir     = File.join(framework_dir, 'config', 'routes.d')
        gemfile_dir    = File.join(framework_dir, 'Gemfile.d')
        FileUtils.mkdir_p(routes_dir)
        FileUtils.mkdir_p(gemfile_dir)
        routes_plugin  = File.join(routes_dir,  "barclamp-#{@name}-engine.routes")
        gemfile_plugin = File.join(gemfile_dir, "barclamp-#{@name}.gemfile")
        File.open(routes_plugin,  'w') do |f|
          f.puts("mount #{engine_class}, :at => '#{@name}'")
        end unless File.exists?(routes_plugin)
        File.open(gemfile_plugin, 'w') do |f|
          f.puts("gem '#{gem_name}', :path => File.join(crowbar_path, '..', 'barclamps', '#{@name}', '#{engine_path}')")
        end unless File.exists?(gemfile_plugin)
      when 'bin'
{'fraction_non_alphanumeric': 0.14094345038752273, 'fraction_numerical': 0.0021050617165821453, 'mean_word_length': 3.221324717285945, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3866938', 'n_tokens_mistral': 3671, 'n_tokens_neox': 3602, 'n_words': 862}