<commit_before>require 'cl'
require 'logger'
require 'open3'
require 'tmpdir'

module Dpl
  module Ctx
    class Bash < Cl::Ctx
      attr_accessor :folds, :stdout, :stderr

      def initialize(stdout = $stdout, stderr = $stderr)
        @stdout, @stderr = stdout, stderr
        @folds = 0
        super('dpl')
      end

      # Folds any log output from the given block
      #
      # Starts a log fold with the given fold message, calls the block, and
      # closes the fold.
      #
      # @param msg [String] the message that will appear on the log fold
      def fold(msg, &block)
        self.folds += 1
        print "travis_fold:start:dpl.#{folds}\r\e[K"
        info "\e[33m#{msg}\e[0m"
        yield
      ensure
        print "\ntravis_fold:end:dpl.#{folds}\r\e[K"
      end

      # Outputs a deprecation warning for a given deprecated option key to stderr.
      #
      # @param key [Symbol] the deprecated option key
      # @param msg [String or Symbol] the deprecation message. if given a Symbol this will be wrapped into the string "Please use #{symbol}".
      def deprecate_opt(key, msg)
        msg = "please use #{msg}" if msg.is_a?(Symbol)
        warn "Deprecated option #{key} used (#{msg})."
      end

      # Outputs an info level message to stdout.
      def info(*msgs)
        stdout.puts(*msgs)
      end

      # Prints an info level message to stdout.
      #
      # This method does not append a newline character to the given message,
      # which usually is not the desired behaviour. The method is intended to
      # be used if an initial, partial message is supposed to be printed, which
      # will be completed later (using the method `info`).
      #
      # For example:
      #
      #   print 'Starting a long running task ...'
      #   run_long_running_task
      #   info 'done.'
      def print(chars)
        stdout.print(chars)
      end

      # Outputs an warning message to stderr
      #
      # This method is intended to be used for warning messages that are
      # supposed to show up in the build log, but do not qualify as errors that
      # would abort the deployment process. The warning will be highlighted as
      # red text. Use sparingly.
      def warn(*msgs)
        msgs = msgs.join("\n").lines
        msgs.each { |msg| stderr.puts("\e[31;1m#{msg}\e[0m") }
      end

      # Outputs an error message to stderr, and raises an error, halting the
      # deployment process.
      #
      # This method is intended to be used for all error conditions that
      # require the deployment process to be aborted.
      def error(message)
        raise Error, message
      end

      # Returns a logger
      #
      # Returns a logger instance, with the given log level set. This can be
      # used to pass to clients that accept a Ruby logger, such as Faraday,
      # for debugging purposes.
      #
      # Use with care.
      #
      # @param level [Symbol] the Ruby logger log level
      def logger(level = :info)
        logger = Logger.new(stderr)
        logger.level = Logger.const_get(level.to_s.upcase)
        logger
      end

      # Installs an APT package
      #
      # Installs the APT package with the given name, unless the command is already
      # available (as determined by `which [cmd]`.
      #
      # @param package [String] the package name
      # @param cmd [String] an executable installed by the package, defaults to the package name
      def apt_get(package, cmd = package)
        shell "sudo apt-get -qq install #{package}", echo: true, assert: true, retry: true unless which(cmd)
      end

      def gems_require(gems)
        missing = gems.reject { |name, version, _| gem_installed?(name, version) }
        gems_install(missing) if missing.any?
        gems.each { |gem| gem_require(*gem) }
      end

      # Requires source files from Ruby gems, installing them on demand if required
      #
      # Installs the Ruby gem with the given version, if not already installed, and
      # requires the specified source files from that gem.
      #
      # @param name    [String] Ruby gem name (required)
      # @param version [String] Ruby gem version (required)
      # @param opts    [Hash] options
      # @option opts [Array<String>, String] :require A single path or a list of paths to source files to require from this Ruby gem
      def gem_require(name, version, opts = {})
        gem_install(name, version) unless gem_installed?(name, version)
        Array(opts[:require] || name).each { |path| require(path) }
      end

      # Installs multiple Ruby gems with the specified version.
      def gems_install(gems)
        gems = gems.map { |name, version, _| "#{name}:#{version.inspect}" }.join(' ')<commit_msg>try running apt-get update first<commit_after>require 'cl'
require 'logger'
require 'open3'
require 'tmpdir'

module Dpl
  module Ctx
    class Bash < Cl::Ctx
      attr_accessor :folds, :stdout, :stderr

      def initialize(stdout = $stdout, stderr = $stderr)
        @stdout, @stderr = stdout, stderr
        @folds = 0
        super('dpl')
      end

      # Folds any log output from the given block
      #
      # Starts a log fold with the given fold message, calls the block, and
      # closes the fold.
      #
      # @param msg [String] the message that will appear on the log fold
      def fold(msg, &block)
        self.folds += 1
        print "travis_fold:start:dpl.#{folds}\r\e[K"
        info "\e[33m#{msg}\e[0m"
        yield
      ensure
        print "\ntravis_fold:end:dpl.#{folds}\r\e[K"
      end

      # Outputs a deprecation warning for a given deprecated option key to stderr.
      #
      # @param key [Symbol] the deprecated option key
      # @param msg [String or Symbol] the deprecation message. if given a Symbol this will be wrapped into the string "Please use #{symbol}".
      def deprecate_opt(key, msg)
        msg = "please use #{msg}" if msg.is_a?(Symbol)
        warn "Deprecated option #{key} used (#{msg})."
      end

      # Outputs an info level message to stdout.
      def info(*msgs)
        stdout.puts(*msgs)
      end

      # Prints an info level message to stdout.
      #
      # This method does not append a newline character to the given message,
      # which usually is not the desired behaviour. The method is intended to
      # be used if an initial, partial message is supposed to be printed, which
      # will be completed later (using the method `info`).
      #
      # For example:
      #
      #   print 'Starting a long running task ...'
      #   run_long_running_task
      #   info 'done.'
      def print(chars)
        stdout.print(chars)
      end

      # Outputs an warning message to stderr
      #
      # This method is intended to be used for warning messages that are
      # supposed to show up in the build log, but do not qualify as errors that
      # would abort the deployment process. The warning will be highlighted as
      # red text. Use sparingly.
      def warn(*msgs)
        msgs = msgs.join("\n").lines
        msgs.each { |msg| stderr.puts("\e[31;1m#{msg}\e[0m") }
      end

      # Outputs an error message to stderr, and raises an error, halting the
      # deployment process.
      #
      # This method is intended to be used for all error conditions that
      # require the deployment process to be aborted.
      def error(message)
        raise Error, message
      end

      # Returns a logger
      #
      # Returns a logger instance, with the given log level set. This can be
      # used to pass to clients that accept a Ruby logger, such as Faraday,
      # for debugging purposes.
      #
      # Use with care.
      #
      # @param level [Symbol] the Ruby logger log level
      def logger(level = :info)
        logger = Logger.new(stderr)
        logger.level = Logger.const_get(level.to_s.upcase)
        logger
      end

      # Installs an APT package
      #
      # Installs the APT package with the given name, unless the command is already
      # available (as determined by `which [cmd]`.
      #
      # @param package [String] the package name
      # @param cmd [String] an executable installed by the package, defaults to the package name
      def apt_get(package, cmd = package)
        return if which(cmd)
        shell 'sudo apt-get update', echo: true, assert: true, retry: true
        shell "sudo apt-get -qq install #{package}", echo: true, assert: true, retry: true
      end

      def gems_require(gems)
        missing = gems.reject { |name, version, _| gem_installed?(name, version) }
        gems_install(missing) if missing.any?
        gems.each { |gem| gem_require(*gem) }
      end

      # Requires source files from Ruby gems, installing them on demand if required
      #
      # Installs the Ruby gem with the given version, if not already installed, and
      # requires the specified source files from that gem.
      #
      # @param name    [String] Ruby gem name (required)
      # @param version [String] Ruby gem version (required)
      # @param opts    [Hash] options
      # @option opts [Array<String>, String] :require A single path or a list of paths to source files to require from this Ruby gem
      def gem_require(name, version, opts = {})
        gem_install(name, version) unless gem_installed?(name, version)
        Array(opts[:require] || name).each { |path| require(path) }
      end

      # Installs multiple Ruby gems with the specified version.
      def gems_install(gems)
        gems = gems.map { |name, version, _| "#{name}:#{version.inspect}" }.join(' ')
{'fraction_non_alphanumeric': 0.087074263097594, 'fraction_numerical': 0.0020831163420477034, 'mean_word_length': 2.313319530710835, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1957846', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2680, 'n_words': 1103}