Milan Verespej | 8303359 | 2019-05-20 09:44:23 +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 HEREE |
| 21 | |
| 22 | import argparse |
| 23 | import subprocess |
| 24 | import logging |
| 25 | import sys |
| 26 | import os |
| 27 | from retrying import retry |
| 28 | |
| 29 | import base |
| 30 | |
| 31 | log = logging.getLogger(name=__name__) |
| 32 | |
| 33 | @retry(stop_max_attempt_number=5, wait_fixed=5000) |
| 34 | def clone_repo(dst, repo, branch=None): |
| 35 | if branch: |
| 36 | command = 'git clone -b {} --single-branch https://{} --bare {}'.format(branch, repo, dst) |
| 37 | else: |
| 38 | command = 'git clone https://{} --bare {}'.format(repo, dst) |
| 39 | log.info('Running: {}'.format(command)) |
| 40 | log.info(subprocess.check_output(command.split(), stderr=subprocess.STDOUT).decode()) |
| 41 | log.info('Downloaded: {}'.format(repo)) |
| 42 | |
| 43 | |
| 44 | def download(git_list, dst_dir, progress): |
| 45 | if not base.check_tool('git'): |
| 46 | log.error('ERROR: git is not installed') |
| 47 | progress.finish(dirty=True) |
| 48 | return 1 |
| 49 | |
| 50 | git_set = {tuple(item.split()) for item in base.load_list(git_list) |
| 51 | if not item.startswith('#')} |
| 52 | |
| 53 | error_count = 0 |
| 54 | |
| 55 | base.start_progress(progress, len(git_set), [], log) |
| 56 | |
| 57 | for repo in git_set: |
| 58 | dst = '{}/{}'.format(dst_dir, repo[0]) |
| 59 | if os.path.isdir(dst): |
| 60 | log.warning('Directory {} already exists. Repo probably present'.format(dst)) |
| 61 | progress.update(progress.value + 1) |
| 62 | continue |
| 63 | try: |
| 64 | clone_repo(dst, *repo) |
| 65 | progress.update(progress.value + 1) |
| 66 | except subprocess.CalledProcessError as err: |
| 67 | log.error(err.output.decode()) |
| 68 | error_count += 1 |
| 69 | |
| 70 | base.finish_progress(progress, error_count, log) |
| 71 | if error_count > 0: |
| 72 | log.error('{} were not downloaded. Check logs for details'.format(error_count)) |
| 73 | return error_count |
| 74 | |
| 75 | |
| 76 | def run_cli(): |
| 77 | parser = argparse.ArgumentParser(description='Download git repositories from list') |
| 78 | parser.add_argument('git_list', metavar='git-list', |
| 79 | help='File with list of npm 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 | progress = base.init_progress('git repositories') |
| 88 | |
| 89 | sys.exit(download(args.git_list, args.output_dir, progress)) |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | run_cli() |