Skip to content

Commit 7c3e34b

Browse files
committed
Refactor downloading
1 parent 39aaaef commit 7c3e34b

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

seleniumbase/core/download_helper.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ def get_downloads_folder():
2424
def reset_downloads_folder():
2525
"""Clears the downloads folder.
2626
If settings.ARCHIVE_EXISTING_DOWNLOADS is set to True, archives it."""
27+
archived_downloads_folder = os.path.join(os.getcwd(), ARCHIVE_DIR) + os.sep
2728
if os.path.exists(downloads_path) and not os.listdir(downloads_path) == []:
28-
archived_downloads_folder = os.path.join(
29-
downloads_path, "..", ARCHIVE_DIR
30-
)
3129
reset_downloads_folder_assistant(archived_downloads_folder)
30+
if os.path.exists(downloads_path) and os.listdir(downloads_path) == []:
31+
try:
32+
os.rmdir(downloads_path)
33+
except OSError:
34+
pass
35+
if (
36+
os.path.exists(archived_downloads_folder)
37+
and os.listdir(archived_downloads_folder) == []
38+
):
39+
try:
40+
os.rmdir(archived_downloads_folder)
41+
except OSError:
42+
pass
3243

3344

3445
def reset_downloads_folder_assistant(archived_downloads_folder):

seleniumbase/fixtures/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ class MultiBrowser:
147147
DRIVER_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/driver_fixing.lock"
148148
DRIVER_REPAIRED = Files.DOWNLOADS_FOLDER + "/driver_fixed.lock"
149149
CERT_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/cert_fixing.lock"
150-
DOWNLOAD_FILE_LOCK = Files.DOWNLOADS_FOLDER + "/download_file.lock"
150+
DOWNLOAD_FILE_LOCK = Files.DOWNLOADS_FOLDER + "/downloading.lock"
151+
FILE_IO_LOCK = Files.DOWNLOADS_FOLDER + "/file_io.lock"
151152

152153

153154
class SavedCookies:

seleniumbase/fixtures/page_utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):
256256

257257

258258
def _save_data_as(data, destination_folder, file_name):
259-
download_file_lock = fasteners.InterProcessLock(
260-
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
259+
file_io_lock = fasteners.InterProcessLock(
260+
constants.MultiBrowser.FILE_IO_LOCK
261261
)
262-
with download_file_lock:
262+
with file_io_lock:
263263
out_file = codecs.open(
264264
os.path.join(destination_folder, file_name), "w+", encoding="utf-8"
265265
)
@@ -268,10 +268,10 @@ def _save_data_as(data, destination_folder, file_name):
268268

269269

270270
def _append_data_to_file(data, destination_folder, file_name):
271-
download_file_lock = fasteners.InterProcessLock(
272-
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
271+
file_io_lock = fasteners.InterProcessLock(
272+
constants.MultiBrowser.FILE_IO_LOCK
273273
)
274-
with download_file_lock:
274+
with file_io_lock:
275275
existing_data = ""
276276
if os.path.exists(os.path.join(destination_folder, file_name)):
277277
with open(os.path.join(destination_folder, file_name), "r") as f:
@@ -286,10 +286,10 @@ def _append_data_to_file(data, destination_folder, file_name):
286286

287287

288288
def _get_file_data(folder, file_name):
289-
download_file_lock = fasteners.InterProcessLock(
290-
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
289+
file_io_lock = fasteners.InterProcessLock(
290+
constants.MultiBrowser.FILE_IO_LOCK
291291
)
292-
with download_file_lock:
292+
with file_io_lock:
293293
if not os.path.exists(os.path.join(folder, file_name)):
294294
raise Exception("File not found!")
295295
with open(os.path.join(folder, file_name), "r") as f:

0 commit comments

Comments
 (0)