|
2 | 2 | import re |
3 | 3 | import warnings |
4 | 4 | import zipfile |
| 5 | +from contextlib import suppress |
5 | 6 | from seleniumbase.config import proxy_list |
6 | 7 | from seleniumbase.config import settings |
7 | 8 | from seleniumbase.fixtures import constants |
8 | 9 | from seleniumbase.fixtures import page_utils |
| 10 | +from seleniumbase.fixtures import shared_utils |
9 | 11 |
|
10 | 12 | DOWNLOADS_DIR = constants.Files.DOWNLOADS_FOLDER |
11 | 13 | PROXY_ZIP_PATH = os.path.join(DOWNLOADS_DIR, "proxy.zip") |
@@ -109,45 +111,48 @@ def create_proxy_ext( |
109 | 111 | """"minimum_chrome_version":"22.0.0"\n""" |
110 | 112 | """}""" |
111 | 113 | ) |
112 | | - import threading |
113 | | - |
114 | | - lock = threading.RLock() # Support multi-threaded tests. Eg. "pytest -n=4" |
115 | | - with lock: |
116 | | - abs_path = os.path.abspath(".") |
117 | | - downloads_path = os.path.join(abs_path, DOWNLOADS_DIR) |
118 | | - if not os.path.exists(downloads_path): |
119 | | - os.mkdir(downloads_path) |
120 | | - if zip_it: |
121 | | - zf = zipfile.ZipFile(PROXY_ZIP_PATH, mode="w") |
122 | | - zf.writestr("background.js", background_js) |
123 | | - zf.writestr("manifest.json", manifest_json) |
124 | | - zf.close() |
125 | | - else: |
126 | | - proxy_ext_dir = PROXY_DIR_PATH |
127 | | - if not os.path.exists(proxy_ext_dir): |
128 | | - os.mkdir(proxy_ext_dir) |
129 | | - manifest_file = os.path.join(proxy_ext_dir, "manifest.json") |
130 | | - with open(manifest_file, mode="w") as f: |
131 | | - f.write(manifest_json) |
132 | | - proxy_host = proxy_string.split(":")[0] |
133 | | - proxy_port = proxy_string.split(":")[1] |
134 | | - background_file = os.path.join(proxy_ext_dir, "background.js") |
135 | | - with open(background_file, mode="w") as f: |
136 | | - f.write(background_js) |
| 114 | + abs_path = os.path.abspath(".") |
| 115 | + downloads_path = os.path.join(abs_path, DOWNLOADS_DIR) |
| 116 | + if not os.path.exists(downloads_path): |
| 117 | + os.mkdir(downloads_path) |
| 118 | + if zip_it: |
| 119 | + zf = zipfile.ZipFile(PROXY_ZIP_PATH, mode="w") |
| 120 | + zf.writestr("background.js", background_js) |
| 121 | + zf.writestr("manifest.json", manifest_json) |
| 122 | + zf.close() |
| 123 | + with suppress(Exception): |
| 124 | + shared_utils.make_writable(PROXY_ZIP_PATH) |
| 125 | + else: |
| 126 | + proxy_ext_dir = PROXY_DIR_PATH |
| 127 | + if not os.path.exists(proxy_ext_dir): |
| 128 | + os.mkdir(proxy_ext_dir) |
| 129 | + with suppress(Exception): |
| 130 | + shared_utils.make_writable(proxy_ext_dir) |
| 131 | + manifest_file = os.path.join(proxy_ext_dir, "manifest.json") |
| 132 | + with open(manifest_file, mode="w") as f: |
| 133 | + f.write(manifest_json) |
| 134 | + with suppress(Exception): |
| 135 | + shared_utils.make_writable(manifest_json) |
| 136 | + proxy_host = proxy_string.split(":")[0] |
| 137 | + proxy_port = proxy_string.split(":")[1] |
| 138 | + background_file = os.path.join(proxy_ext_dir, "background.js") |
| 139 | + with open(background_file, mode="w") as f: |
| 140 | + f.write(background_js) |
| 141 | + with suppress(Exception): |
| 142 | + shared_utils.make_writable(background_js) |
137 | 143 |
|
138 | 144 |
|
139 | 145 | def remove_proxy_zip_if_present(): |
140 | 146 | """Remove Chromium extension zip file used for proxy server authentication. |
141 | 147 | Used in the implementation of https://stackoverflow.com/a/35293284 |
142 | 148 | for https://stackoverflow.com/questions/12848327/ |
143 | 149 | """ |
144 | | - try: |
145 | | - if os.path.exists(PROXY_ZIP_PATH): |
| 150 | + if os.path.exists(PROXY_ZIP_PATH): |
| 151 | + with suppress(Exception): |
146 | 152 | os.remove(PROXY_ZIP_PATH) |
147 | | - if os.path.exists(PROXY_ZIP_LOCK): |
| 153 | + if os.path.exists(PROXY_ZIP_LOCK): |
| 154 | + with suppress(Exception): |
148 | 155 | os.remove(PROXY_ZIP_LOCK) |
149 | | - except Exception: |
150 | | - pass |
151 | 156 |
|
152 | 157 |
|
153 | 158 | def validate_proxy_string(proxy_string): |
|
0 commit comments