Milan Verespej | ccccef6 | 2019-06-18 15:50:40 +0200 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # COPYRIGHT NOTICE STARTS HERE |
| 5 | |
| 6 | # Copyright 2019 © Samsung Electronics Co., Ltd. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | |
| 20 | # COPYRIGHT NOTICE ENDS HERE |
| 21 | |
| 22 | import logging |
| 23 | import subprocess |
| 24 | from abc import ABC |
| 25 | from distutils.spawn import find_executable |
| 26 | |
| 27 | from downloader import AbstractDownloader |
| 28 | |
| 29 | log = logging.getLogger(__name__) |
| 30 | |
| 31 | |
| 32 | class CommandDownloader(AbstractDownloader, ABC): |
| 33 | def __init__(self, list_type, cli_tool, *list_args): |
| 34 | super().__init__(list_type, *list_args) |
| 35 | if not find_executable(cli_tool): |
| 36 | raise FileNotFoundError(cli_tool) |
| 37 | |
| 38 | def download(self): |
| 39 | """ |
| 40 | Download items from list |
| 41 | """ |
| 42 | if not self._initial_log(): |
| 43 | return |
| 44 | items_left = len(self._missing) |
| 45 | error_occurred = False |
| 46 | for item, dst_dir in self._data_list.items(): |
| 47 | try: |
| 48 | self._download_item((item, dst_dir)) |
| 49 | except subprocess.CalledProcessError as err: |
| 50 | log.exception(err.output.decode()) |
| 51 | error_occurred = True |
| 52 | items_left -= 1 |
| 53 | log.info('{} {} left to download.'.format(items_left, self._list_type)) |
| 54 | if error_occurred: |
| 55 | log.error('{} {} were not downloaded.'.format(items_left, self._list_type)) |
| 56 | raise RuntimeError('One or more errors occurred') |
| 57 | |
| 58 | def _download_item(self, item): |
| 59 | pass |