<commit_before>require 'securerandom'
require 'etc'

require_relative '../../commons/integration/docker'
require_relative '../../commons/integration/strace'
require_relative '../../commons/integration/md5sum'
require_relative '../../commons/logging'
require_relative '../../commons/model/change_tracking'
require_relative '../../commons/utils/md5'
require_relative '../../commons/utils/system'
require_relative 'file_status'

module Citac
  module ChangeTrackers
    module Docker
      module ChangeTracker
        class << self
          def track_changes(command, settings, options = {})
            exclusion_patterns = settings.exclusion_patterns.dup
            add_default_exclusion_patterns exclusion_patterns

            snapshot_image = create_snapshot_image

            strace_opts = options.dup
            strace_opts[:start_markers] = settings.start_markers
            strace_opts[:end_markers] = settings.end_markers

            accessed_files, result, syscalls = Citac::Integration::Strace.track_file_access command, strace_opts
            accessed_files.each {|f| log_debug $prog_name, "Accessed file: #{f}"}

            accessed_files.reject! { |f| exclusion_patterns.any? { |p| f =~ p } }

            change_summary = compare_files snapshot_image, accessed_files
            change_summary.additional_data[:syscalls] = syscalls.join $/
            return change_summary, result
          ensure
            remove_snapshot_image snapshot_image
          end

          private

          def add_default_exclusion_patterns(patterns)
            patterns << /^\/dev\//
            patterns << /^\/proc\//
            patterns << /^\/sys\//
            patterns << /^\/opt\/citac\//
            patterns << /^\/tmp\/citac\//
            patterns << /^\/var\/lib\/puppet\//
          end

          def create_snapshot_image
            hostname = Citac::Utils::System.hostname
            repository_name = "#{hostname}_snapshots"
            tag = SecureRandom.uuid

            Citac::Integration::Docker.commit hostname, repository_name, tag
          end

          def remove_snapshot_image(image)
            Citac::Integration::Docker.remove_image image if image<commit_msg>Improved default exclusion patterns to include base directories as well.<commit_after>require 'securerandom'
require 'etc'

require_relative '../../commons/integration/docker'
require_relative '../../commons/integration/strace'
require_relative '../../commons/integration/md5sum'
require_relative '../../commons/logging'
require_relative '../../commons/model/change_tracking'
require_relative '../../commons/utils/md5'
require_relative '../../commons/utils/system'
require_relative 'file_status'

module Citac
  module ChangeTrackers
    module Docker
      module ChangeTracker
        class << self
          def track_changes(command, settings, options = {})
            exclusion_patterns = settings.exclusion_patterns.dup
            add_default_exclusion_patterns exclusion_patterns

            snapshot_image = create_snapshot_image

            strace_opts = options.dup
            strace_opts[:start_markers] = settings.start_markers
            strace_opts[:end_markers] = settings.end_markers

            accessed_files, result, syscalls = Citac::Integration::Strace.track_file_access command, strace_opts
            accessed_files.each {|f| log_debug $prog_name, "Accessed file: #{f}"}

            accessed_files.reject! { |f| exclusion_patterns.any? { |p| f =~ p } }

            change_summary = compare_files snapshot_image, accessed_files
            change_summary.additional_data[:syscalls] = syscalls.join $/
            return change_summary, result
          ensure
            remove_snapshot_image snapshot_image
          end

          private

          def add_default_exclusion_patterns(patterns)
            patterns << /^\/dev(\/|$)/
            patterns << /^\/proc(\/|$)/
            patterns << /^\/sys(\/|$)/
            patterns << /^\/opt\/citac(\/|$)/
            patterns << /^\/tmp\/citac(\/|$)/
            patterns << /^\/var\/lib\/puppet(\/|$)/
          end

          def create_snapshot_image
            hostname = Citac::Utils::System.hostname
            repository_name = "#{hostname}_snapshots"
            tag = SecureRandom.uuid

            Citac::Integration::Docker.commit hostname, repository_name, tag
          end

          def remove_snapshot_image(image)
            Citac::Integration::Docker.remove_image image if image
{'fraction_non_alphanumeric': 0.11328733585577565, 'fraction_numerical': 0.0008902737591809482, 'mean_word_length': 2.3612565445026177, 'pattern_counts': {'":': 0, '<': 31, '<?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': '1208859', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1220, 'n_words': 234}