| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- '''
- 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/master/VERSION"
- SETUPFILE_URL = "http://codebox/lab/ModelePython/raw/master/setup-hello_world.exe"
- CURDIR = os.path.dirname(__file__)
- TMPDIR = os.path.join(tempfile.gettempdir(), os.path.basename(CURDIR), "update")
- TMP_SETUPFILE = os.path.join(TMPDIR, "setup.exe")
- class UpdateNeeded(Exception):
- pass
- class Unavailable(requests.exceptions.RequestException):
- pass
- def autoupdate():
- clean_old()
- try:
- if update_needed():
- update()
- except Unavailable:
- pass
- def clean_old():
- try:
- os.rmdir(TMPDIR)
- except:
- 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():
- # Download the setup.exe
- response = requests.get(SETUPFILE_URL, stream=True)
- os.mkdir(TMPDIR)
- with open(TMP_SETUPFILE, "wb") as handle:
- for chunk in response.iter_content(chunk_size=1024):
- if chunk:
- handle.write(chunk)
- # Run the installer and restart the start command
- args = "/c {} /SILENT & {} {}".format(TMP_SETUPFILE, sys.executable, " ".join(sys.argv[:]))
- Popen(["cmd.exe", args], shell=True)
- # An UpdateNeeded exception is raised so the script exit before being restarted by subprocess
- raise UpdateNeeded()
|