Skip to content

Commit

Permalink
Create directories
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed Nov 1, 2023
1 parent 20720e7 commit c9c877a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
3 changes: 3 additions & 0 deletions py/desitransfer/spacewatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def download_jpg(files, destination, overwrite=False, test=False):
The number of files downloaded.
"""
downloaded = 0
if not test and not os.path.isdir(destination):
log.debug("os.makedirs('%s')", destination)
os.makedirs(destination)
for jpg in files:
base_jpg = jpg.split('/')[-1]
dst_jpg = os.path.join(destination, base_jpg)
Expand Down
27 changes: 16 additions & 11 deletions py/desitransfer/test/test_spacewatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def test_jpg_files(self, mock_requests):
def test_download_jpg(self, mock_exists, mock_requests, mock_utime, mock_log):
"""Test downloads of JPEG files.
"""
mock_exists.side_effect = [True, False, False, False]
mock_exists.side_effect = lambda x: x == os.path.join(destination, 'baz', '20231031_000005.jpg')
# mock_exists.return_value = False
mock_contents = Mock()
mock_contents.headers = {'Last-Modified': 'Mon, 30 Oct 2023 00:00:24 GMT'}
mock_contents.status_code = 200
Expand All @@ -166,17 +167,21 @@ def test_download_jpg(self, mock_exists, mock_requests, mock_utime, mock_log):
'http://foo.bar/20231031_000405.jpg',
'http://foo.bar/20231031_000605.jpg']
destination = self.tmp.name
n = download_jpg(files, destination)
n = download_jpg(files, destination + '/baz')
self.assertEqual(n, 3)
mock_exists.assert_has_calls([call(os.path.join(destination, '20231031_000005.jpg')),
call(os.path.join(destination, '20231031_000205.jpg')),
call(os.path.join(destination, '20231031_000405.jpg')),
call(os.path.join(destination, '20231031_000605.jpg'))])
mock_exists.assert_has_calls([call(os.path.join(destination, 'baz', '20231031_000005.jpg')),
call(os.path.join(destination, 'baz', '20231031_000205.jpg')),
call(os.path.join(destination, 'baz', '20231031_000405.jpg')),
call(os.path.join(destination, 'baz', '20231031_000605.jpg'))])
mock_requests.get.assert_has_calls([call('http://foo.bar/20231031_000205.jpg'),
call('http://foo.bar/20231031_000405.jpg'),
call('http://foo.bar/20231031_000605.jpg')])
mock_utime.assert_has_calls([call(os.path.join(destination, '20231031_000205.jpg'), (1698624024, 1698624024)),
call(os.path.join(destination, '20231031_000405.jpg'), (1698624024, 1698624024)),
call(os.path.join(destination, '20231031_000605.jpg'), (1698624024, 1698624024))])
mock_log.debug.assert_has_calls([call("Skipping existing file: %s.",
os.path.join(destination, '20231031_000005.jpg'))])
mock_utime.assert_has_calls([call(os.path.join(destination, 'baz', '20231031_000205.jpg'), (1698624024, 1698624024)),
call(os.path.join(destination, 'baz', '20231031_000405.jpg'), (1698624024, 1698624024)),
call(os.path.join(destination, 'baz', '20231031_000605.jpg'), (1698624024, 1698624024))])
mock_log.debug.assert_has_calls([call("os.makedirs('%s')", os.path.join(destination, 'baz')),
call("Skipping existing file: %s.",
os.path.join(destination, 'baz', '20231031_000005.jpg')),
call("r = requests.get('%s')", 'http://foo.bar/20231031_000205.jpg'),
call("r = requests.get('%s')", 'http://foo.bar/20231031_000405.jpg'),
call("r = requests.get('%s')", 'http://foo.bar/20231031_000605.jpg')])

0 comments on commit c9c877a

Please sign in to comment.