blob: 64403300f8a54dd5a0574515d2c404247076bbd6 [file] [log] [blame]
Milan Verespej2e1328a2019-06-18 13:40:08 +02001#! /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
22import logging
23from abc import ABC, abstractmethod
24
25import prettytable
26
27log = logging.getLogger(__name__)
28
29
30class AbstractDownloader(ABC):
31
32 def __init__(self, list_type, *list_args):
33 self._list_type = list_type
34 self._data_list = {item: list_arg[1] for list_arg in list_args
35 for item in self._load_list(list_arg[0])}
36 self._missing = self.missing()
37
38 @property
39 def list_type(self):
40 """
41 Type of resource in list
42 """
43 return self._list_type
44
45 @staticmethod
46 def _load_list(path):
47 """
48 Load list from file.
49 :param path: path to file
50 :return: set of items in list
51 """
52 with open(path, 'r') as f:
53 return {item for item in (line.strip() for line in f)
54 if item and not item.startswith('#')}
55
56 @staticmethod
57 def _check_table(header, alignment_dict, data):
58 """
59 General method to generate table
60 :param header: header of the table
61 :param alignment_dict: dictionary with alignment for columns
62 :param data: iterable of rows of table
63 :return: table formatted data
64 """
65 table = prettytable.PrettyTable(header)
66
67 for k, v in alignment_dict.items():
68 table.align[k] = v
69
70 for row in sorted(data):
71 table.add_row(row)
72
73 return table
74
75 @abstractmethod
76 def download(self):
77 """
78 Download resources from lists
79 """
80 pass
81
82 @abstractmethod
83 def _is_missing(self, item):
84 """
85 Check if item is not downloaded
86 """
87 pass
88
89 def missing(self):
90 """
91 Check for missing data (not downloaded)
92 :return: dictionary of missing items
93 """
94 self._missing = {item: dst for item, dst in self._data_list.items() if
95 self._is_missing(item)}
96 return self._missing
97
98 def _log_existing(self):
99 """
100 Log items that are already downloaded.
101 """
102 for item in self._merged_lists():
103 if item not in self._missing:
Milan Verespejda9e6b92019-06-18 15:09:41 +0200104 if type(self).__name__ == 'DockerDownloader':
105 log.info('Docker image present: {}'.format(item))
106 else:
107 log.info('File or directory present: {}'.format(item))
Milan Verespej2e1328a2019-06-18 13:40:08 +0200108
109 def _merged_lists(self):
110 """
111 Get all item names in one set
112 :return: set with all items
113 """
114 return set(self._data_list.keys())
115
116 def _initial_log(self):
117 """
118 Log initial info for download.
119 :return: True if download is necessary False if everything is already downloaded
120 """
121 self._log_existing()
122 items_left = len(self._missing)
123 class_name = type(self).__name__
124 if items_left == 0:
125 log.info('{}: Everything seems to be present no download necessary.'.format(class_name))
126 return False
127 log.info('{}: Initializing download {} {} are not present.'.format(class_name, items_left,
128 self._list_type))
129 return True