2014-05-19 196 views
-1

我從一位同事那裏繼承了一些代碼,這是我之前沒做過的代碼。此代碼適用於我的機器和其他機器。但是,當我試圖在新安裝的Ruby和相關的gem的新系統上運行它時,它不會按預期那樣進行預成型。所以這個代碼工作......只是不在我們所有的系統上。而且我太陌生,不知道爲了找出不一致的地方到底在尋找什麼。Ruby代碼不能正常工作

如果有人對我如何解決這個問題有一個想法或任何提示,我會非常感激。

require 'curb' # For Curl statement 
require 'nokogiri' # for screen scrapping 
require 'io/console' # hide password input 
require 'mechanize' # clicking through and logging in 

#start your engines 
agent = Mechanize.new 
agent.keep_alive = false 
agent.open_timeout = 180 
agent.read_timeout = 180 
page = agent.get('http://_____.co.__________.com/admin/_____/user/') 

#get login for _____ 
puts "Please enter your _____ Login:" 
prompt = '>' 
print prompt 
page.forms.first.fields[1].value = STDIN.gets.chomp() 
puts "Please enter your password:" 
print prompt 
#this way keeps you from seeing the password on the screen 
page.forms.first.fields[2].value = STDIN.noecho(&:gets).chomp() 
puts "Searching for %s" % ARGV[0] 
#click the login button 
login_page = page.forms.first.submit  
#take the first argument and put it in the user search box 
login_page.forms.first.fields.first.value = ARGV[0] 
#click search 
users_page = login_page.forms.first.submit 
#click on the only link, people with "deleted" account might need some hacking to make work 
inventory_page = users_page.links.last.click 
puts "Found %s, now opening user page..." % ARGV[0] 
active_inventory = [] 
puts "checking total active inventorys..." 
#find the inventory table and get all the rows. Then loop through and check if active. If active put FSID into an array to check next 
inventory_page.parser.xpath('//tr[contains(@id,"filesystemproperties")]').each do |row| 
#inventory_page.parser.css('tr.has_original').each do |row|  
    if row.at('img')["alt"] == "True" 
     active_inventory << row.children[2].text.gsub("\n",'').strip 
     puts row.children[2].text.gsub("\n",'').strip 
    end 
end 

#now lets send a inventory, go to the inventory Versions link at the top of the page 
get_inventory = inventory_page.links[3].click 

#require 'logger' #debug stuff, pay no attention. 
#agent.log = Logger.new $stderr 
#agent.agent.http.debug_output = $stderr 
puts "checking total size of inventorys..." 

#time for some simple addition 
inventory_size = 0 
#clear out any blank fsid, then each one send (POST) 
active_inventory.reject!(&:empty?).each do |fsid| 
    get_inventory.forms.first.fields.first.value = fsid 
    size = get_inventory.forms.first.submit 
    #get the top row second column for latest size, and add it to the existing tally for total size 
    inventory_size += size.parser.xpath('//tbody/tr[1]/td[2]').text.to_i 
    puts "%s is %d Bytes" % [fsid, size.parser.xpath('//tbody/tr[1]/td[2]').text.to_i] 
    puts "current inventory: #{inventory_size}" 
    #sleep 15 
end 
#convert those bytes to MEGABytes 
puts "%d currently active inventory(s) found\n" % (active_inventory.count - 3) 
puts "\n \n \nTotal inventory size is: %f MB" % [inventory_size/(1024*1024).to_f] 

您可以在附加圖像中看到輸出。 紫色背景是工作,白色背景被破壞。我在Ubuntu VM上也得到了同樣不成功的結果

Works: OS X 10.8.5; Ruby 2.1.1; XCODE 5.1.1 不起作用: OS X 10.8.5(VirtualBox); Ruby 2.1.1; XCODE 5.1.1 不起作用: Ubuntu 14.04(VirualBox); Ruby 2.1.2; XCODE N/A

enter image description here

enter image description here

+1

我明白,你已經把一些工作納入問這個,但是這是相當困難的基礎就在表面的描述來回答。這不像是有一個「正常工作」的按鈕,每個人都知道你應該推動,而你卻不是。這看起來像是我親自動手的那種問題 - 打破記錄器/調試器並試圖找出環境在哪裏發散。有幾點需要注意的是系統上的寶石版本和網絡差異(因爲這似乎是通過網絡運行的)。 – Chuck

回答

4

這可能是在環境的差異,特別是與寶石,導致不一致的行爲。在每一個環境,嘗試

gem list 

尋找此腳本需要的寶石,並尋找不同的環境之間的不同版本號。如果你確實需要不同的版本,你可以做到以下幾點:

gem uninstall <gem-name> 
gem install <gem-name> -v <desired-version-number> 
+0

哈!感謝Redyaffle,它做到了!較新的安裝只是簡單的有一個最新版本的寶石。卸載新的並且安裝「舊的」將它像冠軍一樣折到位。 – user2279887

+0

優秀的洞察力! –