Advertisement
J2897

SRWare Iron Updater

Dec 20th, 2014
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. # Released under the GNU General Public License version 3 by J2897.
  2.  
  3. title = 'SRWare Iron Updater'
  4. print 'Running: ' + title
  5.  
  6. # Is SRWare Iron installed?
  7. import os
  8. DL = os.getenv('TEMP') + '\\' + 'srware_iron.exe'
  9. PF = os.getenv('PROGRAMFILES')
  10. iron_exe = PF + '\\SRWare Iron\\chrome.exe'
  11.  
  12. if os.path.isfile(iron_exe):
  13.     # Yes.
  14.     first_time = False
  15. else:
  16.     # No.
  17.     first_time = True
  18.  
  19. def DL_file():
  20.     import urllib
  21.     file_url = 'http://www.srware.net/downloads/srware_iron.exe'
  22.     urllib.urlretrieve(file_url, DL)
  23.  
  24. def sub_proc(command):
  25.     import subprocess
  26.     p = subprocess.Popen(command, shell=True, stdout = subprocess.PIPE)
  27.     stdout, stderr = p.communicate()
  28.     return p.returncode # is 0 if success
  29.  
  30. def delay(sec):
  31.     import time
  32.     time.sleep(sec)
  33.  
  34. def stop():
  35.     import sys
  36.     sys.exit()
  37.  
  38. command = [DL, '/SILENT', '/NORESTART'] # More parameters: http://winscp.net/eng/docs/installation#automating_installation
  39.  
  40. if first_time == True:
  41.     # Download and install SRWare Iron.
  42.     print 'File not found:      ' + iron_exe
  43.     print 'Installing SRWare Iron for the first time...'
  44.     DL_file()
  45.     sub_proc(command)
  46.     print 'Ending...'
  47.     delay(5)
  48.     stop()
  49.     # End.
  50.  
  51. target = 'Version:'
  52. url = 'https://www.srware.net/en/software_srware_iron_download.php'
  53. print 'Target:      ' + target
  54. print 'URL:     ' + url
  55.  
  56. import win32api
  57.  
  58. def msg_box(message, box_type):
  59.     user_input = win32api.MessageBox(0, message, title, box_type)
  60.     return user_input
  61.  
  62. # Get the web-page.
  63. def get_page(page):
  64.     import urllib2
  65.     source = urllib2.urlopen(page)
  66.     return source.read()
  67.  
  68. try:
  69.     page = get_page(url)
  70. except:
  71.     msg_box('Could not download the page. You may not be connected to the internet.', 0)
  72.     stop()
  73. else:
  74.     print 'Got page...'
  75.  
  76. # Get the current version information from the web-page.
  77. def find_site_ver(page):
  78.     T1 = page.find(target)
  79.     if T1 == -1:
  80.         return None
  81.     T2 = page.find('>', T1)
  82.     T3 = page.find('<', T2)
  83.     return page[T2+1:T3] # 27.0.1500.0
  84.  
  85. try:
  86.     site_version = find_site_ver(page)
  87.     SV_list = site_version.split('.', 3)
  88. except:
  89.     msg_box('Could not search the page.', 0)
  90.     stop()
  91. else:
  92.     print 'Site version:        ' + site_version
  93.  
  94. if site_version == None:
  95.     msg_box('The search target has not been found on the page. The formatting, or the text on the page, may have been changed.', 0)
  96.     stop()
  97.  
  98. def update_available():
  99.     if SV_list[0] > FV_list[0]:
  100.         return True
  101.     elif SV_list[0] == FV_list[0] and SV_list[1] > FV_list[1]:
  102.         return True
  103.     elif SV_list[0] == FV_list[0] and SV_list[1] == FV_list[1] and SV_list[2] > FV_list[2]:
  104.         return True
  105.     elif SV_list[0] == FV_list[0] and SV_list[1] == FV_list[1] and SV_list[2] == FV_list[2] and SV_list[3] > FV_list[3]:
  106.         return True
  107.     else:
  108.         return False
  109.  
  110. # Get the version information from the local file.
  111. try:
  112.     info = win32api.GetFileVersionInfo(iron_exe, "\\")
  113.     ms = info['FileVersionMS']
  114.     ls = info['FileVersionLS']
  115.     # BUG FOUND...
  116.     # The 'try' exception will never be raised because the file_version will be set to '0.0.0.0' if there's a problem.
  117.     file_version = "%d.%d.%d.%d" % (win32api.HIWORD(ms), win32api.LOWORD(ms),
  118.                                     win32api.HIWORD(ls), win32api.LOWORD(ls)) # 27.0.1400.0
  119.     FV_list = file_version.split('.', 3)
  120. except:
  121.     msg_box('Could not retrieve the local file version information.', 0)
  122.     stop()
  123. else:
  124.     print 'Local version:       ' + file_version
  125.     # Is the site version higher than the file version?
  126.     if update_available():
  127.         # Yes.
  128.         print 'New version available!'
  129.         print 'Updating...'
  130.     else:
  131.         # No.
  132.         print 'No new version available.'
  133.         print 'Ending...'
  134.         delay(5)
  135.         stop()
  136.         # End.
  137.  
  138. # Is Iron currently running?
  139. def find_proc(exe):
  140.     import subprocess
  141.     cmd = 'WMIC PROCESS get Caption'
  142.     proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  143.     for line in proc.stdout:
  144.         if line.find(exe) != -1:
  145.             return True
  146.  
  147. while find_proc('chrome.exe'):
  148.     # Yes.
  149.     print 'Iron is running. Close Iron now!'
  150.     user_input = msg_box('There is a new version of Iron available. Please close Iron and press OK to continue.', 1)
  151.     if user_input == 1:
  152.         pass
  153.     elif user_input == 2:
  154.         stop()
  155.  
  156. # No.
  157. # Download and install SRWare Iron.
  158. DL_file()
  159. sub_proc(command)
  160. print 'Ending...'
  161. delay(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement