Skip to content

Commit 341a075

Browse files
Merge pull request #55 from OceanNetworksCanada/issue-54-revert-download
Issue 54 revert download
2 parents ca0ccc2 + 1fea9a0 commit 341a075

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

src/onc/modules/_OncArchive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def downloadDirectArchivefile(
112112
filePath = outPath / filename
113113
fileExists = os.path.exists(filePath)
114114

115-
if not fileExists or overwrite:
115+
if not fileExists or os.path.getsize(filePath) == 0 or overwrite:
116116
print(f' ({tries} of {n}) Downloading file: "{filename}"')
117117
downInfo = self.downloadArchivefile(filename, overwrite)
118118
size += downInfo["size"]

src/onc/modules/_util.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import time
23
from datetime import timedelta
34
from pathlib import Path
@@ -17,16 +18,15 @@ def saveAsFile(
1718
filePath = outPath / fileName
1819
outPath.mkdir(parents=True, exist_ok=True)
1920

20-
# Save file in outPath if it doesn't exist yet
21-
if Path.exists(filePath) and not overwrite:
21+
# Save/Overwrite file in outPath if the file doesn't exist yet
22+
# or it is there but with 0 file size
23+
if not overwrite and Path.exists(filePath) and os.path.getsize(filePath) != 0:
2224
raise FileExistsError(filePath)
2325

2426
start = time.time()
2527
size = 0
26-
with open(filePath, "wb+") as file:
27-
for chunk in response.iter_content(chunk_size=128):
28-
file.write(chunk)
29-
size += len(chunk)
28+
with open(filePath, "wb") as file:
29+
file.write(response.content)
3030

3131
downloadTime = time.time() - start
3232
return (size, round(downloadTime, 3))

src/onc/onc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,8 @@ def downloadArchivefile(self, filename: str = "", overwrite: bool = False):
13191319
filename : str, default ""
13201320
A valid name of a file in DMAS Archiving System.
13211321
overwrite : bool, default False
1322-
Whether to overwrite the file if it exists.
1322+
Whether to overwrite the file if it exists. 0 size file is treated as non-existent,
1323+
meaning it gets overwritten even when overwrite=False.
13231324
13241325
Returns
13251326
-------

tests/archive_file/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ def params_location():
1818
def params_location_multiple_pages(params_location):
1919
# rowLimit should be less than the total number of rows.
2020
return params_location | {"rowLimit": 2}
21+
22+
23+
@pytest.fixture
24+
def params_location_single_file(params_location):
25+
# Returned archivefile name should be BPR-Folger-59_20191126T000000.000Z.txt
26+
return params_location | {"dateFrom": "2019-11-26", "dateTo": "2019-11-27"}

tests/archive_file/test_archivefile_direct_download.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
3+
14
def test_valid_params_one_page(requester, params_location, util):
25
data = requester.getArchivefile(params_location)
36
result = requester.downloadDirectArchivefile(params_location)
@@ -16,3 +19,24 @@ def test_valid_params_multiple_pages(requester, params_location_multiple_pages,
1619
)
1720

1821
assert result["stats"]["fileCount"] == params_location_multiple_pages["rowLimit"]
22+
23+
24+
def test_valid_params_overwrite_zero_file_size(
25+
requester, params_location_single_file, util
26+
):
27+
filename = "BPR-Folger-59_20191126T000000.000Z.txt"
28+
29+
file_path = requester.outPath / filename
30+
31+
# Touch an empty file
32+
with open(file_path, "w"):
33+
pass
34+
35+
# Case when downloading failed, leaving an empty file behind
36+
assert os.path.getsize(file_path) == 0
37+
38+
requester.downloadDirectArchivefile(params_location_single_file, overwrite=False)
39+
40+
assert (
41+
os.path.getsize(file_path) != 0
42+
), "0-size file should be overwritten even if overwrite is False"

tests/archive_file/test_archivefile_download.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pytest
24
import requests
35

@@ -25,3 +27,22 @@ def test_valid_params(requester, util):
2527
requester.downloadArchivefile(filename, overwrite=True)
2628

2729
assert util.get_download_files_num(requester) == 1
30+
31+
32+
def test_valid_params_overwrite_zero_file_size(requester):
33+
filename = "BPR-Folger-59_20191123T000000.000Z.txt"
34+
35+
file_path = requester.outPath / filename
36+
37+
# Touch an empty file
38+
with open(file_path, "w"):
39+
pass
40+
41+
# Case when downloading failed, leaving an empty file behind
42+
assert os.path.getsize(file_path) == 0
43+
44+
requester.downloadArchivefile(filename, overwrite=False)
45+
46+
assert (
47+
os.path.getsize(file_path) != 0
48+
), "0-size file should be overwritten even if overwrite is False"

0 commit comments

Comments
 (0)