Merge "[GENERAL] Add Andreas Geissler as committer."
[oom/offline-installer.git] / build / download / command_downloader.py
1 # -*- coding: utf-8 -*-
2
3 #   COPYRIGHT NOTICE STARTS HERE
4
5 #   Copyright 2019 © Samsung Electronics Co., Ltd.
6 #
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
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18
19 #   COPYRIGHT NOTICE ENDS HERE
20
21 import logging
22 import subprocess
23 from abc import ABC
24 from distutils.spawn import find_executable
25
26 from downloader import AbstractDownloader
27
28 log = logging.getLogger(__name__)
29
30
31 class CommandDownloader(AbstractDownloader, ABC):
32     def __init__(self, list_type, cli_tool, *list_args):
33         super().__init__(list_type, *list_args)
34         if not find_executable(cli_tool):
35             raise FileNotFoundError(cli_tool)
36
37     def download(self):
38         """
39         Download items from list
40         """
41         if not self._initial_log():
42             return
43         items_left = len(self._missing)
44         error_occurred = False
45         for item, dst_dir in self._data_list.items():
46             try:
47                 self._download_item((item, dst_dir))
48             except subprocess.CalledProcessError as err:
49                 log.exception(err.output.decode())
50                 error_occurred = True
51             items_left -= 1
52             log.info('{} {} left to download.'.format(items_left, self._list_type))
53         if error_occurred:
54             log.error('{} {} were not downloaded.'.format(items_left, self._list_type))
55             raise RuntimeError('One or more errors occurred')
56
57     def _download_item(self, item):
58         pass