<commit_before>
# vim:tabstop=2:sw=2:et:

require 'json'
require 'systemu'
require 'tempfile'
require 'fileutils'

module Cangallo

  class Qcow2
    attr_reader :path

    def initialize(path=nil)
      @path=path
    end

    def info
      res = execute :info, '--output=json', @path

      JSON.parse res
    end

    def compress(destination = nil, parent = nil)
      copy(destination, :parent => parent, :compress => true)
    end

    def copy(destination = nil, options = {})
      ops = {
        :parent => nil,
        :compress => true
      }.merge(options)

      parent = ops[:parent]

      new_path = destination || @path + '.compressed'

      command = [:convert, "-p", "-O qcow2"]
      #command = ["convert", "-p", "-O qcow2"]
      command << '-c' if ops[:compress]
      command << "-o backing_file=#{parent}" if parent
      command += [@path, new_path]

      execute *command

      # pp command
      # system(*command)

      if !destination
        begin
          File.rm @path
          File.mv new_path, @path
        ensure
          File.rm new_path if File.exist? new_path
        end
      else
        @path = new_path
      end
    end

    def sparsify(destination)
      parent = info['backing_file']
      parent_options = ''

      parent_options = "-o backing_file=#{parent}" if parent

      command = "TMPDIR=#{File.dirname(destination)} virt-sparsify #{parent_options} #{@path} #{destination}"
      status, stdout, stderr = systemu command
    end

    def sha1
      command = "guestfish --progress-bars --ro -a #{@path} " <<
                "run : checksum-device sha1 /dev/sda"
      %x{#{command}}
    end

    def rebase(new_base)
      execute :rebase, '-u', "-b #{new_base}", @path
    end

    def execute(command, *params)
      self.class.execute(command, params)
    end

    def self.execute(command, *params)
      command = "qemu-img #{command} #{params.join(' ')}"
      STDERR.puts command

      status, stdout, stderr = systemu command

      if status.success?
        stdout
      else
        raise stderr
      end<commit_msg>Stript the output of sha1 computing command<commit_after>
# vim:tabstop=2:sw=2:et:

require 'json'
require 'systemu'
require 'tempfile'
require 'fileutils'

module Cangallo

  class Qcow2
    attr_reader :path

    def initialize(path=nil)
      @path=path
    end

    def info
      res = execute :info, '--output=json', @path

      JSON.parse res
    end

    def compress(destination = nil, parent = nil)
      copy(destination, :parent => parent, :compress => true)
    end

    def copy(destination = nil, options = {})
      ops = {
        :parent => nil,
        :compress => true
      }.merge(options)

      parent = ops[:parent]

      new_path = destination || @path + '.compressed'

      command = [:convert, "-p", "-O qcow2"]
      #command = ["convert", "-p", "-O qcow2"]
      command << '-c' if ops[:compress]
      command << "-o backing_file=#{parent}" if parent
      command += [@path, new_path]

      execute *command

      # pp command
      # system(*command)

      if !destination
        begin
          File.rm @path
          File.mv new_path, @path
        ensure
          File.rm new_path if File.exist? new_path
        end
      else
        @path = new_path
      end
    end

    def sparsify(destination)
      parent = info['backing_file']
      parent_options = ''

      parent_options = "-o backing_file=#{parent}" if parent

      command = "TMPDIR=#{File.dirname(destination)} virt-sparsify #{parent_options} #{@path} #{destination}"
      status, stdout, stderr = systemu command
    end

    def sha1
      command = "guestfish --progress-bars --ro -a #{@path} " <<
                "run : checksum-device sha1 /dev/sda"
      %x{#{command}}.strip
    end

    def rebase(new_base)
      execute :rebase, '-u', "-b #{new_base}", @path
    end

    def execute(command, *params)
      self.class.execute(command, params)
    end

    def self.execute(command, *params)
      command = "qemu-img #{command} #{params.join(' ')}"
      STDERR.puts command

      status, stdout, stderr = systemu command

      if status.success?
        stdout
      else
        raise stderr
      end
{'fraction_non_alphanumeric': 0.1270104068117313, 'fraction_numerical': 0.003547776726584674, 'mean_word_length': 2.184487951807229, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '6097248', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1295, 'n_words': 357}