<commit_before>require 'fileutils'

module BackupJenkins
  class Backup
    include BackupJenkins::Formatter

    def initialize(config = Config.new)
      @config = config
    end

    def do_backup
      raise "Backup directory already exists! (#{backup_directory})" if FileTest.directory?(backup_directory)

      copy_files
      create_tarball
      remove_temporary_files
      remove_old_backups
    rescue Interrupt
      puts "Cleaning up..."
      clean_up
    end

    def tarball_filename
      "#{backup_directory}.tar.bz2"
    end

    def list_local_files
      format_backup_file_data(backup_files)
    end

    def clean_up
      puts "Removing #{backup_directory}"
      remove_temporary_files

      puts "Removing #{tarball_filename}"
      FileUtils.rm_rf(tarball_filename)
    rescue Errno::ENOENT
    end

    private

    attr_reader :config

    def backup_directory
      @backup_directory ||= "#{config.backup["dir_base"]}/#{config.base_file_name}_#{timestamp}"
    end

    def timestamp
      Time.now.strftime("%Y%m%d_%H%M")
    end

    def copy_files
      create_dir_and_copy(plugin_files)
      create_dir_and_copy(user_content_files)
      create_dir_and_copy(jobs_files)
    end

    def create_dir_and_copy(file_names)
      file_names.each do |file_name|
        create_dir_and_copy_impl(file_name)
      end
    end

    def create_dir_and_copy_impl(file_name)
      raise "file '#{file_name}' does not exist" unless FileTest.file?(file_name)

      new_file_name = new_file_path(file_name)
      new_file_dir = File.dirname(new_file_name)

      FileUtils.mkdir_p(new_file_dir, :verbose => config.verbose)
      FileUtils.cp(file_name, new_file_name, :verbose => config.verbose)
    end

    def new_file_path(file_name)
      "#{backup_directory}/#{file_name.gsub(%r{#{config.jenkins["home"]}}, "")}".gsub(%r{//}, '/')
    end

    def plugin_files
      Dir["#{config.jenkins["home"]}/plugins/*.jpi"] +
        Dir["#{config.jenkins["home"]}/plugins/*.jpi.pinned"] +
        Dir["#{config.jenkins["home"]}/plugins/*.jpi.disabled"]
    end

    def user_content_files
      Dir["#{config.jenkins["home"]}/userContent/*"]
    end

    def jobs_files
      `find #{config.jenkins["home"]}/jobs -maxdepth 3 -name config.xml -or -name nextBuildNumber`.split(/\n/)
    end

    def create_tarball
      Dir.chdir(backup_directory)
      %x{tar #{tar_options} #{tarball_filename} .}
      raise "Error creating tarball" unless FileTest.file?(tarball_filename)
    end

    def tar_options
      %w(j c f).tap do |options|
        options.unshift('v') if config.verbose
      end.join('')
    end

    def remove_old_backups
      files_to_remove.each do |file|
        FileUtils.rm(file)
      end
    end
<commit_msg>Feedback on creating tarball if verbose<commit_after>require 'fileutils'

module BackupJenkins
  class Backup
    include BackupJenkins::Formatter

    def initialize(config = Config.new)
      @config = config
    end

    def do_backup
      raise "Backup directory already exists! (#{backup_directory})" if FileTest.directory?(backup_directory)

      copy_files
      create_tarball
      remove_temporary_files
      remove_old_backups
    rescue Interrupt
      puts "Cleaning up..."
      clean_up
    end

    def tarball_filename
      "#{backup_directory}.tar.bz2"
    end

    def list_local_files
      format_backup_file_data(backup_files)
    end

    def clean_up
      puts "Removing #{backup_directory}"
      remove_temporary_files

      puts "Removing #{tarball_filename}"
      FileUtils.rm_rf(tarball_filename)
    rescue Errno::ENOENT
    end

    private

    attr_reader :config

    def backup_directory
      @backup_directory ||= "#{config.backup["dir_base"]}/#{config.base_file_name}_#{timestamp}"
    end

    def timestamp
      Time.now.strftime("%Y%m%d_%H%M")
    end

    def copy_files
      create_dir_and_copy(plugin_files)
      create_dir_and_copy(user_content_files)
      create_dir_and_copy(jobs_files)
    end

    def create_dir_and_copy(file_names)
      file_names.each do |file_name|
        create_dir_and_copy_impl(file_name)
      end
    end

    def create_dir_and_copy_impl(file_name)
      raise "file '#{file_name}' does not exist" unless FileTest.file?(file_name)

      new_file_name = new_file_path(file_name)
      new_file_dir = File.dirname(new_file_name)

      FileUtils.mkdir_p(new_file_dir, :verbose => config.verbose)
      FileUtils.cp(file_name, new_file_name, :verbose => config.verbose)
    end

    def new_file_path(file_name)
      "#{backup_directory}/#{file_name.gsub(%r{#{config.jenkins["home"]}}, "")}".gsub(%r{//}, '/')
    end

    def plugin_files
      Dir["#{config.jenkins["home"]}/plugins/*.jpi"] +
        Dir["#{config.jenkins["home"]}/plugins/*.jpi.pinned"] +
        Dir["#{config.jenkins["home"]}/plugins/*.jpi.disabled"]
    end

    def user_content_files
      Dir["#{config.jenkins["home"]}/userContent/*"]
    end

    def jobs_files
      `find #{config.jenkins["home"]}/jobs -maxdepth 3 -name config.xml -or -name nextBuildNumber`.split(/\n/)
    end

    def create_tarball
      puts "Creating #{tarball_filename}..." if config.verbose
      Dir.chdir(backup_directory)
      %x{tar #{tar_options} #{tarball_filename} .}
      raise "Error creating tarball" unless FileTest.file?(tarball_filename)
    end

    def tar_options
      %w(j c f).tap do |options|
        options.unshift('v') if config.verbose
      end.join('')
    end

    def remove_old_backups
      files_to_remove.each do |file|
        FileUtils.rm(file)
      end
    end

{'fraction_non_alphanumeric': 0.11206434316353887, 'fraction_numerical': 0.0007149240393208221, 'mean_word_length': 3.0346070656092285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '747945', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1846, 'n_words': 338}