1 # -*- coding: utf-8 -*-
3 # COPYRIGHT NOTICE STARTS HERE
5 # Copyright 2019 © Samsung Electronics Co., Ltd.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # COPYRIGHT NOTICE ENDS HERE
22 from abc import ABC, abstractmethod
26 log = logging.getLogger(__name__)
29 class AbstractDownloader(ABC):
31 def __init__(self, list_type, *list_args):
32 self._list_type = list_type
33 self._data_list = {item: list_arg[1] for list_arg in list_args
34 for item in self.load_list(list_arg[0])}
35 self._missing = self.missing()
40 Type of resource in list
42 return self._list_type
48 :param path: path to file
49 :return: set of items in list
51 with open(path, 'r') as f:
52 return {item for item in (line.strip() for line in f)
53 if item and not item.startswith('#')}
56 def _check_table(header, alignment_dict, data):
58 General method to generate table
59 :param header: header of the table
60 :param alignment_dict: dictionary with alignment for columns
61 :param data: iterable of rows of table
62 :return: table formatted data
64 table = prettytable.PrettyTable(header)
66 for k, v in alignment_dict.items():
69 for row in sorted(data):
77 Download resources from lists
82 def _is_missing(self, item):
84 Check if item is not downloaded
90 Check for missing data (not downloaded)
91 :return: dictionary of missing items
93 self._missing = {item: dst for item, dst in self._data_list.items() if
94 self._is_missing(item)}
97 def _log_existing(self):
99 Log items that are already downloaded.
101 for item in self._merged_lists():
102 if item not in self._missing:
103 if type(self).__name__ == 'DockerDownloader':
104 log.info('Docker image present: {}'.format(item))
106 log.info('File or directory present: {}'.format(item))
108 def _merged_lists(self):
110 Get all item names in one set
111 :return: set with all items
113 return set(self._data_list.keys())
115 def _initial_log(self):
117 Log initial info for download.
118 :return: True if download is necessary False if everything is already downloaded
121 items_left = len(self._missing)
122 class_name = type(self).__name__
124 log.info('{}: Everything seems to be present no download necessary.'.format(class_name))
126 log.info('{}: Initializing download {} {} are not present.'.format(class_name, items_left,