|
|
@@ -9,11 +9,14 @@ import tempfile
|
|
|
|
|
|
import requests
|
|
|
|
|
|
-VERSIONFILE_URL = "http://codebox/lab/ModelePython/raw/VERSION"
|
|
|
-SETUPFILE_URL = "http://codebox/lab/ModelePython/raw/setup-hello_world.exe"
|
|
|
+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
|
|
|
|
|
|
@@ -21,12 +24,19 @@ 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:
|
|
|
@@ -42,19 +52,20 @@ def update_needed():
|
|
|
return available_version > installed_version
|
|
|
|
|
|
def update():
|
|
|
- with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
|
- # Download the setup.exe
|
|
|
- response = requests.get(SETUPFILE_URL, stream=True)
|
|
|
+ # 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)
|
|
|
+ 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 = "/SILENT && {}".format(tmpdir / "setup.exe", " ".join(sys.argv[:]))
|
|
|
+ # Run the installer and restart the start command
|
|
|
+ args = "/SILENT && {}".format(" ".join(sys.argv[:]))
|
|
|
|
|
|
- Popen([os.path.join(tmpdir, "setup.exe"), args], shell=True)
|
|
|
+ Popen([TMP_SETUPFILE, args], shell=True)
|
|
|
|
|
|
# An UpdateNeeded exception is raised so the script exit before being restarted by subprocess
|
|
|
raise UpdateNeeded()
|