55from zipfile import ZipFile
66from urllib .request import urlretrieve
77
8+ LATEST_RELEASE_STABLE = (
9+ "curl https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE"
10+ )
811
9- def get_driver_version ():
10- ps = "curl https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE"
11- return subprocess .check_output (ps , shell = True ).decode ("utf-8" )
12+
13+ def get_webpage_content ( url ):
14+ return subprocess .check_output (url , shell = True ).decode ("utf-8" )
1215
1316
1417def get_download_link (version_number ):
@@ -29,34 +32,39 @@ def get_download_link(version_number):
2932 return link
3033
3134
32- def check_file ():
33- if exists ("./src/driver_version .json" ):
35+ def check_file (filepath = "./src/versions.json" ):
36+ if exists ("./src/versions .json" ):
3437 return
3538 else :
36- with open ("./src/driver_version.json" , "w" , encoding = "utf-8" ) as openfile :
37- template = {"Chrome_Driver_Version" : "" }
39+ with open (filepath , "w" , encoding = "utf-8" ) as openfile :
40+ template = {"Chrome_Driver_Version" : "" , "App_Version" : "" }
3841 json .dump (template , openfile , indent = 4 )
3942 return
4043
4144
42- def get_local_driver_version ():
45+ def unpack_zip (filepath , destination ):
46+ with ZipFile (filepath , "r" ) as zipObj :
47+ zipObj .extractall (destination )
48+
49+
50+ def get_local_version (version_type , filepath = "./src/versions.json" ):
4351 version = None
4452 try :
45- with open ("./src/driver_version.json" , "r" , encoding = "utf-8" ) as openfile :
53+ with open (filepath , "r" , encoding = "utf-8" ) as openfile :
4654 json_object = json .load (openfile )
47- version = json_object ["Chrome_Driver_Version" ]
48- except KeyError :
55+ version = json_object [version_type ]
56+ except :
4957 version = ""
5058 return version
5159
5260
53- def update_version (version ):
61+ def update_version (version , version_type , filepath = "./src/versions.json" ):
5462 json_object = None
55- with open ("./src/driver_version.json" , "r" , encoding = "utf-8" ) as openfile :
63+ with open (filepath , "r" , encoding = "utf-8" ) as openfile :
5664 json_object = json .load (openfile )
5765
58- with open ("./src/driver_version.json" , "w" , encoding = "utf-8" ) as openfile :
59- json_object ["Chrome_Driver_Version" ] = version
66+ with open (filepath , "w" , encoding = "utf-8" ) as openfile :
67+ json_object [version_type ] = version
6068 json .dump (json_object , openfile , indent = 4 )
6169
6270
@@ -65,20 +73,25 @@ def clean():
6573 return
6674
6775
76+ def get_remote_zip (url , filename ):
77+ urlretrieve (url , filename )
78+ return
79+
80+
6881def compare_and_download ():
6982 check_file ()
70- local_version = get_local_driver_version ( )
71- remote_version = get_driver_version ( )
83+ local_version = get_local_version ( "Chrome_Driver_Version" )
84+ remote_version = get_webpage_content ( LATEST_RELEASE_STABLE )
7285
7386 if local_version .split ("." )[0 ] == remote_version .split ("." )[0 ]:
7487 print ("chrome driver up to date" )
7588 return
7689
7790 remote_download_link = get_download_link (remote_version )
7891
79- urlretrieve (remote_download_link , "./driver.zip" )
92+ get_remote_zip (remote_download_link , "./driver.zip" )
93+
94+ unpack_zip ("./driver.zip" , "./" )
8095
81- with ZipFile ("./driver.zip" , "r" ) as zipObj :
82- zipObj .extractall ()
83- update_version (remote_version )
96+ update_version (remote_version , "Chrome_Driver_Version" )
8497 clean ()
0 commit comments