Chip Boling | 32aab30 | 2019-01-23 10:50:18 -0600 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | from task import Task |
| 17 | from twisted.internet.defer import inlineCallbacks, TimeoutError, failure, AlreadyCalledError |
| 18 | from twisted.internet import reactor |
| 19 | from voltha.extensions.omci.omci_defs import ReasonCodes |
| 20 | import requests |
| 21 | import os |
| 22 | import time |
| 23 | |
| 24 | class FileDownloadTask(Task): |
| 25 | name = "Image File Download Task" |
| 26 | CHUNK_SIZE = 1024 |
| 27 | |
| 28 | def __init__(self, omci_agent, img_dnld, clock= None): #device_id, url, local_path) |
| 29 | super(FileDownloadTask, self).__init__(FileDownloadTask.name, omci_agent, img_dnld.id, |
| 30 | exclusive=False, |
| 31 | watchdog_timeout=45) |
| 32 | # self.url = url |
| 33 | # self.local_path = local_path |
| 34 | self._image_download = img_dnld |
| 35 | self.reactor = clock if clock is not None else reactor |
| 36 | self._local_deferred = None |
| 37 | # self._request = None |
| 38 | # self._file = None |
| 39 | # self.log.debug('{} running'.format(FileDownloadTask.name)) |
| 40 | |
| 41 | # def __save_data(self): |
| 42 | # chunk = self._request.iter_content(chunk_size=FileDownloadTask.CHUNK_SIZE) |
| 43 | # if len(chunk) == 0: |
| 44 | # self._file.close() |
| 45 | # self.deferred.callback(self._image_download) |
| 46 | # else: |
| 47 | # self._file.write(chunk) |
| 48 | # self._image_download.downloaded_bytes += len(chunk) |
| 49 | # self.reactor.callLater(0, self.__save_data) |
| 50 | |
| 51 | @inlineCallbacks |
| 52 | def perform_download_data(self): |
| 53 | try: |
| 54 | r = requests.get(self._image_download.url, stream=True) |
| 55 | with open(self._image_download.local_dir + '/' + self._image_download.name, 'wb') as f: |
| 56 | for chunk in r.iter_content(chunk_size=FileDownloadTask.CHUNK_SIZE): |
| 57 | self.strobe_watchdog() |
| 58 | if chunk: # filter out keep-alive new chunks |
| 59 | yield f.write(chunk) |
| 60 | self._image_download.file_size += len(chunk) |
| 61 | # yield time.sleep(1) |
| 62 | self.deferred.callback(self._image_download) |
| 63 | except Exception as e: |
| 64 | self.deferred.errback(failure.Failure(e)) |
| 65 | |
| 66 | def start(self): |
| 67 | super(FileDownloadTask, self).start() |
| 68 | if not os.path.exists(self._image_download.local_dir): |
| 69 | os.makedirs(self._image_download.local_dir) |
| 70 | |
| 71 | self.strobe_watchdog() |
| 72 | self._image_download.file_size = 0 |
| 73 | self._local_deferred = self.reactor.callLater(0, self.perform_download_data) |
| 74 | # try: |
| 75 | # if not os.path.exists(self._image_download.local_dir): |
| 76 | # os.makedirs(self._image_download.local_dir) |
| 77 | |
| 78 | # self.strobe_watchdog() |
| 79 | # self._image_download.downloaded_bytes = 0 |
| 80 | # self.reactor.callLater(0, self.perform_download_data) |
| 81 | |
| 82 | # self._request = requests.get(self._image_download.url, stream=True) |
| 83 | # with open(self._image_download.local_dir + '/' + self._image_download.name, 'wb') as f: |
| 84 | # for chunk in r.iter_content(chunk_size=FileDownloadTask.CHUNK_SIZE): |
| 85 | # self.strobe_watchdog() |
| 86 | # if chunk: # filter out keep-alive new chunks |
| 87 | # f.write(chunk) |
| 88 | # self._image_download.downloaded_bytes += len(chunk) |
| 89 | |
| 90 | # self.deferred.callback(self._image_download) |
| 91 | # except Exception as e: |
| 92 | # self.deferred.errback(failure.Failure(e)) |
| 93 | |
| 94 | # def stop(self): |
| 95 | # # self.cancel_deferred() |
| 96 | # super(FileDownloadTask, self).stop() |
| 97 | |
| 98 | def cancel_deferred(self): |
| 99 | self.log.debug('FileDownloadTask cancel_deferred') |
| 100 | super(FileDownloadTask, self).cancel_deferred() |
| 101 | |
| 102 | d, self._local_deferred = self._local_deferred, None |
| 103 | try: |
| 104 | if d is not None and not d.called: |
| 105 | d.cancel() |
| 106 | except: |
| 107 | pass |
| 108 | |