<commit_before>module VagrantPlugins
  module ProviderKvm
    module Action
      class SetName
        def initialize(app, env)
          @logger = Log4r::Logger.new("vagrant::action::vm::setname")
          @app = app
        end

        def call(env)
          name = env[:machine].provider_config.name

          if !name
            prefix = env[:root_path].basename.to_s
            prefix.gsub!(/[^-a-z0-9_]/i, "")
            name = prefix + "_#{Time.now.to_i}"
          end

          # @todo raise error if name is taken in kvm
          # @todo don't set the name if the vm already has a name

          @logger.info("Setting the name of the VM: #{name}")
          env[:machine].provider.driver.set_name(name)

          @app.call(env)
        end

      end
    end
  end
end<commit_msg>set name with machine name<commit_after>module VagrantPlugins
  module ProviderKvm
    module Action
      class SetName
        def initialize(app, env)
          @logger = Log4r::Logger.new("vagrant::action::vm::setname")
          @app = app
        end

        def call(env)
          name = env[:machine].provider_config.name

          # If we already set the name before, then don't do anything
          sentinel = env[:machine].data_dir.join("action_set_name")
          if !name && sentinel.file?
            @logger.info("Default name was already set before, not doing it again.")
            return @app.call(env)
          end

          # If no name was manually set, then use a default
          if !name
            prefix = "#{env[:root_path].basename.to_s}_#{env[:machine].name}"
            prefix.gsub!(/[^-a-z0-9_]/i, "")
            name = prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}"
          end

          # @todo raise error if name is taken in kvm
          # Verify the name is not taken
          # vms = env[:machine].provider.driver.read_vms
          #raise Vagrant::Errors::VMNameExists, :name => name if \
          #  vms.has_key?(name) && vms[name] != env[:machine].id
          #
          #if vms.has_key?(name)
          #  @logger.info("Not setting the name because our name is already set.")
          #else
          #  env[:ui].info(I18n.t(
          #    "vagrant.actions.vm.set_name.setting_name", name: name))
          #  env[:machine].provider.driver.set_name(name)
          #end

          @logger.info("Setting the name of the VM: #{name}")
          env[:machine].provider.driver.set_name(name)

          # Create the sentinel
          sentinel.open("w") do |f|
            f.write(Time.now.to_i.to_s)
          end

          @app.call(env)
        end

      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12085843373493976, 'fraction_numerical': 0.007153614457831325, 'mean_word_length': 1.8356456776947705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2344745', 'n_tokens_mistral': 836, 'n_tokens_neox': 811, 'n_words': 202}