Advertisement
J2897

Cygwin Setup Updater v1.0

Jan 15th, 2016
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. # Released under the GNU General Public License version 3 by J2897.
  2. import os
  3. title = 'Cygwin Setup Updater'
  4. os.system('title ' + title)
  5. drive_letter = os.getenv('SYSTEMDRIVE')
  6. tmp = os.getenv('TEMP')
  7. userprofile = os.getenv('USERPROFILE')
  8. pickle_cache = userprofile + '\\Cygwin.dat'
  9. cygstore = drive_letter + '\\cygstore'
  10. def stop():
  11.     import sys
  12.     sys.exit()
  13. target = 'most recent version'
  14. url = 'https://www.cygwin.com'
  15. print 'Main target:         ' + target
  16. print 'URL:             ' + url
  17.  
  18. # Get the web-page.
  19. def get_page(page):
  20.     import urllib2
  21.     source = urllib2.urlopen(page)
  22.     return source.read()
  23. try:
  24.     page = get_page(url)
  25. except:
  26.     print 'Could not download the page. You may not be connected to the internet.'
  27.     stop()
  28.  
  29. # Get the current version information from the web-page.
  30. def find_site_ver(page):
  31.     A1 = page.find(target)
  32.     if A1 == -1:
  33.         return None, None, None
  34.     A2 = page.find('>', A1+len(target))
  35.     A3 = page.find('<', A2)
  36.     site_ver = page[A2+1:A3]
  37.     second_target = 'href="'
  38.     B1 = page.find(second_target, A2) + len(second_target)
  39.     B2 = page.find('"', B1)
  40.     filename_32 = page[B1:B2]
  41.     third_target = 'href="'
  42.     C1 = page.find(third_target, B2) + len(third_target)
  43.     C2 = page.find('"', C1)
  44.     filename_64 = page[C1:C2]
  45.     return filename_32, filename_64, site_ver # setup-x86.exe setup-x86_64.exe 2.3.0
  46. try:
  47.     site_filename_32, site_filename_64, site_ver = find_site_ver(page)
  48. except:
  49.     print 'Could not search the page.'
  50.     stop()
  51. if site_filename_32 == None:
  52.     print 'The search target has not been found on the page. The formatting, or the text on the page, may have been changed.'
  53.     stop()
  54.  
  55. # Detect the OS architecture.
  56. def get_os_architecture():
  57.     PA, PAW6432 = os.getenv('PROCESSOR_ARCHITECTURE'), os.getenv('PROCESSOR_ARCHITEW6432')
  58.     if (PA == 'x86' and PAW6432 == None):
  59.         return '32-Bit'
  60.     else:
  61.         return '64-Bit'
  62. bit = get_os_architecture()
  63.  
  64. # Select the appropriate setup file.
  65. if bit == '64-Bit':
  66.     site_filename = site_filename_64
  67. else:
  68.     site_filename = site_filename_32
  69. print 'Found ' + bit + ' setup file:    ' + site_filename
  70. print 'Site version:            ' + site_ver
  71.  
  72. # Is the cache file in the user's profile folder?
  73. import cPickle
  74. if not os.path.isdir(cygstore):
  75.     print 'Creating folder:         ' + cygstore
  76.     os.makedirs(cygstore)
  77. elif os.path.isfile(pickle_cache):
  78.  
  79.     # Get the version information from the cache file (e.g. '2.2.0').
  80.     def load_pickle_file():
  81.         with open(pickle_cache, 'rb') as pickle_file:
  82.             return cPickle.load(pickle_file)
  83.     try:
  84.         local_ver = load_pickle_file()
  85.         print 'Local version:           ' + local_ver
  86.     except:
  87.         print 'Could not load cache.'
  88.         stop()
  89.  
  90.     # Is the local version the same as the site version?
  91.     if local_ver == site_ver:
  92.         print 'Match!'
  93.         stop()
  94.  
  95. # Download the setup file.
  96. try:
  97.     import urllib
  98.     urllib.urlretrieve(url + '/' + site_filename, tmp + '\\' + site_filename)
  99.     if os.path.isfile(cygstore + '\\' + site_filename):
  100.         os.remove(cygstore + '\\' + site_filename)
  101.     os.rename(tmp + '\\' + site_filename, cygstore + '\\' + site_filename)
  102. except:
  103.     print 'Could not download the file.'
  104.     stop()
  105. else:
  106.     print 'Downloaded.'
  107.    
  108. # Dump the site version information into the cache file.
  109. def dump_pickle_file(cache_file, version):
  110.     with open(cache_file, 'wb') as pickle_file:
  111.         cPickle.dump(version, pickle_file)
  112. try:
  113.     dump_pickle_file(pickle_cache, site_ver)
  114. except:
  115.     print 'Could not dump the site version information to: ' + pickle_cache
  116.     stop()
  117. else:
  118.     print 'Dumped cache:        ' + pickle_cache
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement