Skip to content

Commit 0c76840

Browse files
authored
Merge pull request #187 from menkej/empty-folders
Empty folders are no longer created when only printing filenames.
2 parents c37c54b + 011d8ed commit 0c76840

File tree

5 files changed

+74
-17
lines changed

5 files changed

+74
-17
lines changed

icloudpd/base.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,6 @@ def download_photo(counter, photo):
403403
date_path = folder_structure.format(created_date)
404404

405405
download_dir = os.path.normpath(os.path.join(directory, date_path))
406-
407-
if not os.path.exists(download_dir):
408-
try:
409-
os.makedirs(download_dir)
410-
except OSError: # pragma: no cover
411-
pass # pragma: no cover
412-
413406
download_size = size
414407

415408
try:

icloudpd/download.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ def download_media(icloud, photo, download_path, size):
3636
"""Download the photo to path, with retries and error handling"""
3737
logger = setup_logger()
3838

39+
# get back the directory for the file to be downloaded and create it if not there already
40+
download_dir = os.path.dirname(download_path)
41+
42+
if not os.path.exists(download_dir):
43+
try:
44+
os.makedirs(download_dir)
45+
except OSError: # pragma: no cover
46+
pass # pragma: no cover
47+
3948
for retries in range(constants.MAX_RETRIES):
4049
try:
4150
photo_response = photo.download(size)

scripts/run_all_checks

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/bin/bash
22
set -euo pipefail
3+
4+
# realpath not availabe on Mac by default...
5+
realpath() {
6+
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
7+
}
8+
39
CURRENT_DIR=$(dirname "$0")
410
ROOT_DIR="$(realpath $(dirname "$0")/..)"
511

scripts/test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/bin/bash
22
set -euo pipefail
3+
4+
# realpath not availabe on Mac by default...
5+
realpath() {
6+
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
7+
}
8+
39
CURRENT_DIR=$(dirname "$0")
410
ROOT_DIR="$(realpath $(dirname "$0")/..)"
511

tests/test_listing_recent_photos.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
from unittest import TestCase
2-
from vcr import VCR
32
import os
43
import shutil
5-
import click
6-
from click.testing import CliRunner
74
import json
85
import mock
6+
from vcr import VCR
7+
from click.testing import CliRunner
98
from icloudpd.base import main
109
from tests.helpers.print_result_exception import print_result_exception
1110

1211
vcr = VCR(decode_compressed_response=True)
1312

1413
class ListingRecentPhotosTestCase(TestCase):
14+
1515
def test_listing_recent_photos(self):
1616
base_dir = os.path.normpath("tests/fixtures/Photos")
1717
if os.path.exists(base_dir):
@@ -69,6 +69,45 @@ def test_listing_recent_photos(self):
6969
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7404.MOV")), filenames[7]
7070
)
7171

72+
assert result.exit_code == 0
73+
74+
def test_listing_photos_does_not_create_folders(self):
75+
base_dir = os.path.normpath("tests/fixtures/Photos")
76+
if os.path.exists(base_dir):
77+
shutil.rmtree(base_dir)
78+
os.makedirs(base_dir)
79+
80+
# make sure the directory does not exist yet.
81+
# Should only be created after download, not after just --print-filenames
82+
self.assertFalse(os.path.exists(os.path.join(base_dir, os.path.normpath("2018/07/31"))))
83+
84+
# Note - This test uses the same cassette as test_download_photos.py
85+
with vcr.use_cassette("tests/vcr_cassettes/listing_photos.yml"):
86+
# Pass fixed client ID via environment variable
87+
os.environ["CLIENT_ID"] = "DE309E26-942E-11E8-92F5-14109FE0B321"
88+
runner = CliRunner()
89+
result = runner.invoke(
90+
main,
91+
[
92+
"--username",
93+
"jdoe@gmail.com",
94+
"--password",
95+
"password1",
96+
"--recent",
97+
"5",
98+
"--only-print-filenames",
99+
"--no-progress-bar",
100+
"--threads-num",
101+
1,
102+
"-d",
103+
base_dir,
104+
],
105+
)
106+
print_result_exception(result)
107+
# make sure the directory still does not exist.
108+
# Should only be created after download, not after just --print-filenames
109+
self.assertFalse(
110+
os.path.exists(os.path.join(base_dir, os.path.normpath("2018/07/31"))))
72111

73112
assert result.exit_code == 0
74113

@@ -110,19 +149,24 @@ def test_listing_recent_photos_with_missing_filenameEnc(self):
110149

111150
# self.assertEqual(len(filenames), 5)
112151
self.assertEqual(
113-
os.path.join(base_dir, os.path.normpath("2018/07/31/AY6c_BsE0jja.JPG")), filenames[0]
152+
os.path.join(base_dir, os.path.normpath("2018/07/31/AY6c_BsE0jja.JPG")),
153+
filenames[0]
114154
)
115155
self.assertEqual(
116-
os.path.join(base_dir, os.path.normpath("2018/07/31/AY6c_BsE0jja.MOV")), filenames[1]
156+
os.path.join(base_dir, os.path.normpath("2018/07/31/AY6c_BsE0jja.MOV")),
157+
filenames[1]
117158
)
118159
self.assertEqual(
119-
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.JPG")), filenames[2]
160+
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.JPG")),
161+
filenames[2]
120162
)
121163
self.assertEqual(
122-
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.MOV")), filenames[3]
164+
os.path.join(base_dir, os.path.normpath("2018/07/30/IMG_7408.MOV")),
165+
filenames[3]
123166
)
124167
self.assertEqual(
125-
os.path.join(base_dir, os.path.normpath("2018/07/30/AZ_wAGT9P6jh.JPG")), filenames[4]
168+
os.path.join(base_dir, os.path.normpath("2018/07/30/AZ_wAGT9P6jh.JPG")),
169+
filenames[4]
126170
)
127171
assert result.exit_code == 0
128172

@@ -186,5 +230,4 @@ def test_listing_recent_photos_with_missing_downloadURL(self):
186230
self.assertEqual(
187231
first_arg['asset_record']['fields']['assetDate']['value'],
188232
1533021744816)
189-
assert result.exit_code == 0
190-
233+
assert result.exit_code == 0

0 commit comments

Comments
 (0)