Improve getting tmp download path

pull/2496/head
Åke Forslund 2020-04-05 20:49:24 +02:00
parent a3c625bb0a
commit c5dccc16e2
2 changed files with 26 additions and 10 deletions

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# #
from glob import glob
import os import os
from os.path import exists, dirname from os.path import exists, dirname
import subprocess import subprocess
@ -32,16 +33,11 @@ def _get_download_tmp(dest):
(str) path to temporary download location (str) path to temporary download location
""" """
tmp_base = dest + '.part' tmp_base = dest + '.part'
if not exists(tmp_base): existing = glob(tmp_base + '*')
if len(existing) > 0:
return '{}.{}'.format(tmp_base, len(existing))
else:
return tmp_base return tmp_base
else:
i = 1
while(True):
tmp = tmp_base + '.' + str(i)
if not exists(tmp):
return tmp
else:
i += 1
class Downloader(Thread): class Downloader(Thread):

View File

@ -1,7 +1,8 @@
from threading import Event from threading import Event
from unittest import TestCase, mock from unittest import TestCase, mock
from mycroft.util.download import download, _running_downloads from mycroft.util.download import (download, _running_downloads,
_get_download_tmp)
TEST_URL = 'http://example.com/mycroft-test.tar.gz' TEST_URL = 'http://example.com/mycroft-test.tar.gz'
TEST_DEST = '/tmp/file.tar.gz' TEST_DEST = '/tmp/file.tar.gz'
@ -83,3 +84,22 @@ class TestDownload(TestCase):
self.assertTrue(downloader is downloader2) self.assertTrue(downloader is downloader2)
transfer_done.set() transfer_done.set()
downloader.join() downloader.join()
@mock.patch('mycroft.util.download.glob')
class TestGetTemp(TestCase):
def test_no_existing(self, mock_glob):
mock_glob.return_value = []
dest = '/tmp/test'
self.assertEqual(_get_download_tmp(dest), dest + '.part')
def test_existing(self, mock_glob):
mock_glob.return_value = ['/tmp/test.part']
dest = '/tmp/test'
self.assertEqual(_get_download_tmp(dest), dest + '.part.1')
def test_multiple_existing(self, mock_glob):
mock_glob.return_value = ['/tmp/test.part', '/tmp/test.part.1',
'/tmp/test.part.2']
dest = '/tmp/test'
self.assertEqual(_get_download_tmp(dest), dest + '.part.3')