From eb07788db19db5b29debb545eb5ff83f59ed74fe Mon Sep 17 00:00:00 2001 From: David Calavera Date: Tue, 13 Mar 2012 21:29:38 -0700 Subject: [PATCH] Remove global variables --- lib/vagrant/command/base.rb | 4 +- lib/vagrant/driver/virtualbox_4_0.rb | 131 ++++++++++++++------------- lib/vagrant/driver/virtualbox_4_1.rb | 131 ++++++++++++++------------- lib/vagrant/hosts/bsd.rb | 10 +- lib/vagrant/hosts/linux.rb | 10 +- vagrant.gemspec | 2 +- 6 files changed, 145 insertions(+), 143 deletions(-) diff --git a/lib/vagrant/command/base.rb b/lib/vagrant/command/base.rb index babb65cab..cf500ce54 100644 --- a/lib/vagrant/command/base.rb +++ b/lib/vagrant/command/base.rb @@ -81,10 +81,10 @@ module Vagrant names.each do |name| raise Errors::MultiVMEnvironmentRequired if !@env.multivm? - if name =~ /^\/(.+?)\/$/ + if pattern = name[/^\/(.+?)\/$/, 1] # This is a regular expression name, so we convert to a regular # expression and allow that sort of matching. - regex = Regexp.new($1.to_s) + regex = Regexp.new(pattern) @env.vms.each do |name, vm| vms << vm if name =~ regex diff --git a/lib/vagrant/driver/virtualbox_4_0.rb b/lib/vagrant/driver/virtualbox_4_0.rb index b4e6b7eb1..2f5b7b0b4 100644 --- a/lib/vagrant/driver/virtualbox_4_0.rb +++ b/lib/vagrant/driver/virtualbox_4_0.rb @@ -25,8 +25,8 @@ module Vagrant def clear_shared_folders info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) info.split("\n").each do |line| - if line =~ /^SharedFolderNameMachineMapping\d+="(.+?)"$/ - execute("sharedfolder", "remove", @uuid, "--name", $1.to_s) + if name = line[/^SharedFolderNameMachineMapping\d+="(.+?)"$/, 1] + execute("sharedfolder", "remove", @uuid, "--name", name) end end end @@ -42,8 +42,8 @@ module Vagrant def create_host_only_network(options) # Create the interface - execute("hostonlyif", "create") =~ /^Interface '(.+?)' was successfully created$/ - name = $1.to_s + interface = execute("hostonlyif", "create") + name = interface[/^Interface '(.+?)' was successfully created$/, 1] # Configure it execute("hostonlyif", "ipconfig", name, @@ -66,15 +66,17 @@ module Vagrant def delete_unused_host_only_networks networks = [] execute("list", "hostonlyifs").split("\n").each do |line| - networks << $1.to_s if line =~ /^Name:\s+(.+?)$/ + if network_name = line[/^Name:\s+(.+?)$/, 1] + networks << network_name + end end execute("list", "vms").split("\n").each do |line| - if line =~ /^".+?"\s+\{(.+?)\}$/ - info = execute("showvminfo", $1.to_s, "--machinereadable", :retryable => true) + if vm_name = line[/^".+?"\s+\{(.+?)\}$/, 1] + info = execute("showvminfo", vm_name, "--machinereadable", :retryable => true) info.split("\n").each do |line| - if line =~ /^hostonlyadapter\d+="(.+?)"$/ - networks.delete($1.to_s) + if network_name = line[/^hostonlyadapter\d+="(.+?)"$/, 1] + networks.delete(network_name) end end end @@ -170,8 +172,7 @@ module Vagrant if lines.include?("OK.") # The progress of the import will be in the last line. Do a greedy # regular expression to find what we're looking for. - if lines.last =~ /.+(\d{2})%/ - current = $1.to_i + if current = lines.last[/.+(\d{2})%/, 1] if current > last last = current yield current if block_given? @@ -182,16 +183,15 @@ module Vagrant end # Find the name of the VM name - if output !~ /Suggested VM name "(.+?)"/ + name = output[/Suggested VM name "(.+?)"/, 1] + if !name @logger.error("Couldn't find VM name in the output.") return nil end - name = $1.to_s - output = execute("list", "vms") - if output =~ /^"#{Regexp.escape(name)}" \{(.+?)\}$/ - return $1.to_s + if existing_vm = output[/^"#{Regexp.escape(name)}" \{(.+?)\}$/, 1] + return existing_vm end nil @@ -208,17 +208,19 @@ module Vagrant info.split("\n").each do |line| # This is how we find the nic that a FP is attached to, # since this comes first. - current_nic = $1.to_i if line =~ /^nic(\d+)=".+?"$/ + if nic = line[/^nic(\d+)=".+?"$/, 1] + current_nic = nic.to_i + end # If we care about active VMs only, then we check the state # to verify the VM is running. - if active_only && line =~ /^VMState="(.+?)"$/ && $1.to_s != "running" + if active_only && (state = line[/^VMState="(.+?)"$/, 1] and state != "running") return [] end # Parse out the forwarded port information - if line =~ /^Forwarding.+?="(.+?),.+?,.*?,(.+?),.*?,(.+?)"$/ - result = [current_nic, $1.to_s, $2.to_i, $3.to_i] + if matcher = line =~ /^Forwarding.+?="(.+?),.+?,.*?,(.+?),.*?,(.+?)"$/ + result = [current_nic, matcher[1], matcher[2].to_i, matcher[3].to_i] @logger.debug(" - #{result.inspect}") results << result end @@ -232,14 +234,14 @@ module Vagrant info = {} block.split("\n").each do |line| - if line =~ /^Name:\s+(.+?)$/ - info[:name] = $1.to_s - elsif line =~ /^IPAddress:\s+(.+?)$/ - info[:ip] = $1.to_s - elsif line =~ /^NetworkMask:\s+(.+?)$/ - info[:netmask] = $1.to_s - elsif line =~ /^Status:\s+(.+?)$/ - info[:status] = $1.to_s + if name = line[/^Name:\s+(.+?)$/, 1] + info[:name] = name + elsif ip = line[/^IPAddress:\s+(.+?)$/, 1] + info[:ip] = ip + elsif netmask = line[/^NetworkMask:\s+(.+?)$/, 1] + info[:netmask] = netmask + elsif status = line[/^Status:\s+(.+?)$/, 1] + info[:status] = status end end @@ -251,11 +253,10 @@ module Vagrant def read_guest_additions_version output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version", :retryable => true) - if output =~ /^Value: (.+?)$/ + if value = output[/^Value: (.+?)$/, 1] # Split the version by _ since some distro versions modify it # to look like this: 4.1.2_ubuntu, and the distro part isn't # too important. - value = $1.to_s return value.split("_").first end @@ -268,14 +269,14 @@ module Vagrant info = {} block.split("\n").each do |line| - if line =~ /^NetworkName:\s+HostInterfaceNetworking-(.+?)$/ - info[:network] = $1.to_s - elsif line =~ /^IP:\s+(.+?)$/ - info[:ip] = $1.to_s - elsif line =~ /^lowerIPAddress:\s+(.+?)$/ - info[:lower] = $1.to_s - elsif line =~ /^upperIPAddress:\s+(.+?)$/ - info[:upper] = $1.to_s + if network = line[/^NetworkName:\s+HostInterfaceNetworking-(.+?)$/, 1] + info[:network] = network + elsif ip = line[/^IP:\s+(.+?)$/, 1] + info[:ip] = ip + elsif lower = line[/^lowerIPAddress:\s+(.+?)$/, 1] + info[:lower] = lower + elsif upper = line[/^upperIPAddress:\s+(.+?)$/, 1] + info[:upper] = upper end end @@ -287,14 +288,14 @@ module Vagrant info = {} block.split("\n").each do |line| - if line =~ /^Name:\s+(.+?)$/ - info[:name] = $1.to_s - elsif line =~ /^IPAddress:\s+(.+?)$/ - info[:ip] = $1.to_s - elsif line =~ /^NetworkMask:\s+(.+?)$/ - info[:netmask] = $1.to_s - elsif line =~ /^Status:\s+(.+?)$/ - info[:status] = $1.to_s + if name = line[/^Name:\s+(.+?)$/, 1] + info[:name] = name + elsif ip = line[/^IPAddress:\s+(.+?)$/, 1] + info[:ip] = ip + elsif netmask = line[/^NetworkMask:\s+(.+?)$/, 1] + info[:netmask] = netmask + elsif status = line[/^Status:\s+(.+?)$/, 1] + info[:status] = status end end @@ -308,7 +309,9 @@ module Vagrant def read_mac_address info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) info.split("\n").each do |line| - return $1.to_s if line =~ /^macaddress1="(.+?)"$/ + if mac = line[/^macaddress1="(.+?)"$/, 1] + return mac + end end nil @@ -316,8 +319,8 @@ module Vagrant def read_machine_folder execute("list", "systemproperties", :retryable => true).split("\n").each do |line| - if line =~ /^Default machine folder:\s+(.+?)$/i - return $1.to_s + if folder = line[/^Default machine folder:\s+(.+?)$/i, 1] + return folder end end @@ -328,21 +331,21 @@ module Vagrant nics = {} info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) info.split("\n").each do |line| - if line =~ /^nic(\d+)="(.+?)"$/ - adapter = $1.to_i - type = $2.to_sym + if matcher = line =~ /^nic(\d+)="(.+?)"$/ + adapter = matcher[1].to_i + type = matcher[2].to_sym nics[adapter] ||= {} nics[adapter][:type] = type - elsif line =~ /^hostonlyadapter(\d+)="(.+?)"$/ - adapter = $1.to_i - network = $2.to_s + elsif matcher = line =~ /^hostonlyadapter(\d+)="(.+?)"$/ + adapter = matcher[1].to_i + network = matcher[2].to_s nics[adapter] ||= {} nics[adapter][:hostonly] = network - elsif line =~ /^bridgeadapter(\d+)="(.+?)"$/ - adapter = $1.to_i - network = $2.to_s + elsif matcher = line =~ /^bridgeadapter(\d+)="(.+?)"$/ + adapter = matcher[1].to_i + network = matcher[2].to_s nics[adapter] ||= {} nics[adapter][:bridge] = network @@ -356,8 +359,8 @@ module Vagrant output = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) if output =~ /^name=""$/ return :inaccessible - elsif output =~ /^VMState="(.+?)"$/ - return $1.to_sym + elsif state = output[/^VMState="(.+?)"$/, 1] + return state.to_sym end nil @@ -366,9 +369,7 @@ module Vagrant def read_used_ports ports = [] execute("list", "vms", :retryable => true).split("\n").each do |line| - if line =~ /^".+?" \{(.+?)\}$/ - uuid = $1.to_s - + if uuid = line[/^".+?" \{(.+?)\}$/, 1] # Ignore our own used ports next if uuid == @uuid @@ -384,8 +385,8 @@ module Vagrant def read_vms results = [] execute("list", "vms", :retryable => true).split("\n").each do |line| - if line =~ /^".+?" \{(.+?)\}$/ - results << $1.to_s + if vm = line[/^".+?" \{(.+?)\}$/, 1] + results << vm end end diff --git a/lib/vagrant/driver/virtualbox_4_1.rb b/lib/vagrant/driver/virtualbox_4_1.rb index 45e2d5aeb..7424f5494 100644 --- a/lib/vagrant/driver/virtualbox_4_1.rb +++ b/lib/vagrant/driver/virtualbox_4_1.rb @@ -25,8 +25,8 @@ module Vagrant def clear_shared_folders info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) info.split("\n").each do |line| - if line =~ /^SharedFolderNameMachineMapping\d+="(.+?)"$/ - execute("sharedfolder", "remove", @uuid, "--name", $1.to_s) + if folder = line[/^SharedFolderNameMachineMapping\d+="(.+?)"$/, 1] + execute("sharedfolder", "remove", @uuid, "--name", folder) end end end @@ -42,8 +42,8 @@ module Vagrant def create_host_only_network(options) # Create the interface - execute("hostonlyif", "create") =~ /^Interface '(.+?)' was successfully created$/ - name = $1.to_s + interface = execute("hostonlyif", "create") + name = interface[/^Interface '(.+?)' was successfully created$/, 1] # Configure it execute("hostonlyif", "ipconfig", name, @@ -66,15 +66,17 @@ module Vagrant def delete_unused_host_only_networks networks = [] execute("list", "hostonlyifs").split("\n").each do |line| - networks << $1.to_s if line =~ /^Name:\s+(.+?)$/ + if network = line[/^Name:\s+(.+?)$/, 1] + networks << network + end end execute("list", "vms").split("\n").each do |line| - if line =~ /^".+?"\s+\{(.+?)\}$/ - info = execute("showvminfo", $1.to_s, "--machinereadable", :retryable => true) + if vm = line[/^".+?"\s+\{(.+?)\}$/, 1] + info = execute("showvminfo", vm, "--machinereadable", :retryable => true) info.split("\n").each do |line| - if line =~ /^hostonlyadapter\d+="(.+?)"$/ - networks.delete($1.to_s) + if adapter = line[/^hostonlyadapter\d+="(.+?)"$/, 1] + networks.delete(adapter) end end end @@ -170,8 +172,7 @@ module Vagrant if lines.include?("OK.") # The progress of the import will be in the last line. Do a greedy # regular expression to find what we're looking for. - if lines.last =~ /.+(\d{2})%/ - current = $1.to_i + if current = lines.last[/.+(\d{2})%/, 1] if current > last last = current yield current if block_given? @@ -182,16 +183,15 @@ module Vagrant end # Find the name of the VM name - if output !~ /Suggested VM name "(.+?)"/ + name = output[/Suggested VM name "(.+?)"/, 1] + if !name @logger.error("Couldn't find VM name in the output.") return nil end - name = $1.to_s - output = execute("list", "vms") - if output =~ /^"#{Regexp.escape(name)}" \{(.+?)\}$/ - return $1.to_s + if existing_vm = output[/^"#{Regexp.escape(name)}" \{(.+?)\}$/, 1] + return existing_vm end nil @@ -208,17 +208,19 @@ module Vagrant info.split("\n").each do |line| # This is how we find the nic that a FP is attached to, # since this comes first. - current_nic = $1.to_i if line =~ /^nic(\d+)=".+?"$/ + if nic = line[/^nic(\d+)=".+?"$/, 1] + current_nic = nic.to_i + end # If we care about active VMs only, then we check the state # to verify the VM is running. - if active_only && line =~ /^VMState="(.+?)"$/ && $1.to_s != "running" + if active_only && (state = line[/^VMState="(.+?)"$/, 1] and state != "running") return [] end # Parse out the forwarded port information - if line =~ /^Forwarding.+?="(.+?),.+?,.*?,(.+?),.*?,(.+?)"$/ - result = [current_nic, $1.to_s, $2.to_i, $3.to_i] + if matcher = line =~ /^Forwarding.+?="(.+?),.+?,.*?,(.+?),.*?,(.+?)"$/ + result = [current_nic, matcher[1], matcher[2].to_i, matcher[3].to_i] @logger.debug(" - #{result.inspect}") results << result end @@ -232,14 +234,14 @@ module Vagrant info = {} block.split("\n").each do |line| - if line =~ /^Name:\s+(.+?)$/ - info[:name] = $1.to_s - elsif line =~ /^IPAddress:\s+(.+?)$/ - info[:ip] = $1.to_s - elsif line =~ /^NetworkMask:\s+(.+?)$/ - info[:netmask] = $1.to_s - elsif line =~ /^Status:\s+(.+?)$/ - info[:status] = $1.to_s + if name = line[/^Name:\s+(.+?)$/, 1] + info[:name] = name + elsif ip = line[/^IPAddress:\s+(.+?)$/, 1] + info[:ip] = ip + elsif netmask = line[/^NetworkMask:\s+(.+?)$/, 1] + info[:netmask] = netmask + elsif status = line[/^Status:\s+(.+?)$/, 1] + info[:status] = status end end @@ -251,11 +253,10 @@ module Vagrant def read_guest_additions_version output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version", :retryable => true) - if output =~ /^Value: (.+?)$/ + if value = output[/^Value: (.+?)$/, 1] # Split the version by _ since some distro versions modify it # to look like this: 4.1.2_ubuntu, and the distro part isn't # too important. - value = $1.to_s return value.split("_").first end @@ -268,14 +269,14 @@ module Vagrant info = {} block.split("\n").each do |line| - if line =~ /^NetworkName:\s+HostInterfaceNetworking-(.+?)$/ - info[:network] = $1.to_s - elsif line =~ /^IP:\s+(.+?)$/ - info[:ip] = $1.to_s - elsif line =~ /^lowerIPAddress:\s+(.+?)$/ - info[:lower] = $1.to_s - elsif line =~ /^upperIPAddress:\s+(.+?)$/ - info[:upper] = $1.to_s + if network = line[/^NetworkName:\s+HostInterfaceNetworking-(.+?)$/, 1] + info[:network] = network + elsif ip = line[/^IP:\s+(.+?)$/, 1] + info[:ip] = ip + elsif lower = line[/^lowerIPAddress:\s+(.+?)$/, 1] + info[:lower] = lower + elsif upper = line[/^upperIPAddress:\s+(.+?)$/, 1] + info[:upper] = upper end end @@ -287,14 +288,14 @@ module Vagrant info = {} block.split("\n").each do |line| - if line =~ /^Name:\s+(.+?)$/ - info[:name] = $1.to_s - elsif line =~ /^IPAddress:\s+(.+?)$/ - info[:ip] = $1.to_s - elsif line =~ /^NetworkMask:\s+(.+?)$/ - info[:netmask] = $1.to_s - elsif line =~ /^Status:\s+(.+?)$/ - info[:status] = $1.to_s + if name = line[/^Name:\s+(.+?)$/, 1] + info[:name] = name + elsif ip = line[/^IPAddress:\s+(.+?)$/, 1] + info[:ip] = ip + elsif netmask = line[/^NetworkMask:\s+(.+?)$/, 1] + info[:netmask] = netmask + elsif status = line[/^Status:\s+(.+?)$/, 1] + info[:status] = status end end @@ -308,7 +309,9 @@ module Vagrant def read_mac_address info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) info.split("\n").each do |line| - return $1.to_s if line =~ /^macaddress1="(.+?)"$/ + if mac = line[/^macaddress1="(.+?)"$/, 1] + return mac + end end nil @@ -316,8 +319,8 @@ module Vagrant def read_machine_folder execute("list", "systemproperties", :retryable => true).split("\n").each do |line| - if line =~ /^Default machine folder:\s+(.+?)$/i - return $1.to_s + if folder = line[/^Default machine folder:\s+(.+?)$/i, 1] + return folder end end @@ -328,21 +331,21 @@ module Vagrant nics = {} info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) info.split("\n").each do |line| - if line =~ /^nic(\d+)="(.+?)"$/ - adapter = $1.to_i - type = $2.to_sym + if matcher = line =~ /^nic(\d+)="(.+?)"$/ + adapter = matcher[1].to_i + type = matcher[2].to_sym nics[adapter] ||= {} nics[adapter][:type] = type - elsif line =~ /^hostonlyadapter(\d+)="(.+?)"$/ - adapter = $1.to_i - network = $2.to_s + elsif matcher = line =~ /^hostonlyadapter(\d+)="(.+?)"$/ + adapter = matcher[1].to_i + network = matcher[2].to_s nics[adapter] ||= {} nics[adapter][:hostonly] = network - elsif line =~ /^bridgeadapter(\d+)="(.+?)"$/ - adapter = $1.to_i - network = $2.to_s + elsif matcher = line =~ /^bridgeadapter(\d+)="(.+?)"$/ + adapter = matcher[1].to_i + network = matcher[2].to_s nics[adapter] ||= {} nics[adapter][:bridge] = network @@ -356,8 +359,8 @@ module Vagrant output = execute("showvminfo", @uuid, "--machinereadable", :retryable => true) if output =~ /^name=""$/ return :inaccessible - elsif output =~ /^VMState="(.+?)"$/ - return $1.to_sym + elsif state = output[/^VMState="(.+?)"$/, 1] + return state.to_sym end nil @@ -366,9 +369,7 @@ module Vagrant def read_used_ports ports = [] execute("list", "vms", :retryable => true).split("\n").each do |line| - if line =~ /^".+?" \{(.+?)\}$/ - uuid = $1.to_s - + if uuid = line[/^".+?" \{(.+?)\}$/, 1] # Ignore our own used ports next if uuid == @uuid @@ -384,8 +385,8 @@ module Vagrant def read_vms results = [] execute("list", "vms", :retryable => true).split("\n").each do |line| - if line =~ /^".+?" \{(.+?)\}$/ - results << $1.to_s + if vm line[/^".+?" \{(.+?)\}$/, 1] + results << vm end end diff --git a/lib/vagrant/hosts/bsd.rb b/lib/vagrant/hosts/bsd.rb index 8794caca4..6d7163d45 100644 --- a/lib/vagrant/hosts/bsd.rb +++ b/lib/vagrant/hosts/bsd.rb @@ -66,9 +66,9 @@ module Vagrant output = false File.read("/etc/exports").lines.each do |line| - if line =~ /^# VAGRANT-BEGIN: (.+?)$/ - if valid_ids.include?($1.to_s) - @logger.debug("Valid ID: #{$1.to_s}") + if id = line[/^# VAGRANT-BEGIN: (.+?)$/, 1] + if valid_ids.include?(id) + @logger.debug("Valid ID: #{id}") else if !output # We want to warn the user but we only want to output once @@ -76,8 +76,8 @@ module Vagrant output = true end - @logger.info("Invalid ID, pruning: #{$1.to_s}") - nfs_cleanup($1.to_s) + @logger.info("Invalid ID, pruning: #{id}") + nfs_cleanup(id) end end end diff --git a/lib/vagrant/hosts/linux.rb b/lib/vagrant/hosts/linux.rb index c179fd1d4..105e654ae 100644 --- a/lib/vagrant/hosts/linux.rb +++ b/lib/vagrant/hosts/linux.rb @@ -63,9 +63,9 @@ module Vagrant output = false File.read("/etc/exports").lines.each do |line| - if line =~ /^# VAGRANT-BEGIN: (.+?)$/ - if valid_ids.include?($1.to_s) - @logger.debug("Valid ID: #{$1.to_s}") + if id = line[/^# VAGRANT-BEGIN: (.+?)$/, 1] + if valid_ids.include?(id) + @logger.debug("Valid ID: #{id}") else if !output # We want to warn the user but we only want to output once @@ -73,8 +73,8 @@ module Vagrant output = true end - @logger.info("Invalid ID, pruning: #{$1.to_s}") - nfs_cleanup($1.to_s) + @logger.info("Invalid ID, pruning: #{id}") + nfs_cleanup(id) end end end diff --git a/vagrant.gemspec b/vagrant.gemspec index 437fdac90..661d9b69b 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -33,7 +33,7 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec-mocks", "~> 2.8.0" s.files = `git ls-files`.split("\n") - s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact + s.executables = `git ls-files`.split("\n").map{|f| f[/^bin\/(.*)/, 1]}.compact s.require_path = 'lib' end