Milan Verespej | 994f60e | 2019-06-18 16:30:01 +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 datetime |
| 24 | import logging |
| 25 | import os |
| 26 | import subprocess |
| 27 | import sys |
| 28 | import timeit |
| 29 | |
| 30 | from retrying import retry |
| 31 | |
| 32 | from command_downloader import CommandDownloader |
| 33 | |
| 34 | log = logging.getLogger(name=__name__) |
| 35 | |
| 36 | |
| 37 | class PyPiDownloader(CommandDownloader): |
| 38 | def __init__(self, *list_args): |
| 39 | super().__init__('pypi packages', 'pip', *list_args) |
| 40 | |
| 41 | @property |
| 42 | def check_table(self): |
| 43 | """ |
| 44 | Return check table for pypi packages |
| 45 | :return: '' not implemented |
| 46 | """ |
| 47 | log.warning('Check mode for pypi is not implemented.') |
| 48 | return '' |
| 49 | |
| 50 | def _is_missing(self, item): |
| 51 | """ |
| 52 | Check if item is missing |
| 53 | :param item: item to check |
| 54 | :return: True since don't know the actual filename |
| 55 | """ |
| 56 | # always true don't know the name |
| 57 | return True |
| 58 | |
| 59 | @retry(stop_max_attempt_number=5, wait_fixed=5000) |
| 60 | def _download_item(self, item): |
| 61 | """ |
| 62 | Download pip package using pip |
| 63 | :param item: tuple(package_name, dst_dir) (name possibly with version specification) |
| 64 | """ |
| 65 | package_name, dst_dir = item |
| 66 | command = 'pip download --dest {} {}'.format(dst_dir, package_name) |
| 67 | log.info('Running: {}'.format(command)) |
| 68 | log.info( |
| 69 | subprocess.check_output(command.split(), stderr=subprocess.STDOUT).decode()) |
| 70 | log.info('Downloaded: {}'.format(package_name)) |
| 71 | |
| 72 | |
| 73 | def run_cli(): |
| 74 | """ |
| 75 | Run as cli tool |
| 76 | """ |
| 77 | parser = argparse.ArgumentParser(description='Download git repositories from list') |
| 78 | parser.add_argument('pypi_list', metavar='pypi-list', |
| 79 | help='File with list of pypi packages to download.') |
| 80 | parser.add_argument('--output-dir', '-o', default=os.getcwd(), |
| 81 | help='Download destination') |
| 82 | |
| 83 | args = parser.parse_args() |
| 84 | |
| 85 | logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s') |
| 86 | |
| 87 | downloader = PyPiDownloader([args.pypi_list, args.output_dir]) |
| 88 | |
| 89 | timer_start = timeit.default_timer() |
| 90 | try: |
| 91 | downloader.download() |
| 92 | except RuntimeError as err: |
| 93 | log.exception(err) |
| 94 | sys.exit(1) |
| 95 | finally: |
| 96 | log.info('Downloading finished in {}'.format( |
| 97 | datetime.timedelta(seconds=timeit.default_timer() - timer_start))) |
| 98 | |
| 99 | |
| 100 | if __name__ == '__main__': |
| 101 | run_cli() |