1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
4 # COPYRIGHT NOTICE STARTS HERE
6 # Copyright 2019 © Samsung Electronics Co., Ltd.
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 # COPYRIGHT NOTICE ENDS HERE
29 from collections import defaultdict
31 from command_downloader import CommandDownloader
33 log = logging.getLogger(name=__name__)
36 class RpmDownloader(CommandDownloader):
37 def __init__(self, *list_args):
38 super().__init__('rpm packages', 'yumdownloader', *list_args)
39 # beneficial to have it in same format
42 def check_table(self):
44 Return check table for rpm packages
45 :return: '' not implemented
47 log.warning('Check mode for rpms is not implemented.')
51 def _download_rpm_set(dst, rpms):
52 command = 'yumdownloader --destdir={} {}'.format(dst, ' '.join(rpms))
53 log.info('Running command: {}'.format(command))
55 subprocess.check_output(command.split(), stderr=subprocess.STDOUT).decode())
56 log.info('Downloaded: {}'.format(', '.join(sorted(rpms))))
60 Check for missing rpms (not downloaded)
61 :return: dictionary of missing items grouped by dst dir
63 # we need slightly different format for yumdownloader
64 self._missing = defaultdict(set)
65 for item, dst in self._data_list.items():
66 self._missing[dst].add(item)
69 def _is_missing(self, item): # pylint: disable=W0613
71 Check if item is missing
72 :param item: item to check
73 :return: it is always missing because not sure about downloaded filename
75 # don't know file names so always missing
78 def _initial_log(self):
80 Simpler then in parent
82 class_name = type(self).__name__
83 log.info('{}: Initializing download {} {} are not present.'.format(class_name, len(self._data_list),
88 Download rpm packages from lists
91 error_occurred = False
93 for dst, rpm_set in self._missing.items():
95 self._download_rpm_set(dst, rpm_set)
96 except subprocess.CalledProcessError as err:
97 log.exception(err.output)
100 log.error('Download failed')
101 raise RuntimeError('Download unsuccessful')
108 parser = argparse.ArgumentParser(description='Download rpm packages from list')
109 parser.add_argument('rpm_list', metavar='rpm-list',
110 help='File with list of npm packages to download.')
111 parser.add_argument('--output-dir', '-o', default=os.getcwd(),
112 help='Download destination')
114 args = parser.parse_args()
116 logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s')
118 timer_start = timeit.default_timer()
120 downloader = RpmDownloader([args.rpm_list, args.output_dir])
121 downloader.download()
125 log.info('Downloading finished in {}'.format(
126 datetime.timedelta(seconds=timeit.default_timer() - timer_start)))
129 if __name__ == '__main__':