def download_subscriber_voices(selected_voice): """ Function to download all premium voices, starting with the currently selected if applicable """ def make_executable(dest): """ Call back function to make the downloaded file executable. """ LOG.info('Make executable') # make executable st = os.stat(dest) os.chmod(dest, st.st_mode | stat.S_IEXEC) # First download the selected voice if needed voice_file = SUBSCRIBER_VOICES.get(selected_voice) print voice_file if voice_file is not None and not exists(voice_file): print 'voice foesn\'t exist, downloading' dl = download(DeviceApi().get_subscriber_voice_url(selected_voice), voice_file, make_executable) # Wait for completion while not dl.done: sleep(1) # Download the rest of the subsciber voices as needed for voice in SUBSCRIBER_VOICES: voice_file = SUBSCRIBER_VOICES[voice] if not exists(voice_file): dl = download(DeviceApi().get_subscriber_voice_url(voice), voice_file, make_executable) # Wait for completion while not dl.done: sleep(1)
def test_download_cache(self, mock_os, mock_subprocess): """Make sure that a cached download is used if exists.""" transfer_done = Event() def wget_call(*args, **kwargs): nonlocal transfer_done transfer_done.wait() return 0 downloader = download(url=TEST_URL, dest=TEST_DEST) downloader2 = download(url=TEST_URL, dest=TEST_DEST) # When called with the same args a cached download in progress should # be returned instead of a new one. self.assertTrue(downloader is downloader2) transfer_done.set() downloader.join()
def download_subscriber_voices(selected_voice): """Function to download all premium voices. The function starts with the currently selected if applicable """ subscriber_voices = get_subscriber_voices() def make_executable(dest): """Call back function to make the downloaded file executable.""" LOG.info('Make executable new voice binary executable') # make executable file_stat = os.stat(dest) os.chmod(dest, file_stat.st_mode | stat.S_IEXEC) # First download the selected voice if needed voice_file = subscriber_voices.get(selected_voice) if voice_file is not None and not exists(voice_file): LOG.info('Voice doesn\'t exist, downloading') url = DeviceApi().get_subscriber_voice_url(selected_voice) # Check we got an url if url: dl_status = download(url, voice_file, make_executable) # Wait for completion while not dl_status.done: sleep(1) else: LOG.debug('{} is not available for this architecture'.format( selected_voice)) # Download the rest of the subsciber voices as needed for voice in subscriber_voices: voice_file = subscriber_voices[voice] if not exists(voice_file): url = DeviceApi().get_subscriber_voice_url(voice) # Check we got an url if url: dl_status = download(url, voice_file, make_executable) # Wait for completion while not dl_status.done: sleep(1) else: LOG.debug( '{} is not available for this architecture'.format(voice))
def download_subscriber_voices(selected_voice): """ Function to download all premium voices, starting with the currently selected if applicable """ def make_executable(dest): """ Call back function to make the downloaded file executable. """ LOG.info('Make executable') # make executable st = os.stat(dest) os.chmod(dest, st.st_mode | stat.S_IEXEC) # First download the selected voice if needed voice_file = SUBSCRIBER_VOICES.get(selected_voice) if voice_file is not None and not exists(voice_file): LOG.info('voice doesn\'t exist, downloading') url = DeviceApi().get_subscriber_voice_url(selected_voice) # Check we got an url if url: dl = download(url, voice_file, make_executable) # Wait for completion while not dl.done: sleep(1) else: LOG.debug('{} is not available for this architecture' .format(selected_voice)) # Download the rest of the subsciber voices as needed for voice in SUBSCRIBER_VOICES: voice_file = SUBSCRIBER_VOICES[voice] if not exists(voice_file): url = DeviceApi().get_subscriber_voice_url(voice) # Check we got an url if url: dl = download(url, voice_file, make_executable) # Wait for completion while not dl.done: sleep(1) else: LOG.debug('{} is not available for this architecture' .format(voice))
def test_download_basic(self, mock_os, mock_subprocess): """Test the basic download call.""" mock_subprocess.call.return_value = 0 downloader = download(url=TEST_URL, dest=TEST_DEST) downloader.join() mock_subprocess.call.assert_called_once_with([ 'wget', '-c', TEST_URL, '-O', TEST_DEST + '.part', '--tries=20', '--read-timeout=5' ]) self.assertTrue(downloader.done)
def test_download_with_header(self, mock_os, mock_subprocess): """Test download with specific header.""" mock_subprocess.call.return_value = 0 test_hdr = 'TEST_HEADER' downloader = download(url=TEST_URL, dest=TEST_DEST, header=test_hdr) downloader.join() self.assertTrue(downloader.done) mock_subprocess.call.assert_called_once_with([ 'wget', '-c', TEST_URL, '-O', TEST_DEST + '.part', '--tries=20', '--read-timeout=5', '--header=' + test_hdr ])
def test_download_callback(self, mock_os, mock_subprocess): """Check that callback function is called with correct destination.""" mock_subprocess.call.return_value = 0 action_called_with = None def action(dest): nonlocal action_called_with action_called_with = dest downloader = download(url=TEST_URL, dest=TEST_DEST, complete_action=action) downloader.join() self.assertTrue(downloader.done) self.assertEqual(action_called_with, TEST_DEST)