''' Auto-updater ''' from distutils.version import StrictVersion import os from subprocess import Popen import sys import tempfile import requests VERSIONFILE_URL = "http://codebox/lab/ModelePython/raw/VERSION" SETUPFILE_URL = "http://codebox/lab/ModelePython/raw/setup-hello_world.exe" CURDIR = os.path.dirname(__file__) class UpdateNeeded(Exception): pass class Unavailable(requests.exceptions.RequestException): pass def autoupdate(): try: if update_needed(): update() except Unavailable: pass def update_needed(): # Available version try: raw = requests.get(VERSIONFILE_URL).text available_version = StrictVersion(raw) except (ValueError, requests.exceptions.RequestException): raise Unavailable() # Installed version with open(os.path.join(CURDIR, "VERSION")) as f: installed_version = f.read() return available_version > installed_version def update(): with tempfile.TemporaryDirectory() as tmpdir: # Download the setup.exe response = requests.get(SETUPFILE_URL, stream=True) with open("setup.exe", "wb") as handle: for data in response.iter_content(): handle.write(data) # Run the installer and restart the start command args = "/SILENT && {}".format(tmpdir / "setup.exe", " ".join(sys.argv[:])) Popen([os.path.join(tmpdir, "setup.exe"), args], shell=True) # An UpdateNeeded exception is raised so the script exit before being restarted by subprocess raise UpdateNeeded()