summaryrefslogtreecommitdiffstats
path: root/build/download/pypi_downloader.py
blob: 4ab6b1f4ae017b64c2b43a666deeddda0b72ad42 (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
#! /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 os
import subprocess
import sys
import timeit

from retrying import retry

from command_downloader import CommandDownloader

log = logging.getLogger(name=__name__)


class PyPiDownloader(CommandDownloader):
    def __init__(self, *list_args):
        super().__init__('pypi packages', 'pip', *list_args)

    @property
    def check_table(self):
        """
        Return check table for pypi packages
        :return: '' not implemented
        """
        log.warning('Check mode for pypi is not implemented.')
        return ''

    def _is_missing(self, item):
        """
        Check if item is missing
        :param item: item to check
        :return: True since don't know the actual filename
        """
        # always true don't know the name
        return True

    @retry(stop_max_attempt_number=5, wait_fixed=5000)
    def _download_item(self, item):
        """
        Download pip package using pip
        :param item: tuple(package_name, dst_dir) (name possibly with version specification)
        """
        package_name, dst_dir = item
        command = 'pip download --dest {} {}'.format(dst_dir, package_name)
        log.info('Running: {}'.format(command))
        log.info(
            subprocess.check_output(command.split(), stderr=subprocess.STDOUT).decode())
        log.info('Downloaded: {}'.format(package_name))


def run_cli():
    """
    Run as cli tool
    """
    parser = argparse.ArgumentParser(description='Download git repositories from list')
    parser.add_argument('pypi_list', metavar='pypi-list',
                        help='File with list of pypi packages to download.')
    parser.add_argument('--output-dir', '-o', default=os.getcwd(),
                        help='Download destination')

    args = parser.parse_args()

    logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s')

    downloader = PyPiDownloader([args.pypi_list, args.output_dir])

    timer_start = timeit.default_timer()
    try:
        downloader.download()
    except RuntimeError as err:
        log.exception(err)
        sys.exit(1)
    finally:
        log.info('Downloading finished in {}'.format(
            datetime.timedelta(seconds=timeit.default_timer() - timer_start)))


if __name__ == '__main__':
    run_cli()