|
| 1 | +from __future__ import print_function |
| 2 | +import os |
| 3 | +import json |
| 4 | +import zipfile |
| 5 | +import shutil |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +import urllib #import urlretrieve, urlopen |
| 10 | + |
| 11 | +#def downloadFile(url, outFile): |
| 12 | +# urlretrieve(url, outFile) |
| 13 | + |
| 14 | +def isNewer(new, original): |
| 15 | + majorMult, minorMult, patchMult = 10000,100,1 |
| 16 | + major1, minor1, patch1 = original.split('.') |
| 17 | + major2, minor2, patch2 = new.split('.') |
| 18 | + return majorMult * major2 + minorMult * minor2 + patchMult * minor2 > majorMult * major1 + minorMult * minor1 + patchMult * minor1 |
| 19 | + |
| 20 | + |
| 21 | +def downloadProgress(count, blockSize, totalSize): |
| 22 | + if count % 1000 == 0: |
| 23 | + percentDone = float(count) * blockSize / totalSize * 100.0 |
| 24 | + print("%4.2f%%" % percentDone,end='\b\b\b\b\b\b') |
| 25 | + |
| 26 | +def main(): |
| 27 | + currentInstallFileName = "viewer_currentInstall.json" |
| 28 | + |
| 29 | + versionAndChangeLogUrl = "http://s3.amazonaws.com/battlecode-2018/viewer/" |
| 30 | + |
| 31 | + versionFileName = "version.txt" |
| 32 | + changelogFileName = "changelog.json" |
| 33 | + |
| 34 | + baseUrl = "http://s3.amazonaws.com/battlecode-2018/viewer/" |
| 35 | + |
| 36 | + directory = os.path.dirname(os.path.realpath(__file__)) |
| 37 | + |
| 38 | + zipFileName = "viewer_latest.zip" |
| 39 | + viewerDirectory = "viewer_latest/" |
| 40 | + |
| 41 | + currentInfoFileLocation = os.path.join(directory, currentInstallFileName) |
| 42 | + if os.path.exists(currentInfoFileLocation): |
| 43 | + currentInfoFile = open(currentInfoFileLocation) |
| 44 | + currentInfo = json.load(currentInfoFile) |
| 45 | + currentInfoFile.close() |
| 46 | + else: |
| 47 | + possibleSystems = [ |
| 48 | + ("1", "Windows (64-bit)", "Win64"), |
| 49 | + ("2", "Windows (32-bit)", "Win32"), |
| 50 | + ("3", "Linux (64-bit)", "Linux64"), |
| 51 | + ("4", "Linux (32-bit)", "Linux32"), |
| 52 | + ("5", "Mac OS X", "Mac") |
| 53 | + ] |
| 54 | + print("It looks like this is your first time installing the viewer. What system are you using?") |
| 55 | + for optionNum, optionName, actualName in possibleSystems: |
| 56 | + print("%s) %s" % (optionNum, optionName)) |
| 57 | + systemInp = raw_input("> ") |
| 58 | + try: |
| 59 | + systemInp = int(systemInp) |
| 60 | + if systemInp <= 0 or systemInp > len(possibleSystems): |
| 61 | + raise Exception() |
| 62 | + currentInfo = { |
| 63 | + 'version': '0.0.0', |
| 64 | + 'system': possibleSystems[systemInp - 1][2] |
| 65 | + } |
| 66 | + print("Done setup! You've selected the system %s. \nIf you ever want to change this setup, delete the file %s " % (possibleSystems[systemInp-1][1], currentInstallFileName)) |
| 67 | + except: |
| 68 | + print("Invalid input. Exiting..") |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + |
| 72 | + versionFileUrl = versionAndChangeLogUrl + versionFileName |
| 73 | + latestVersion = urllib.urlopen(versionFileUrl).read().decode() |
| 74 | + |
| 75 | + print("Checking for updates...") |
| 76 | + |
| 77 | + if isNewer(latestVersion, currentInfo['version']): |
| 78 | + print("There is a newer version available.\nCurrent version is: %s. The new version is %s." % (currentInfo['version'], latestVersion)) |
| 79 | + else: |
| 80 | + print("No updates! Would you like to reinstall the viewer anyway?") |
| 81 | + shouldDownload = raw_input("Download? (Y/N) > ").lower() == "y" |
| 82 | + if shouldDownload: |
| 83 | + newestUrl = baseUrl + ("%s/%s.zip" % (latestVersion, currentInfo['system'])) |
| 84 | + downloadLocation = os.path.join(directory, zipFileName) |
| 85 | + if os.path.exists(downloadLocation): |
| 86 | + print("Removing previous archive...") |
| 87 | + os.remove(downloadLocation) |
| 88 | + print("Deleted old archive.") |
| 89 | + print("Downloading new client... This could take a while.") |
| 90 | + urllib.urlretrieve(newestUrl, downloadLocation, downloadProgress) |
| 91 | + print() |
| 92 | + print("Successfully downloaded files. ") |
| 93 | + outputDirectory = os.path.join(directory, viewerDirectory) |
| 94 | + if os.path.exists(outputDirectory): |
| 95 | + print("Removing previous client") |
| 96 | + shutil.rmtree(outputDirectory, True) |
| 97 | + print("Successfully removed previous client.") |
| 98 | + print("Extracting from archive...") |
| 99 | + zip_ref = zipfile.ZipFile(downloadLocation, "r") |
| 100 | + zip_ref.extractall(outputDirectory) |
| 101 | + zip_ref.close() |
| 102 | + print("Extracted fully!") |
| 103 | + if os.path.exists(downloadLocation): |
| 104 | + print("Cleaning up downloads...") |
| 105 | + os.remove(downloadLocation) |
| 106 | + print("Cleaned up") |
| 107 | + try: |
| 108 | + if currentInfo['system'] == 'Linux32': |
| 109 | + print("Fixing permissions...") |
| 110 | + print("chmod 777 viewer_latest/Linux32/battleclient18.x86") |
| 111 | + os.system("chmod 777 viewer_latest/Linux32/battleclient18.x86") |
| 112 | + print("Done fixing permissions!") |
| 113 | + elif currentInfo['system'] == 'Linux64': |
| 114 | + print("Fixing permissions...") |
| 115 | + print("chmod 777 viewer_latest/Linux64/battleclient18.x86_64") |
| 116 | + os.system("chmod 777 viewer_latest/Linux64/battleclient18.x86_64") |
| 117 | + print("Done fixing permissions!") |
| 118 | + if currentInfo['system'] == 'Mac': |
| 119 | + print("Fixing permissions...") |
| 120 | + print("chmod -R 777 viewer_latest/Mac/battleclient18.app") |
| 121 | + os.system("chmod -R 777 viewer_latest/Mac/battleclient18.app") |
| 122 | + print("Done fixing permissions!") |
| 123 | + except: |
| 124 | + pass |
| 125 | + print("Updating current version number...") |
| 126 | + newInfo = {} |
| 127 | + newInfo['version'] = latestVersion |
| 128 | + newInfo['system'] = currentInfo['system'] |
| 129 | + currentInfoFile = open(currentInfoFileLocation, "w") |
| 130 | + currentInfo = json.dump(newInfo, currentInfoFile) |
| 131 | + currentInfoFile.close() |
| 132 | + print("All set! The viewer is in: %s" % outputDirectory) |
| 133 | + else: |
| 134 | + print("Not downloading - your system has not been changed.") |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
| 138 | + |
| 139 | + |
0 commit comments