Merge "Add base download script"
[oom/offline-installer.git] / build / download / rpm_packages.py
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 argparse
23 import subprocess
24 import logging
25 import sys
26 import os
27
28 import base
29
30 log = logging.getLogger(name=__name__)
31
32
33 def download(rpm_list, dst_dir):
34     if not base.check_tool('yumdownloader'):
35         log.error('ERROR: yumdownloader is not installed')
36         raise RuntimeError('yumdownloader missing')
37
38     rpm_set = base.load_list(rpm_list)
39
40     command = 'yumdownloader --destdir={} {}'.format(dst_dir, ' '.join(rpm_set))
41     log.info('Running command: {}'.format(command))
42     try:
43         subprocess.check_call(command.split())
44     except subprocess.CalledProcessError as err:
45         log.exception(err.output)
46         raise err
47     log.info('Downloaded')
48
49
50 def run_cli():
51     parser = argparse.ArgumentParser(description='Download rpm packages from list')
52     parser.add_argument('rpm_list', metavar='rpm-list',
53                         help='File with list of npm packages to download.')
54     parser.add_argument('--output-dir', '-o', default=os.getcwd(),
55                         help='Download destination')
56
57     args = parser.parse_args()
58
59     logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s')
60
61     try:
62         download(args.rpm_list, args.output_dir)
63     except (subprocess.CalledProcessError, RuntimeError):
64         sys.exit(1)
65
66
67
68 if __name__ == '__main__':
69     run_cli()