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