Advertisement
J2897

WinSCP Updater

Dec 20th, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.53 KB | None | 0 0
  1. # Released under the GNU General Public License version 3 by J2897.
  2.  
  3. def get_page(page):
  4.     import urllib2
  5.     source = urllib2.urlopen(page)
  6.     return source.read()
  7.  
  8. title = 'WinSCP Updater'
  9. target = '>Download WinSCP<'
  10. url = 'http://winscp.net/eng/download.php'
  11.  
  12. print 'Running:     ' + title
  13. print 'Target:          ' + target
  14. print 'URL:         ' + url
  15.  
  16. try:
  17.     page = get_page(url)
  18. except:
  19.     page = None
  20. else:
  21.     print 'Got page...'
  22.  
  23. def msg_box(message, box_type):
  24.     import win32api
  25.     user_input = win32api.MessageBox(0, message, title, box_type)
  26.     return user_input
  27.  
  28. def stop():
  29.     import sys
  30.     sys.exit()
  31.  
  32. if page == None:
  33.     msg_box('Could not download the page. You may not be connected to the internet.', 0)
  34.     stop()
  35.  
  36. def find_site_ver(page):
  37.     T1 = page.find(target)
  38.     if T1 == -1:
  39.         return None, None
  40.     T2 = page.find('>WinSCP ', T1)
  41.     T3 = page.find('<', T2)
  42.     T4 = page.find('winscp', T3)
  43.     T5 = page.find('.exe', T4)
  44.     return page[T2+8:T3], page[T4:T5+4] # 5.1.5, winscp515setup.exe
  45.  
  46. try:
  47.     site_version, FN = find_site_ver(page)
  48. except:
  49.     msg_box('Could not search the page.', 0)
  50.     stop()
  51. else:
  52.     if site_version == None:
  53.         print 'Not found!'
  54.         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)
  55.         stop()
  56.     else:
  57.         print 'Found:           ' + site_version
  58.  
  59. import os
  60. tmp = os.getenv('TEMP')
  61. PF = os.getenv('PROGRAMFILES')
  62. WinSCP_exe = PF + '\\WinSCP\\WinSCP.exe'
  63. DL = tmp + '\\' + FN
  64. command = [DL, '/SILENT', '/NORESTART'] # More parameters: http://winscp.net/eng/docs/installation#automating_installation
  65.  
  66. def DL_file():
  67.     import urllib
  68.     url = 'http://downloads.sourceforge.net/project/winscp/WinSCP/' + site_version + '/' + FN
  69.     urllib.urlretrieve(url, DL)
  70.  
  71. def sub_proc(command):
  72.     import subprocess
  73.     p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
  74.     stdout, stderr = p.communicate()
  75.     return p.returncode # is 0 if success
  76.  
  77. def download_install():
  78.     try:
  79.         DL_file()
  80.     except:
  81.         msg_box('Failed to download ' + FN + ' to ' + tmp + '.', 0)
  82.         stop()
  83.     else:
  84.         print 'Downloaded:      ' + FN
  85.  
  86.     try:
  87.         RC = sub_proc(command)
  88.     except:
  89.         RC = None
  90.  
  91.     if RC == None:
  92.         msg_box('Failed to execute ' + FN + '.', 0)
  93.         stop()
  94.     elif RC == 0:
  95.         msg_box('Successfully updated to version ' + site_version + '.', 0)
  96.         stop()
  97.     else:
  98.         msg_box('Successfully spawned new process for ' + FN + '. But the installation appears to have failed.', 0)
  99.         stop()
  100.  
  101. # Check if the WinSCP.exe file exists...
  102. if not os.path.isfile(WinSCP_exe):
  103.     # No:   Download and install WinSCP, and quit.
  104.     print 'WinSCP.exe file doesn\'t exist.'
  105.     print 'Installing WinSCP for the first time...'
  106.     download_install()
  107.     print 'Ending...'
  108.     delay(5)
  109.     stop()
  110.  
  111. import win32api
  112. try:
  113.     info = win32api.GetFileVersionInfo(WinSCP_exe, "\\")
  114.     ms = info['FileVersionMS']
  115.     ls = info['FileVersionLS']
  116.     # BUG FOUND...
  117.     # The 'try' exception will never be raised because the file_version will be set to '0.0.0.0' if there's a problem.
  118.     file_version = "%d.%d.%d.%d" % (win32api.HIWORD(ms), win32api.LOWORD (ms),
  119.                                     win32api.HIWORD (ls), win32api.LOWORD (ls))
  120. except:
  121.     msg_box('Cannot find the file version of the local WinSCP.exe file.', 0)
  122.     stop()
  123. else:
  124.     print 'Got local file version information...'
  125.  
  126. # Check if the site_version numbers are in the file_version numbers...
  127. def clean(text):
  128.     import re
  129.     return re.sub('[^0-9]', '', text)
  130.  
  131. clean_site_version = clean(site_version)
  132. clean_file_version = clean(file_version)[:len(clean_site_version)]
  133.  
  134. print 'Local version:       ' + clean_file_version
  135. print 'Site version:        ' + clean_site_version
  136.  
  137. def delay(sec):
  138.     import time
  139.     time.sleep(sec)
  140.  
  141. if clean_file_version.find(clean_site_version) != -1:
  142.     # Yes:  Quit.
  143.     print 'Match!'
  144.     print 'Ending...'
  145.     delay(5)
  146.     stop()
  147.  
  148. # Check if WinSCP is running...
  149. def find_proc(exe):
  150.     import subprocess
  151.     cmd = 'WMIC PROCESS get Caption'
  152.     proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  153.     for line in proc.stdout:
  154.         if line.find(exe) != -1:
  155.             return True
  156.  
  157. while find_proc('WinSCP.exe'):
  158.     print 'WinSCP is running. Close WinSCP now!'
  159.     user_input = msg_box('There is a new version of WinSCP available. Please close WinSCP and press OK to continue.', 1)
  160.     if user_input == 1:
  161.         pass
  162.     elif user_input == 2:
  163.         stop()
  164.  
  165. # Now download and install the new file...
  166. user_input = msg_box('If you use a custom WinSCP.ini file, back it up now and then press OK when you are ready to proceed with the update.', 1)
  167. if user_input == 2:
  168.     stop()
  169.  
  170. download_install()
  171. print 'Ending...'
  172. delay(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement