<commit_before>#!/bin/sh
exec ruby -S -x $0 "$@"
#!ruby
# coding: utf-8

##### require
require 'yaml'
require 'optparse'
require 'readline'
require 'fileutils'
require 'pp'

if RUBY_VERSION >= '1.9.2'
  require_relative 'lib/config.rb'
  require_relative 'lib/func.rb'
  require_relative 'lib/init.rb'
#  require_relative 'lib/targetwrapper.rb'
else
  require File.expand_path(File.dirname(__FILE__) + '/lib/config.rb')
  require File.expand_path(File.dirname(__FILE__) + '/lib/func.rb')
  require File.expand_path(File.dirname(__FILE__) + '/lib/init.rb')
#  require File.expand_path(File.dirname(__FILE__) + '/lib/targetwrapper.rb')
end

##### option parse
params = ARGV.getopts('', 'mode:', 'nodes:', 'keys:', 'file:', 'from:')
puts params if $DEBUG

if params['mode'] == nil or 
  (params['mode'] != 'init' and
   params['mode'] != 'select' and
   params['mode'] != 'update' and
   params['mode'] != 'delete')
  puts "ERROR: mode option"
  puts "\t--mode init|select|update|delete"
  exit 1
end

if (params['mode'] == 'select' and params['from'] == nil) or
   (params['mode'] != 'select' and params['from'] != nil)
  puts "ERROR: select option"
  puts "\t--mode select --from DIRECTORY"
  exit 1
end

##### configuration
work_dir = File.expand_path(File.dirname(__FILE__))

# read config file
config_list = ["default.yaml", "customize.yaml"]
config_list.map! { |name| File.join(work_dir, "conf", name) }
config = read_config_file(config_list)
pp config if $DEBUG

# create directory
build_dir = File.join(work_dir, "build", "master")
puts FileUtils.mkdir_p (build_dir)
create_initial_directory(build_dir)

if params["from"] != nil
  source_dir = File.join(work_dir, params["from"], "hieradata")
  hiera_file_list = Dir.glob(File.join(source_dir, "./**/*.yaml"))
else
  hiera_file_list = Dir.glob(File.join(build_dir, "./**/*.yaml"))
end
pp hiera_file_list

if params['file'] != nil
  patch_file = params['file']
else
  patch_file = File.join(work_dir, 'patch.yaml')
end

hiera_data_hash = {}
hiera_file_list.each do |file_name|
#  hiera_data_hash[file_name] = YAML.load_file(file_name)
  puts file_name if $DEBUG
  node = file_name.gsub(source_dir, '').gsub(/^\/*\.\/(.+)\.yaml$/, '\1')
  hiera_data_hash[node] = YAML.load_file(file_name)
end

case params['mode']
when 'init'
  ##### create initial puppet file
  puts '-' * 50
  puts 'create files - '
  create_initial_file(build_dir, config["verbose"])
when 'select'
  pp hiera_data_hash if $DEBUG
  if params['nodes'] != nil<commit_msg>bug fix last commit<commit_after>#!/bin/sh
exec ruby -S -x $0 "$@"
#!ruby
# coding: utf-8

##### require
require 'yaml'
require 'optparse'
require 'readline'
require 'fileutils'
require 'pp'

if RUBY_VERSION >= '1.9.2'
  require_relative 'lib/config.rb'
  require_relative 'lib/func.rb'
  require_relative 'lib/init.rb'
#  require_relative 'lib/targetwrapper.rb'
else
  require File.expand_path(File.dirname(__FILE__) + '/lib/config.rb')
  require File.expand_path(File.dirname(__FILE__) + '/lib/func.rb')
  require File.expand_path(File.dirname(__FILE__) + '/lib/init.rb')
#  require File.expand_path(File.dirname(__FILE__) + '/lib/targetwrapper.rb')
end

##### option parse
params = ARGV.getopts('', 'mode:', 'nodes:', 'keys:', 'file:', 'from:')
puts params if $DEBUG

if params['mode'] == nil or 
  (params['mode'] != 'init' and
   params['mode'] != 'select' and
   params['mode'] != 'update' and
   params['mode'] != 'delete')
  puts "ERROR: mode option"
  puts "\t--mode init|select|update|delete"
  exit 1
end

if (params['mode'] == 'select' and params['from'] == nil) or
   (params['mode'] != 'select' and params['from'] != nil)
  puts "ERROR: select option"
  puts "\t--mode select --from DIRECTORY"
  exit 1
end

##### configuration
work_dir = File.expand_path(File.dirname(__FILE__))

# read config file
config_list = ["default.yaml", "customize.yaml"]
config_list.map! { |name| File.join(work_dir, "conf", name) }
config = read_config_file(config_list)
pp config if $DEBUG

# create directory
build_dir = File.join(work_dir, "build", "master")
puts FileUtils.mkdir_p (build_dir)
create_initial_directory(build_dir)

if params["from"] != nil
  source_dir = File.join(work_dir, params["from"], "hieradata")
  hiera_file_list = Dir.glob(File.join(source_dir, "./**/*.yaml"))
else
  source_dir = build_dir
  hiera_file_list = Dir.glob(File.join(build_dir, "./**/*.yaml"))
end
pp hiera_file_list

if params['file'] != nil
  patch_file = params['file']
else
  patch_file = File.join(work_dir, 'patch.yaml')
end

hiera_data_hash = {}
hiera_file_list.each do |file_name|
#  hiera_data_hash[file_name] = YAML.load_file(file_name)
  puts file_name if $DEBUG
  node = file_name.gsub(source_dir, '').gsub(/^\/*\.\/(.+)\.yaml$/, '\1')
  hiera_data_hash[node] = YAML.load_file(file_name)
end

case params['mode']
when 'init'
  ##### create initial puppet file
  puts '-' * 50
  puts 'create files - '
  create_initial_file(build_dir, config["verbose"])
when 'select'
  pp hiera_data_hash if $DEBUG
  if params['nodes'] != nil
{'fraction_non_alphanumeric': 0.17693995611410332, 'fraction_numerical': 0.003989626969878316, 'mean_word_length': 4.891891891891892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1592320', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1816, 'n_words': 414}