22import json
33from os .path import exists
44from os import remove
5- from zipfile import ZipFile
65from urllib .request import urlretrieve
76import shutil
87
1211
1312
1413def get_webpage_content (url ):
14+ """Returns the page content of specified url
15+
16+ Args:
17+ url (str): url to downloadable content
18+
19+ Returns:
20+ str: Returns string value of content
21+ """
1522 return subprocess .check_output (url , shell = True ).decode ("utf-8" )
1623
1724
1825def get_download_link (version_number ):
26+ """Sifts through google's json API endpoints to find
27+ the matching chromedriver download to specified version
28+
29+ Args:
30+ version_number (str): version to find the download for
31+
32+ Returns:
33+ str: returns url of zip to download
34+ """
1935 downloads_json = "curl https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
2036 downloads = subprocess .check_output (downloads_json , shell = True )
2137 link = None
@@ -34,6 +50,12 @@ def get_download_link(version_number):
3450
3551
3652def check_file (filepath = "./src/versions.json" ):
53+ """checks to see if the versions.json exists
54+ if not, it will create one.
55+
56+ Args:
57+ filepath (str, optional): Defaults to "./src/versions.json".
58+ """
3759 if exists ("./src/versions.json" ):
3860 return
3961 else :
@@ -44,21 +66,39 @@ def check_file(filepath="./src/versions.json"):
4466
4567
4668def unpack_zip (filepath , destination ):
69+ """unpacks zip from filepath to destination
70+
71+ Args:
72+ filepath (str): path to zip
73+ destination (str): unpack path
74+ """
4775 shutil .unpack_archive (filepath , destination , "zip" )
4876
4977
5078def get_local_version (version_type , filepath = "./src/versions.json" ):
51- version = None
52- try :
53- with open (filepath , "r" , encoding = "utf-8" ) as openfile :
54- json_object = json .load (openfile )
55- version = json_object [version_type ]
56- except :
57- version = ""
79+ """Gets the installed version of specified version type from versions.json
80+
81+ Args:
82+ version_type (str): will return this version from the json
83+ filepath (str, optional): Defaults to "./src/versions.json".
84+
85+ Returns:
86+ str: version from versions.json
87+ """
88+ with open (filepath , "r" , encoding = "utf-8" ) as openfile :
89+ json_object = json .load (openfile )
90+ version = json_object [version_type ]
5891 return version
5992
6093
6194def update_version (version , version_type , filepath = "./src/versions.json" ):
95+ """_summary_
96+
97+ Args:
98+ version (_type_): _description_
99+ version_type (_type_): _description_
100+ filepath (str, optional): _description_. Defaults to "./src/versions.json".
101+ """
62102 json_object = None
63103 with open (filepath , "r" , encoding = "utf-8" ) as openfile :
64104 json_object = json .load (openfile )
@@ -69,21 +109,69 @@ def update_version(version, version_type, filepath="./src/versions.json"):
69109
70110
71111def clean ():
112+ """removes the zip file as it unnecessary once unpacked"""
72113 remove ("./driver.zip" )
73114 return
74115
75116
76117def get_remote_zip (url , filename ):
118+ """downloads the zip from the url
119+
120+ Args:
121+ url (str): url of download
122+ filename (str): what to name the downloaded file
123+ """
77124 urlretrieve (url , filename )
78125 return
79126
80127
128+ def get_local_chrome_version ():
129+ """Tries to retrieve the installed version of chrome
130+ by looking for the shortcut and finding the target path
131+
132+ If there is no chrome found it will simply return the latest version of
133+ chrome available.
134+
135+ Returns:
136+ str: version of chrome to match the driver to be downloaded.
137+ """
138+ shortcut_path_command = "wmic path win32_shortcutfile where name='C:\\ \\ ProgramData\\ \\ Microsoft\\ \\ Windows\\ \\ Start Menu\\ \\ Programs\\ \\ Google Chrome.lnk' get target /value"
139+ shortcut_output = subprocess .check_output (
140+ shortcut_path_command , shell = True , universal_newlines = True
141+ )
142+ shortcut = shortcut_output .split ("\n " )
143+ shortcut = [x for x in shortcut if x ]
144+
145+ # if there is something wrong with the path to chrome
146+ # this allow me to download the latest version instead
147+ # of matching chrome
148+ if not shortcut :
149+ return get_webpage_content (LATEST_RELEASE_STABLE )
150+
151+ shortcut = shortcut [0 ].split ("=" )[1 ]
152+ shortcut = shortcut .replace ("\\ " , "\\ \\ " )
153+ command = f"wmic datafile where name='{ shortcut } ' get Version /value"
154+ version_output = subprocess .check_output (
155+ command , shell = True , universal_newlines = True
156+ )
157+ version_output = version_output .split ("\n " )
158+ version_output = [x for x in version_output if x ]
159+ version_output = version_output [0 ].split ("=" )[1 ]
160+ version_output = version_output .split ("." )
161+ version_output [- 1 ] = "0"
162+ version_output = "." .join (version_output )
163+ return version_output
164+
165+
81166def compare_and_download ():
167+ """Finds the current installed version of the chromedriver
168+ and downloads the required version"""
82169 check_file ()
83170 local_version = get_local_version ("Chrome_Driver_Version" )
84- remote_version = get_webpage_content (LATEST_RELEASE_STABLE )
85171
86- if local_version .split ("." )[0 ] == remote_version .split ("." )[0 ]:
172+ remote_version = get_local_chrome_version ()
173+
174+ if local_version == remote_version :
87175 print ("chrome driver up to date" )
88176 return
89177
0 commit comments