<commit_before>require "train/platforms/detect/helpers/os_linux"
require "train/platforms/detect/helpers/os_windows"
require "rbconfig"

module Train::Platforms::Detect::Helpers
  module OSCommon
    include Train::Platforms::Detect::Helpers::Linux
    include Train::Platforms::Detect::Helpers::Windows

    def ruby_host_os(regex)
      ::RbConfig::CONFIG["host_os"] =~ regex
    end

    def winrm?
      backend_name == "TrainPlugins::WinRM::Connection"
    end

    def backend_name
      @backend.class.name
    end

    def unix_file_contents(path)
      # keep a log of files incase multiple checks call the same one
      return @files[path] if @files.key?(path)

      if ARGV.include? "--sudo"
        res = @backend.run_command("sudo test -f #{path} && sudo cat #{path}")
      else
        res = @backend.run_command("test -f #{path} && cat #{path}")
      end
      # ignore files that can't be read
      @files[path] = res.exit_status == 0 ? res.stdout : nil
      @files[path]
    end

    def unix_file_exist?(path)
      @backend.run_command("test -f #{path}").exit_status == 0
    end

    def command_output(cmd)
      res = @backend.run_command(cmd).stdout
      # When you try to execute command using ssh connction as root user and you have provided ssh user identity file
      # it gives standard output to login as authorised user other than root. To show this standard ouput as an error
      # to user we are matching the string of stdout and raising the error here so that user gets exact information.
      if @backend.class.to_s == "Train::Transports::SSH::Connection" && res =~ /Please login as the user/
        raise Train::UserError, "SSH failed: #{res}"
      end

      res.strip! unless res.nil?
      res
    end

    def unix_uname_s
      return @uname[:s] if @uname.key?(:s)

      if ARGV.include? "--sudo"
        @uname[:s] = command_output("sudo uname -s")
      else
        @uname[:s] = command_output("uname -s")
      end
    end

    def unix_uname_r
      return @uname[:r] if @uname.key?(:r)

      if ARGV.include? "--sudo"
        @uname[:r] = command_output("sudo uname -r")
      else
        @uname[:r] = command_output("uname -r")
      end
    end

    def unix_uname_m
      return @uname[:m] if @uname.key?(:m)

      if ARGV.include? "--sudo"
        @uname[:m] = command_output("sudo uname -m")
      else
        @uname[:m] = command_output("uname -m")
      end
    end

    def brocade_version
      return @cache[:brocade] if @cache.key?(:brocade)

      res = command_output("version")

      m = res.match(/^Fabric OS:\s+v(\S+)$/)
      unless m.nil?
        return @cache[:brocade] = { version: m[1], type: "fos" }
      end

      @cache[:brocade] = nil
    end

    def cisco_show_version
      return @cache[:cisco] if @cache.key?(:cisco)

      res = command_output("show version")

      m = res.match(/Cisco IOS Software, [^,]+? \(([^,]+?)\), Version (\d+\.\d+)/)
      unless m.nil?
        return @cache[:cisco] = { version: m[2], model: m[1], type: "ios" }
      end

      m = res.match(/Cisco IOS Software, IOS-XE Software, [^,]+? \(([^,]+?)\), Version (\d+\.\d+\.\d+[A-Z]*)/)<commit_msg>Update os_common.rb<commit_after>require "train/platforms/detect/helpers/os_linux"
require "train/platforms/detect/helpers/os_windows"
require "rbconfig"

module Train::Platforms::Detect::Helpers
  module OSCommon
    include Train::Platforms::Detect::Helpers::Linux
    include Train::Platforms::Detect::Helpers::Windows

    def ruby_host_os(regex)
      ::RbConfig::CONFIG["host_os"] =~ regex
    end

    def winrm?
      backend_name == "TrainPlugins::WinRM::Connection"
    end

    def backend_name
      @backend.class.name
    end

    def unix_file_contents(path)
      # keep a log of files incase multiple checks call the same one
      return @files[path] if @files.key?(path)

      res = @backend.run_command("test -f #{path} && cat #{path}")
      
      # ignore files that can't be read
      @files[path] = res.exit_status == 0 ? res.stdout : nil
      @files[path]
    end

    def unix_file_exist?(path)
      @backend.run_command("test -f #{path}").exit_status == 0
    end

    def command_output(cmd)
      res = @backend.run_command(cmd).stdout
      # When you try to execute command using ssh connction as root user and you have provided ssh user identity file
      # it gives standard output to login as authorised user other than root. To show this standard ouput as an error
      # to user we are matching the string of stdout and raising the error here so that user gets exact information.
      if @backend.class.to_s == "Train::Transports::SSH::Connection" && res =~ /Please login as the user/
        raise Train::UserError, "SSH failed: #{res}"
      end

      res.strip! unless res.nil?
      res
    end

    def unix_uname_s
      return @uname[:s] if @uname.key?(:s)
      @uname[:s] = command_output("uname -s")
    end

    def unix_uname_r
      return @uname[:r] if @uname.key?(:r)

      @uname[:r] = command_output("uname -r")
    end

    def unix_uname_m
      return @uname[:m] if @uname.key?(:m)
      
      @uname[:m] = command_output("uname -m")
    end

    def brocade_version
      return @cache[:brocade] if @cache.key?(:brocade)

      res = command_output("version")

      m = res.match(/^Fabric OS:\s+v(\S+)$/)
      unless m.nil?
        return @cache[:brocade] = { version: m[1], type: "fos" }
      end

      @cache[:brocade] = nil
    end

    def cisco_show_version
      return @cache[:cisco] if @cache.key?(:cisco)

      res = command_output("show version")

      m = res.match(/Cisco IOS Software, [^,]+? \(([^,]+?)\), Version (\d+\.\d+)/)
      unless m.nil?
        return @cache[:cisco] = { version: m[2], model: m[1], type: "ios" }
      end

      m = res.match(/Cisco IOS Software, IOS-XE Software, [^,]+? \(([^,]+?)\), Version (\d+\.\d+\.\d+[A-Z]*)/)
{'fraction_non_alphanumeric': 0.1441884107082345, 'fraction_numerical': 0.0016943409013893595, 'mean_word_length': 2.7055869428750783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5387860', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1958, 'n_words': 526}