1+ from sys import version
2+ from tkinter import *
3+ from tkinter import messagebox
4+ import os
5+ import requests
6+ from Updater import changelogDiag , downloadDiag , legacy
7+ from vars import *
8+ from tokenString import *
9+ import json
10+ import time
11+
12+ root = Tk ()
13+ root .withdraw ()
14+
15+ def downloadNewVersion (versionString , softwareName , legacyMode ):
16+ os .system ("taskkill /f /im " + softwareName + ".exe" )
17+
18+ url = "https://github.com/" + GITHUB_USER + "/" + GITHUB_REPO + "/releases/download/" + versionString + "/" + softwareName + ".exe"
19+ r = requests .get (url , stream = True )
20+
21+ downDiag = downloadDiag .DownloadDiag (root , "Downloading " + softwareName + " " + versionString )
22+ while downDiag == None : pass
23+
24+ with open (softwareName + ".exe" , 'wb' ) as f :
25+ startTime = time .mktime (time .localtime ())
26+ downloaded = 0
27+ total = int (r .headers .get ('content-length' ))
28+
29+ for chunk in r .iter_content (chunk_size = max (int (total / 1000 ), 1024 * 1024 )):
30+ if chunk :
31+ downloaded += len (chunk )
32+ f .write (chunk )
33+ cTime = time .mktime (time .localtime ())
34+
35+ downDiag .updateValues (downloaded , total , 0 if cTime == startTime else round (downloaded / (cTime - startTime ), 2 ))
36+
37+ downDiag .destroy ()
38+
39+ if not legacyMode :
40+ content = []
41+ with open (VERSION_PATH , 'r' ) as f :
42+ content = json .loads (f .read ())
43+
44+ content [softwareName ] = versionString
45+ with open (VERSION_PATH , 'w' ) as f :
46+ f .write (json .dumps (content , indent = 4 , separators = (',' , ': ' )))
47+ else :
48+ content = []
49+ with open (OLD_VERSION_PATH , 'r' ) as f :
50+ content = f .readlines ()
51+
52+ content [LEGACY_VERSIONS .index (softwareName )] = versionString
53+ for i in range (0 , len (content )):
54+ if i != len (content ) - 1 : content [i ] += "\n "
55+
56+ with open (OLD_VERSION_PATH , 'w' ) as f :
57+ f .writelines (content )
58+
59+ def restartProgram (softwareName ):
60+ os .startfile (softwareName + ".exe" )
61+
62+ def checkNewVersion (softwareName ):
63+ isNewVersion = False
64+ legacyMode = False
65+
66+ # Update old version file
67+ if not os .path .exists (VERSION_PATH ):
68+ if os .path .exists (OLD_VERSION_PATH ):
69+ version = []
70+ with open (OLD_VERSION_PATH , 'r' ) as f :
71+ version = f .readlines ()
72+
73+ for i in range (0 , len (version )): version [i ] = version [i ].strip ()
74+ legacyMode = legacy .checkLegacy (version )
75+
76+ if not legacyMode :
77+ newVersion = {GITHUB_REPO : version [0 ], "Updater" : version [1 ]}
78+ os .remove (OLD_VERSION_PATH )
79+ else :
80+ newVersion = {GITHUB_REPO : "v1.0" , "Updater" : "v1.0" }
81+
82+ if not legacyMode :
83+ with open (VERSION_PATH , 'w' ) as f :
84+ f .write (json .dumps (newVersion , indent = 4 , separators = (',' , ': ' )))
85+
86+ if legacyMode :
87+ with open (OLD_VERSION_PATH , 'r' ) as f :
88+ versionString = f .readlines ()[LEGACY_VERSIONS .index (softwareName )]
89+ else :
90+ with open (VERSION_PATH , 'r' ) as f :
91+ versionString = json .loads (f .read ())[softwareName ]
92+
93+ versionNumber = versionString .split ("v" )[1 ]
94+
95+ response = requests .get ("https://api.github.com/repos/" + GITHUB_USER + "/" + GITHUB_REPO + "/releases" , headers = {"Authorization" : TOKEN })
96+ releases = response .json ()
97+
98+ for r in releases :
99+ tokenized = r ["name" ].split ()
100+ if tokenized [0 ] == softwareName :
101+ latestVersionString = tokenized [1 ]
102+ latestVersionNumber = latestVersionString .split ("v" )[1 ]
103+
104+ if versionNumber > latestVersionNumber :
105+ break
106+
107+ if versionNumber < latestVersionNumber :
108+ res = messagebox .askquestion ("Updater" , "A new version of " + softwareName + " is available: " + latestVersionString + "\n Do you want to update?" )
109+ if res == "yes" :
110+ downloadNewVersion (latestVersionString , softwareName , legacyMode )
111+
112+ changelogRaw = r ["body" ].split ("##" )[1 ].split ("\r \n " )
113+ changelog = []
114+ for c in changelogRaw [1 :]:
115+ if c != "" : changelog .append (c )
116+
117+ changelogDiag .ChangelogDiag (root , "Changelog" , changelog )
118+
119+ isNewVersion = True
120+
121+ restartProgram (softwareName )
122+ break
123+
124+ root .destroy ()
125+ return isNewVersion
0 commit comments