summaryrefslogtreecommitdiffstats
path: root/build/download/download.py
blob: 0af129893baa4551e04205e6b09735d48eb0c3f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#! /usr/bin/env python
# -*- coding: utf-8 -*-

#   COPYRIGHT NOTICE STARTS HERE

#   Copyright 2019 © Samsung Electronics Co., Ltd.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

#   COPYRIGHT NOTICE ENDS HERE

import argparse
import datetime
import logging
import sys
import timeit

import docker_downloader
import git_downloader
import http_downloader
import npm_downloader
import pypi_downloader
import rpm_downloader

log = logging.getLogger(name=__name__)


def parse_args():
    """
    Parse command line arguments
    :return: arguments
    """
    parser = argparse.ArgumentParser(description='Download data from lists')
    list_group = parser.add_argument_group()
    list_group.add_argument('--docker', action='append', nargs='+', default=[],
                            metavar=('list', 'dir-name'),
                            help='Docker type list. If second argument is specified '
                                 'it is treated as directory where images will be saved '
                                 'otherwise only pull operation is executed this can\'t '
                                 'be mixed between multiple docker list specifications. '
                                 'if one of the list does not have directory specified '
                                 'all lists are only pulled!!!')
    list_group.add_argument('--http', action='append', nargs=2, default=[],
                            metavar=('list', 'dir-name'),
                            help='Http type list and directory to save downloaded files')
    list_group.add_argument('--npm', action='append', nargs=2, default=[],
                            metavar=('list', 'dir-name'),
                            help='npm type list and directory to save downloaded files')
    list_group.add_argument('--rpm', action='append', nargs=2, default=[],
                            metavar=('list', 'dir-name'),
                            help='rpm type list and directory to save downloaded files')
    list_group.add_argument('--git', action='append', nargs=2, default=[],
                            metavar=('list', 'dir-name'),
                            help='git repo type list and directory to save downloaded files')
    list_group.add_argument('--pypi', action='append', nargs=2, default=[],
                            metavar=('list', 'dir-name'),
                            help='pypi packages type list and directory to save downloaded files')
    parser.add_argument('--npm-registry', default='https://registry.npmjs.org',
                        help='npm registry to use (default: https://registry.npmjs.org)')
    parser.add_argument('--check', '-c', action='store_true', default=False,
                        help='Check what is missing. No download.')
    parser.add_argument('--debug', action='store_true', default=False,
                        help='Turn on debug output')

    args = parser.parse_args()

    for arg in ('docker', 'npm', 'http', 'rpm', 'git', 'pypi'):
        if getattr(args, arg):
            return args

    parser.error('One of --docker, --npm, --http, --rpm, --git or --pypi must be specified')


def log_start(item_type):
    """
    Log starting message
    :param item_type: type of resources
    :return:
    """
    log.info('Starting download of {}.'.format(item_type))


def handle_download(downloader, check_mode, errorred_lists, start_time):
    """
    Handle download of resources
    :param downloader: downloader to use
    :param check_mode: run in check mode (boolean)
    :param errorred_lists: list of data types of failed lists
    :param start_time: timeit.default_timer() right before download
    :return: timeit.default_timer() at the end of download
    """
    if check_mode:
        print(downloader.check_table)
    else:
        log_start(downloader.list_type)
        try:
            downloader.download()
        except RuntimeError:
            errorred_lists.append(downloader.list_type)
    return log_time_interval(start_time, downloader.list_type)


def handle_command_download(downloader_class, check_mode, errorred_lists, start_time, *args):
    """
    Handle download of resources where shell command is used
    :param downloader_class: Class of command_downloader.CommandDownloader to use
    :param check_mode: run in check mode (boolean)
    :param errorred_lists: list of data types of failed lists
    :param start_time: timeit.default_timer() right before download
    :param args: arguments for downloader class initialization
    :return: timeit.default_timer() at the end of download
    """
    try:
        downloader = downloader_class(*args)
        return handle_download(downloader, check_mode, errorred_lists, start_time)
    except FileNotFoundError as err:
        classname = type(downloader_class).__name__
        log.exception('Error initializing: {}: {}'.format(classname, err))
    return timeit.default_timer()


def log_time_interval(start, resource_type=''):
    """
    Log how long the download took
    :param start: timeit.default_timer() when interval started
    :param resource_type: type of data that was downloaded. (empty string for whole download)
    :return: timeit.default_timer() after logging
    """
    e_time = datetime.timedelta(seconds=timeit.default_timer() - start)
    if resource_type:
        msg = 'Download of {} took {}\n'.format(resource_type, e_time)
    else:
        msg = 'Execution ended. Total elapsed time {}'.format(e_time)
    log.info(msg)
    return timeit.default_timer()


def run_cli():
    if sys.version_info.major < 3:
        log.error('Unfortunately Python 2 is not supported for data download.')
        sys.exit(1)
    args = parse_args()

    console_handler = logging.StreamHandler(sys.stdout)
    console_formatter = logging.Formatter('%(message)s')
    console_handler.setFormatter(console_formatter)
    now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    log_file = 'download_data-{}.log'.format(now)
    file_format = "%(asctime)s: %(filename)s: %(levelname)s: %(message)s"

    if args.debug:
        logging.basicConfig(level=logging.DEBUG, filename=log_file, format=file_format)
    else:
        logging.basicConfig(level=logging.INFO, filename=log_file, format=file_format)
    root_logger = logging.getLogger()
    root_logger.addHandler(console_handler)

    errorred_lists = []
    timer_start = interval_start = timeit.default_timer()

    if args.check:
        log.info('Check mode. No download will be executed.')

    if args.docker:
        save = True if len(list(filter(lambda x: len(x) == 2, args.docker))) == len(args.docker) else False
        docker = docker_downloader.DockerDownloader(save, *args.docker, workers=3)
        interval_start = handle_download(docker, args.check, errorred_lists, interval_start)

    if args.http:
        http = http_downloader.HttpDownloader(*args.http)
        interval_start = handle_download(http, args.check, errorred_lists, interval_start)

    if args.npm:
        npm = npm_downloader.NpmDownloader(args.npm_registry, *args.npm)
        interval_start = handle_download(npm, args.check, errorred_lists, interval_start)

    if args.rpm:
        interval_start = handle_command_download(rpm_downloader.RpmDownloader, args.check, errorred_lists,
                                                 interval_start, *args.rpm)

    if args.git:
        interval_start = handle_command_download(git_downloader.GitDownloader, args.check, errorred_lists,
                                                 interval_start, *args.git)

    if args.pypi:
        handle_command_download(pypi_downloader.PyPiDownloader, args.check, errorred_lists,
                                interval_start, *args.pypi)

    if not args.check:
        log_time_interval(timer_start)

    if errorred_lists:
        log.error('Errors encountered while processing these types:'
                  '\n{}'.format('\n'.join(errorred_lists)))
        sys.exit(1)


if __name__ == '__main__':
    run_cli()