Advertisement
DeaD_EyE

nevernvernverdothis.py BAD CODE

May 9th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.36 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os, json, urllib, re, sys
  3.  
  4. try:
  5.     from config import *
  6. except:
  7.     print 'Config nicht gefunden bzw. beschaedigt..'
  8.     sys.exit(1)
  9.  
  10. try:
  11.     import texttable as tt
  12. except:
  13.     CSI="\x1B["
  14.     reset=CSI+"m"
  15.     text = 'Das Modul texttables ist nicht installiert:\napt-get install python-pip; pip install texttable'
  16.     print CSI+"31;40m" + text + CSI + "0m"
  17.     sys.exit(1)
  18.  
  19.  
  20.  
  21. def CheckVersion(steaminf, appid, regex):
  22.     """
  23.    return (needUpdate, version, requiredversion)
  24.    """
  25.     requrl  = 'http://api.steampowered.com/ISteamApps/UpToDateCheck/v0001/?appid=%s&version=%s&format=json'
  26.     if not (os.path.isfile(steaminf) and os.access(steaminf,os.R_OK)):
  27.         return (True, '0', 'steam.inf fehlt')
  28.     try:
  29.         steaminf = open(steaminf).read().replace('\r','')
  30.         version = re.findall(regex,steaminf)[0].replace('.','')
  31.     except:
  32.         return (True, '0', 'keine Versionsinfo')
  33.     try:
  34.         response = urllib.urlopen(requrl % (appid, version))
  35.         response = json.load(response)['response']
  36.     except:
  37.         return (True, '0', 'steamapi offline')
  38.     if response.has_key('required_version'):
  39.         return (True, version, response['required_version'])
  40.     else:
  41.         return (False, version, version)
  42.    
  43.  
  44. from os.path import join
  45. from os import putenv, devnull
  46. from shlex import split
  47.  
  48. class Queue:
  49.     _types = ('hldsupdatetool', 'steamcmd', 'updatetool', 'updatetool_anon')
  50.     _ldpath = {  'hldsupdatetool'   : None,
  51.                 'steamcmd'          : 'linux32',
  52.                 'updatetool'        : '',
  53.                 'updatetool_anon'   : ''}
  54.     _bin     = { 'hldsupdatetool'   : 'steam',
  55.                 'steamcmd'          : 'linux32/steamcmd',
  56.                 'updatetool'        : 'UpdateTool',
  57.                 'updatetool_anon'   : 'UpdateTool'}
  58.     _cmdline = {'hldsupdatetool'    : '%(bin)s -command update -game %(appid)s -dir %(dir)s',
  59.                 'steamcmd'          : '%(bin)s +login %(login)s %(password)s +force_install_dir %(dir)s +app_update %(appid)s -validate +quit',
  60.                 'updatetool'        : '%(bin)s -command update -game %(appid)s -username %(login)s -password %(password)s -dir %(dir)s',
  61.                 'updatetool_anon'   : '%(bin)s -command update -game %(appid)s -dir %(dir)s'
  62.                 }
  63.     _order = ('updatetool', 'updatetool_anon', 'hldsupdatetool', 'steamcmd')
  64.    
  65.     def __init__(self):
  66.         self.updates = {}
  67.         for type in self._types:
  68.             self.updates.update({type : []})
  69.        
  70.     def _create(self, updatertype, updaterpath, serverbase, appid, gamename, login='anonymous', password=''):
  71.         self._binpath = join(updaterpath, self._bin[updatertype])
  72.         self._cmddict = { 'bin'   : self._binpath,
  73.                         'appid' : appid,
  74.                         'login' : login,
  75.                         'password' : password,
  76.                         'dir'   : serverbase}
  77.         if self._ldpath[updatertype] != None:
  78.             return (split(self._cmdline[updatertype] % self._cmddict),
  79.                 ('LD_LIBRARY_PATH', join(updaterpath, self._ldpath[updatertype])),
  80.                 gamename, updatertype)
  81.         else:
  82.             return (split(self._cmdline[updatertype] % self._cmddict), None, gamename, updatertype)
  83.     def _createpath(self,master,serverbase):
  84.         if not os.path.exists(os.path.join(master,serverbase)):
  85.             os.mkdir(os.path.join(master,serverbase))                
  86.     def add(self, updatertype, updaterpath, serverbase, appid, gamename, login='anonymous', password=''):
  87.         if not updatertype in self._types:
  88.             raise Exception('Unbekannter: %s' % updatertype)
  89.         self._createpath(paths['master'], serverbase)
  90.         self.updates[updatertype].append(self._create(updatertype, updaterpath, serverbase, appid, gamename, login, password))
  91.  
  92.     def doupdates(self):
  93.         for order in self._order:
  94.             self.updates[order].reverse()
  95.             while len(self.updates[order]) > 0:
  96.                 yield self.updates[order].pop()
  97.  
  98.  
  99. update = Queue()
  100. header = [  'Spiel', 'AppID',
  101.             'Aktuell', 'Installierte Version',
  102.             'Benoetigt','Updater']
  103. tab = tt.Texttable(max_width=0)
  104. tab.header(header)
  105. print 'Beginne Versionscheck...\n'
  106. for server in servertypes:
  107.     login, password = ('anonymous', '')
  108.     if server.has_key('Login'):
  109.         login, password = server['Login']
  110.     if server['VersionCheckable']:
  111.         steaminf = os.path.join(paths['master'], server['MasterServer'], server['steaminf'])
  112.         appid = server['VersCheckAppID']
  113.         regex = server['VersionCheckRegex']
  114.         needUpdate, version, requiredversion = CheckVersion(steaminf, appid, regex)
  115.         if needUpdate is True:
  116.             #print server['Game'], ' benoetigt ein Update von Version ', version ,' auf Version ', requiredversion
  117.             tab.add_row([server['Game'], server['UpdateAppID'], 'Nein', version, requiredversion, server['Updater']])
  118.             update.add(server['Updater'], paths[server['Updater']], join(paths['master'], server['MasterServer']), server['UpdateAppID'], server['Game'])
  119.         else:
  120.             #print server['Game'], ' ist aktuell. Version: ', version
  121.             tab.add_row([server['Game'], server['UpdateAppID'], 'Ja', version, requiredversion, server['Updater']])
  122.     else:
  123.         tab.add_row([server['Game'], server['UpdateAppID'], 'Unbekannt', 'Unbekannt', 'Unbekannt', server['Updater']])
  124.         update.add(server['Updater'], paths[server['Updater']], join(paths['master'], server['MasterServer']), server['UpdateAppID'], server['Game'], login=login, password=password)
  125.  
  126. print tab.draw()
  127.  
  128. print '\nBeginne mit dem Updateprozess...\n'
  129. from subprocess import Popen, PIPE
  130. for upd, ldpath, gamename, updater in update.doupdates():
  131.     print 'Aktualisiere %s' % gamename,
  132.     if ldpath != None:
  133.         putenv(*ldpath)
  134.         p = Popen(upd, shell=False, stderr=None, stdout=PIPE).communicate()[0]
  135.         #p.wait()
  136.         #print p
  137.         if re.findall(UpdateSuccessRegex[updater],p):
  138.             print '[OK]'
  139.         else:
  140.             print '[Fehler]'
  141.     else:
  142.         p = Popen(upd, shell=False, stderr=None, stdout=PIPE).communicate()[0]
  143.         #p.wait()
  144.         #print p
  145.         if re.findall(UpdateSuccessRegex[updater],p):
  146.             print '[OK]'
  147.         else:
  148.             print '[Fehler]'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement