<commit_before>require 'trollop'
require 'json'
require 'pathname'
require 'logger'
require 'foreman/process'

module Politburo
  class CLI
    attr_reader :options
    attr_reader :targets

    def self.create(arguments)
      p = Trollop::Parser.new do
          version "politburo, version #{Politburo::VERSION}"
          banner <<-EOS
        Politburo - The Babushka-wielding DevOps orchestrator 

        Usage:
               politburo [options] resource[#state] [resource2[#state]]+
        where [options] are:
        EOS
        opt :envfile, "Use a different envfile", :short => 'e', :default => 'Envfile'
        opt :'babushka-sources-dir', "Use a different babushka sources dir", :short => 'b', :default => '~/.babushka/sources'
      end

      opts = Trollop::with_standard_exception_handling p do
        opts = p.parse arguments
        raise Trollop::HelpNeeded if arguments.empty? # show help screen

        opts
      end

      self.new(opts, arguments)
    end

    def initialize(options, targets)
      @options = options
      @targets = targets
    end

    def run()
      generate_to_path = target_generation_path

      begin
        generate_to(generate_to_path.realpath)
        run_babushka
      ensure
        generate_to_path.rmtree
        log.debug("Cleaned up target generation path: '#{generate_to_path.realdirpath}'")
      end
    end

    def generate_to(target_dir)
      target_deps_file = File.join(target_dir, 'politburo_generated_deps.rb')
      File.open(target_deps_file, "w") { | f | f.write(root.to_babushka_deps) }
      log.debug("Generated deps to: '#{target_deps_file}'")
    end

    def root()
      @root ||= Politburo::DSL.define(envfile_contents)
    end

    def envfile_contents()
      @envfile_contents ||= File.open(options[:envfile], "r") { | f | f.read() }
    end

    def babushka_sources_path()
      result_path = Pathname.new(File.expand_path(options[:'babushka-sources-dir']))
      raise "Babushka sources directory: '#{result_path.to_s}' does not exist or isn't a directory" unless result_path.directory?
      result_path
    end

    def target_generation_dirname
      @target_generation_dirname ||= "politburo-run-#{Time.now.to_i.to_s}"
    end

    def target_generation_path()
      @target_generation_path ||= begin 
        target_generation_path = babushka_sources_path + target_generation_dirname
        target_generation_path.mkdir
        raise "Could not create target generation path: '#{target_generation_path.realpath}'" unless target_generation_path.directory?
        log.debug("Created target generation path: '#{target_generation_path.realpath}' ")
        target_generation_path
      end
    end

    def log()
      @log ||= ::Logger.new(STDOUT)
    end

    private

    def run_babushka
        command = "babushka #{target_generation_dirname}:All#ready"
        log.debug("About to execute command: '#{command}'")
        process = Foreman::Process.new(command)
        process.run
        log.info("Run complete.")
    end
  end
end<commit_msg>Initial using of foreman to run process.<commit_after>require 'trollop'
require 'json'
require 'pathname'
require 'logger'
require 'foreman/process'

module Politburo
  class CLI
    attr_reader :options
    attr_reader :targets

    def self.create(arguments)
      p = Trollop::Parser.new do
          version "politburo, version #{Politburo::VERSION}"
          banner <<-EOS
        Politburo - The Babushka-wielding DevOps orchestrator 

        Usage:
               politburo [options] resource[#state] [resource2[#state]]+
        where [options] are:
        EOS
        opt :envfile, "Use a different envfile", :short => 'e', :default => 'Envfile'
        opt :'babushka-sources-dir', "Use a different babushka sources dir", :short => 'b', :default => '~/.babushka/sources'
      end

      opts = Trollop::with_standard_exception_handling p do
        opts = p.parse arguments
        raise Trollop::HelpNeeded if arguments.empty? # show help screen

        opts
      end

      self.new(opts, arguments)
    end

    def initialize(options, targets)
      @options = options
      @targets = targets
    end

    def run()
      generate_to_path = target_generation_path

      begin
        generate_to(generate_to_path.realpath)
        run_babushka
      ensure
        generate_to_path.rmtree
        log.debug("Cleaned up target generation path: '#{generate_to_path.realdirpath}'")
      end
    end

    def generate_to(target_dir)
      target_deps_file = File.join(target_dir, 'politburo_generated_deps.rb')
      File.open(target_deps_file, "w") { | f | f.write(root.to_babushka_deps) }
      log.debug("Generated deps to: '#{target_deps_file}'")
    end

    def root()
      @root ||= Politburo::DSL.define(envfile_contents)
    end

    def envfile_contents()
      @envfile_contents ||= File.open(options[:envfile], "r") { | f | f.read() }
    end

    def babushka_sources_path()
      result_path = Pathname.new(File.expand_path(options[:'babushka-sources-dir']))
      raise "Babushka sources directory: '#{result_path.to_s}' does not exist or isn't a directory" unless result_path.directory?
      result_path
    end

    def target_generation_dirname
      @target_generation_dirname ||= "politburo-run-#{Time.now.to_i.to_s}"
    end

    def target_generation_path()
      @target_generation_path ||= begin 
        target_generation_path = babushka_sources_path + target_generation_dirname
        target_generation_path.mkdir
        raise "Could not create target generation path: '#{target_generation_path.realpath}'" unless target_generation_path.directory?
        log.debug("Created target generation path: '#{target_generation_path.realpath}' ")
        target_generation_path
      end
    end

    def log()
      @log ||= ::Logger.new(STDOUT)
    end

    private

    def run_babushka
        command = "babushka #{target_generation_dirname}:All#ready"
        log.debug("About to execute command: '#{command}'")
        process = Foreman::Process.new(command)
        process.run
        Process.waitall
        log.info("Run complete.")
    end
  end
end
{'fraction_non_alphanumeric': 0.10303916788558427, 'fraction_numerical': 0.00032504469364537625, 'mean_word_length': 2.9347826086956523, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3472981', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1859, 'n_words': 430}