<commit_before>require 'zip'

module Paco
module Env
  class Base
    attr_reader :test_path, :project_path

    # constructor
    #
    # @raise RuntimeError
    def initialize
      raise 'error. check PACO_TEST_PATH env.' \
        if !ENV['PACO_TEST_PATH']

      @test_path = ENV['PACO_TEST_PATH'].sub(/\/$/, '')
    end

    # install package
    #
    # @raise RuntimeError
    # @param [Paco::Specification]
    # @param [Array] パッケージに含まれるファイルのパス
    # @return [nil]
    def install(spec, files)
      raise 'error. not implemented.'
    end

    # build package
    #
    # @raise RuntimeError
    # @param [Paco::Specification]
    # @return [Array] パッケージに含めるファイルのパス
    def build(spec)
      raise 'error. not implemented.'
    end

    # test package
    #
    # @raise RuntimeError
    # @return [nil]
    def test
      raise 'error. not implemented.'
    end

    # cleanup PACO_TEST_PATH
    # @raise RuntimeError
    # @return [nil]
    def cleanup
      if Dir.exist?(@test_path) then
        Dir.entries(@test_path).each do |path|
          next if path.match(/^\.{1,2}$/) # skip . or ..

          absolute_path = sprintf("%s/%s", @test_path, path)
          if File.directory?(absolute_path)
            FileUtils.remove_dir(absolute_path)
          else
            FileUtils.remove(absolute_path, {:force => true, :verbose => true})
          end
        end
      end

      nil
    end

    # zip files
    # @param
    # @param
    # @return nil
    def zip(zipfile, files)
      FileUtils.remove(zipfile) if File.exist?(zipfile)

      Zip::File.open(zipfile, Zip::File::CREATE) do |zipfile|
        files.each do |filename|
          zipfile.add(filename, filename)
        end
      end

      nil
    end

    # unzip archive
    # @return [Array] files
    def unzip(file)
      spec  = nil
      file  = File.expand_path(file)
      files = []
      Zip::File.open(file) do |zipfile|
        zipfile.each do |entry|
          destfile = @project_path + '/' + entry.name

          FileUtils.remove(entry.name, {:verbose => true}) if File.exist?(entry.name)
          puts "Extract #{entry.name}"
          entry.extract(destfile)

          if File.extname(entry.name) == '.paco' then
            spec = eval File.read(destfile)
          end

          files.push entry.name
        end
      end

      files<commit_msg>prompt if file already exist<commit_after>require 'zip'

module Paco
module Env
  class Base
    attr_reader :test_path, :project_path

    # constructor
    #
    # @raise RuntimeError
    def initialize
      raise 'error. check PACO_TEST_PATH env.' \
        if !ENV['PACO_TEST_PATH']

      @test_path = ENV['PACO_TEST_PATH'].sub(/\/$/, '')
    end

    # install package
    #
    # @raise RuntimeError
    # @param [Paco::Specification]
    # @param [Array] パッケージに含まれるファイルのパス
    # @return [nil]
    def install(spec, files)
      raise 'error. not implemented.'
    end

    # build package
    #
    # @raise RuntimeError
    # @param [Paco::Specification]
    # @return [Array] パッケージに含めるファイルのパス
    def build(spec)
      raise 'error. not implemented.'
    end

    # test package
    #
    # @raise RuntimeError
    # @return [nil]
    def test
      raise 'error. not implemented.'
    end

    # cleanup PACO_TEST_PATH
    # @raise RuntimeError
    # @return [nil]
    def cleanup
      if Dir.exist?(@test_path) then
        Dir.entries(@test_path).each do |path|
          next if path.match(/^\.{1,2}$/) # skip . or ..

          absolute_path = sprintf("%s/%s", @test_path, path)
          if File.directory?(absolute_path)
            FileUtils.remove_dir(absolute_path)
          else
            FileUtils.remove(absolute_path, {:force => true, :verbose => true})
          end
        end
      end

      nil
    end

    # zip files
    # @param
    # @param
    # @return nil
    def zip(zipfile, files)
      FileUtils.remove(zipfile) if File.exist?(zipfile)

      Zip::File.open(zipfile, Zip::File::CREATE) do |zipfile|
        files.each do |filename|
          zipfile.add(filename, filename)
        end
      end

      nil
    end

    # unzip archive
    # @return [Array] files
    def unzip(file)
      spec  = nil
      file  = File.expand_path(file)
      files = []
      Zip::File.open(file) do |zipfile|
        zipfile.each do |entry|
          destfile = @project_path + '/' + entry.name

          FileUtils.remove(entry.name, {:verbose => true}) if File.exist?(entry.name)
          puts "Extract #{entry.name}"
          entry.extract(destfile) do |entry,destfile|
            puts sprintf("%s is already exist. overwrite? [y/N]", entry.name)
            ['y','Y'].include?($stdin.gets.chomp)
          end

          if File.extname(entry.name) == '.paco' then
            spec = eval File.read(destfile)
          end

          files.push entry.name
        end
      end

      files
{'fraction_non_alphanumeric': 0.1164790174002047, 'fraction_numerical': 0.0008188331627430911, 'mean_word_length': 1.9810860280658937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '878188', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1567, 'n_words': 373}