<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: Apache License, Version 2.0
#
# 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.
#


require 'tmpdir'
require 'fcntl'
require 'etc'

class Chef
  module Mixin
    module Command
      
      def run_command(args={})         
        if args.has_key?(:creates)
          if File.exists?(args[:creates])
            Chef::Log.debug("Skipping #{args[:command_string]} - creates #{args[:creates]} exists.")
            return false
          end
        end
        
        if args.has_key?(:onlyif)
          status = popen4(args[:onlyif]) { |p, i, o, e| }
          if status.exitstatus != 0
            Chef::Log.debug("Skipping #{args[:command_string]} - onlyif #{args[:onlyif]} returned #{status.exitstatus}")
            return false
          end
        end
        
        if args.has_key?(:unless)
          status = popen4(args[:unless]) { |p, i, o, e| }
          if status.exitstatus == 0
            Chef::Log.debug("Skipping #{args[:command_string]} - unless #{args[:unless]} returned #{status.exitstatus}")
            return false
          end
        end
        
        exec_processing_block = lambda do |pid, stdin, stdout, stderr|
          stdin.close

          stdout_string = stdout.gets(nil)
          if stdout_string
            Chef::Log.debug("---- Begin #{args[:command_string]} STDOUT ----")
            Chef::Log.debug(stdout_string.strip)
            Chef::Log.debug("---- End #{args[:command_string]} STDOUT ----")
          end
          stderr_string = stderr.gets(nil)
          if stderr_string
            Chef::Log.debug("---- Begin #{args[:command_string]} STDERR ----")
            Chef::Log.debug(stderr_string.strip)
            Chef::Log.debug("---- End #{args[:command_string]} STDERR ----")
          end
        end
        
        args[:cwd] ||= Dir.tmpdir        
        unless File.directory?(args[:cwd])
          raise Chef::Exception::Exec, "#{args[:cwd]} does not exist or is not a directory"
        end
        
        status = nil
        Dir.chdir(args[:cwd]) do
          if args[:timeout]
            begin
              Timeout.timeout(args[:timeout]) do
                status = popen4(args[:command], args, &exec_processing_block)
              end
            rescue Exception => e
              Chef::Log.error("#{args[:command_string]} exceeded timeout #{args[:timeout]}")
              raise(e)
            end
          else
            status = popen4(args[:command], args, &exec_processing_block)
          end
        
          if status.exitstatus != args[:returns]
            raise Chef::Exception::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}"
          else
            Chef::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
          end
        end
        status
      end
      <commit_msg>Updating command to have a default returns argument of 0<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: Apache License, Version 2.0
#
# 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.
#


require 'tmpdir'
require 'fcntl'
require 'etc'

class Chef
  module Mixin
    module Command
      
      def run_command(args={})         
        if args.has_key?(:creates)
          if File.exists?(args[:creates])
            Chef::Log.debug("Skipping #{args[:command_string]} - creates #{args[:creates]} exists.")
            return false
          end
        end
        
        if args.has_key?(:onlyif)
          status = popen4(args[:onlyif]) { |p, i, o, e| }
          if status.exitstatus != 0
            Chef::Log.debug("Skipping #{args[:command_string]} - onlyif #{args[:onlyif]} returned #{status.exitstatus}")
            return false
          end
        end
        
        if args.has_key?(:unless)
          status = popen4(args[:unless]) { |p, i, o, e| }
          if status.exitstatus == 0
            Chef::Log.debug("Skipping #{args[:command_string]} - unless #{args[:unless]} returned #{status.exitstatus}")
            return false
          end
        end
        
        exec_processing_block = lambda do |pid, stdin, stdout, stderr|
          stdin.close

          stdout_string = stdout.gets(nil)
          if stdout_string
            Chef::Log.debug("---- Begin #{args[:command_string]} STDOUT ----")
            Chef::Log.debug(stdout_string.strip)
            Chef::Log.debug("---- End #{args[:command_string]} STDOUT ----")
          end
          stderr_string = stderr.gets(nil)
          if stderr_string
            Chef::Log.debug("---- Begin #{args[:command_string]} STDERR ----")
            Chef::Log.debug(stderr_string.strip)
            Chef::Log.debug("---- End #{args[:command_string]} STDERR ----")
          end
        end
        
        args[:cwd] ||= Dir.tmpdir        
        unless File.directory?(args[:cwd])
          raise Chef::Exception::Exec, "#{args[:cwd]} does not exist or is not a directory"
        end
        
        status = nil
        Dir.chdir(args[:cwd]) do
          if args[:timeout]
            begin
              Timeout.timeout(args[:timeout]) do
                status = popen4(args[:command], args, &exec_processing_block)
              end
            rescue Exception => e
              Chef::Log.error("#{args[:command_string]} exceeded timeout #{args[:timeout]}")
              raise(e)
            end
          else
            status = popen4(args[:command], args, &exec_processing_block)
          end
        
          args[:returns] ||= 0
          if status.exitstatus != args[:returns]
            raise Chef::Exception::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}"
          else
            Chef::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
          end
        end
        status
      end
      
{'fraction_non_alphanumeric': 0.1283147989734816, 'fraction_numerical': 0.004847447961220416, 'mean_word_length': 2.086229652441707, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '5492497', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1941, 'n_words': 541}