Milan Verespej | 455be47 | 2019-05-23 14:21:19 +0200 | [diff] [blame] | 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 logging |
| 24 | import sys |
| 25 | import datetime |
| 26 | import timeit |
| 27 | |
| 28 | import base |
| 29 | import docker_images |
| 30 | import git_repos |
| 31 | import http_files |
| 32 | import npm_packages |
| 33 | import rpm_packages |
| 34 | |
| 35 | log = logging.getLogger(name=__name__) |
| 36 | |
| 37 | def parse_args(): |
| 38 | parser=argparse.ArgumentParser(description='Download data from lists') |
| 39 | list_group = parser.add_argument_group() |
| 40 | list_group.add_argument('--docker', action='append', nargs='+', default=[], |
| 41 | metavar=('list', 'dir-name'), |
| 42 | help='Docker type list. If second argument is specified ' |
| 43 | 'it is treated as directory where images will be saved ' |
| 44 | 'otherwise only pull operation is executed') |
| 45 | list_group.add_argument('--http', action='append', nargs=2, default=[], |
| 46 | metavar=('list', 'dir-name'), |
| 47 | help='Http type list and directory to save downloaded files') |
| 48 | list_group.add_argument('--npm', action='append', nargs=2, default=[], |
| 49 | metavar=('list', 'dir-name'), |
| 50 | help='npm type list and directory to save downloaded files') |
| 51 | list_group.add_argument('--rpm', action='append', nargs=2, default=[], |
| 52 | metavar=('list', 'dir-name'), |
| 53 | help='rpm type list and directory to save downloaded files') |
| 54 | list_group.add_argument('--git', action='append', nargs=2, default=[], |
| 55 | metavar=('list', 'dir-name'), |
| 56 | help='git repo type list and directory to save downloaded files') |
| 57 | parser.add_argument('--npm-registry', default='https://registry.npmjs.org', |
| 58 | help='npm registry to use (default: https://registry.npmjs.org)') |
| 59 | parser.add_argument('--check', '-c', action='store_true', default=False, |
| 60 | help='Check what is missing. No download.') |
| 61 | parser.add_argument('--debug', action='store_true', default=False, |
| 62 | help='Turn on debug output') |
| 63 | |
| 64 | args = parser.parse_args() |
| 65 | |
| 66 | for arg in ('docker', 'npm', 'http', 'rpm', 'git'): |
| 67 | if getattr(args, arg): |
| 68 | return args |
| 69 | |
| 70 | parser.error('One of --docker, --npm, --http, --rpm, --git must be specified') |
| 71 | |
| 72 | |
| 73 | def run_cli(): |
| 74 | args = parse_args() |
| 75 | |
| 76 | console_handler = logging.StreamHandler(sys.stdout) |
| 77 | console_formatter = logging.Formatter('%(message)s') |
| 78 | console_handler.setFormatter(console_formatter) |
| 79 | now = datetime.datetime.now().strftime('%Y%m%d%H%M%S') |
| 80 | log_file = 'download_data-{}.log'.format(now) |
| 81 | file_format = "%(asctime)s: %(filename)s: %(levelname)s: %(message)s" |
| 82 | |
| 83 | if args.debug: |
| 84 | logging.basicConfig(level=logging.DEBUG, filename=log_file, format=file_format) |
| 85 | else: |
| 86 | logging.basicConfig(level=logging.INFO, filename=log_file, format=file_format) |
| 87 | root_logger = logging.getLogger() |
| 88 | root_logger.addHandler(console_handler) |
| 89 | |
| 90 | list_with_errors = [] |
| 91 | timer_start = timeit.default_timer() |
| 92 | |
| 93 | for docker_list in args.docker: |
| 94 | log.info('Processing {}.'.format(docker_list[0])) |
| 95 | progress = None if args.check else base.init_progress('docker images') |
| 96 | save = False |
| 97 | if len(docker_list) > 1: |
| 98 | save = True |
| 99 | else: |
| 100 | docker_list.append(None) |
| 101 | try: |
| 102 | docker_images.download(docker_list[0], save, |
| 103 | docker_list[1], args.check, progress) |
| 104 | except RuntimeError: |
| 105 | list_with_errors.append(docker_list[0]) |
| 106 | |
| 107 | for http_list in args.http: |
| 108 | progress = None if args.check else base.init_progress('http files') |
| 109 | log.info('Processing {}.'.format(http_list[0])) |
| 110 | try: |
| 111 | http_files.download(http_list[0], http_list[1], args.check, |
| 112 | progress) |
| 113 | except RuntimeError: |
| 114 | list_with_errors.append(http_list[0]) |
| 115 | |
| 116 | for npm_list in args.npm: |
| 117 | progress = None if args.check else base.init_progress('npm packages') |
| 118 | log.info('Processing {}.'.format(npm_list[0])) |
| 119 | try: |
| 120 | npm_packages.download(npm_list[0], args.npm_registry, npm_list[1], |
| 121 | args.check, progress) |
| 122 | except RuntimeError: |
| 123 | list_with_errors.append(npm_list[0]) |
| 124 | |
| 125 | for rpm_list in args.rpm: |
| 126 | if args.check: |
| 127 | log.info('Check mode for rpm packages is not implemented') |
| 128 | break |
| 129 | log.info('Processing {}.'.format(rpm_list[0])) |
| 130 | try: |
| 131 | rpm_packages.download(rpm_list[0], rpm_list[1]) |
| 132 | except RuntimeError: |
| 133 | list_with_errors.append(rpm_list[0]) |
| 134 | |
| 135 | for git_list in args.git: |
| 136 | if args.check: |
| 137 | log.info('Check mode for git repositories is not implemented') |
| 138 | break |
| 139 | progress = None if args.check else base.init_progress('git repositories') |
| 140 | log.info('Processing {}.'.format(git_list[0])) |
| 141 | try: |
| 142 | git_repos.download(git_list[0], git_list[1], progress) |
| 143 | except RuntimeError: |
| 144 | list_with_errors.append(git_list[0]) |
| 145 | |
| 146 | e_time = datetime.timedelta(seconds=timeit.default_timer() - timer_start) |
| 147 | log.info(timeit.default_timer() - timer_start) |
| 148 | log.info('Execution ended. Total elapsed time {}'.format(e_time)) |
| 149 | |
| 150 | if list_with_errors: |
| 151 | log.error('Errors encountered while processing these lists:' |
| 152 | '\n{}'.format('\n'.join(list_with_errors))) |
| 153 | sys.exit(1) |
| 154 | |
| 155 | |
| 156 | |
| 157 | if __name__ == '__main__': |
| 158 | run_cli() |