blob: 951003c56f16feabe2047907bd7b604406cdec6e [file] [log] [blame]
Milan Verespejd85b2d72019-06-05 13:58:38 +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 subprocess
26import os
27from retrying import retry
28
29import base
30
31log = logging.getLogger(name=__name__)
32
33@retry(stop_max_attempt_number=5, wait_fixed=5000)
34def download_package(package_name, dst_dir):
35 command = 'pip download --dest {} {}'.format(dst_dir, package_name)
36 log.info('Running: {}'.format(command))
37 log.info(subprocess.check_output(command.split(), stderr=subprocess.STDOUT).decode())
38 log.info('Downloaded: {}'.format(package_name))
39
40
41def download(pypi_list, dst_dir, progress):
42 if not base.check_tool('pip'):
43 log.error('ERROR: pip is not installed')
44 progress.finish(dirty=True)
45 raise RuntimeError('pip missing')
46
47 pypi_set = base.load_list(pypi_list)
48
49 error_count = 0
50
51 base.start_progress(progress, len(pypi_set), [], log)
52
53 for package in pypi_set:
54 try:
55 download_package(package, dst_dir)
56 except subprocess.CalledProcessError as err:
57 log.exception(err.output.decode())
58 error_count += 1
59
60 progress.update(progress.value + 1)
61
62 base.finish_progress(progress, error_count, log)
63 if error_count > 0:
64 log.error('{} packages were not downloaded. Check logs for details'.format(error_count))
65 raise RuntimeError('Download unsuccesfull')
66
67
68def run_cli():
69 parser = argparse.ArgumentParser(description='Download git repositories from list')
70 parser.add_argument('pypi_list', metavar='pypi-list',
71 help='File with list of pypi packages to download.')
72 parser.add_argument('--output-dir', '-o', default=os.getcwd(),
73 help='Download destination')
74
75 args = parser.parse_args()
76
77 logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s')
78
79 progress = base.init_progress('pypi packages')
80 try:
81 download(args.pypi_list, args.output_dir, progress)
82 except RuntimeError as err:
83 log.exception(err)
84 sys.exit(1)
85
86
87if __name__ == '__main__':
88 run_cli()