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 |
Milan Verespej | 301b83a | 2019-06-05 15:23:42 +0200 | [diff] [blame] | 24 | import shutil |
Milan Verespej | 8303359 | 2019-05-20 09:44:23 +0200 | [diff] [blame] | 25 | import logging |
| 26 | import sys |
| 27 | import os |
| 28 | from retrying import retry |
| 29 | |
| 30 | import base |
| 31 | |
| 32 | log = logging.getLogger(name=__name__) |
| 33 | |
| 34 | @retry(stop_max_attempt_number=5, wait_fixed=5000) |
| 35 | def clone_repo(dst, repo, branch=None): |
| 36 | if branch: |
| 37 | command = 'git clone -b {} --single-branch https://{} --bare {}'.format(branch, repo, dst) |
| 38 | else: |
| 39 | command = 'git clone https://{} --bare {}'.format(repo, dst) |
| 40 | log.info('Running: {}'.format(command)) |
| 41 | log.info(subprocess.check_output(command.split(), stderr=subprocess.STDOUT).decode()) |
| 42 | log.info('Downloaded: {}'.format(repo)) |
| 43 | |
| 44 | |
| 45 | def download(git_list, dst_dir, progress): |
| 46 | if not base.check_tool('git'): |
| 47 | log.error('ERROR: git is not installed') |
| 48 | progress.finish(dirty=True) |
Milan Verespej | 455be47 | 2019-05-23 14:21:19 +0200 | [diff] [blame] | 49 | raise RuntimeError('git missing') |
Milan Verespej | 8303359 | 2019-05-20 09:44:23 +0200 | [diff] [blame] | 50 | |
Milan Verespej | 455be47 | 2019-05-23 14:21:19 +0200 | [diff] [blame] | 51 | git_set = {tuple(item.split()) for item in base.load_list(git_list)} |
Milan Verespej | 8303359 | 2019-05-20 09:44:23 +0200 | [diff] [blame] | 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: |
Milan Verespej | 301b83a | 2019-06-05 15:23:42 +0200 | [diff] [blame] | 67 | if os.path.isdir(dst): |
| 68 | shutil.rmtree(dst) |
Milan Verespej | 455be47 | 2019-05-23 14:21:19 +0200 | [diff] [blame] | 69 | log.exception(err.output.decode()) |
Milan Verespej | 8303359 | 2019-05-20 09:44:23 +0200 | [diff] [blame] | 70 | error_count += 1 |
| 71 | |
| 72 | base.finish_progress(progress, error_count, log) |
| 73 | if error_count > 0: |
| 74 | log.error('{} were not downloaded. Check logs for details'.format(error_count)) |
Milan Verespej | 301b83a | 2019-06-05 15:23:42 +0200 | [diff] [blame] | 75 | raise RuntimeError('Download unsuccessful') |
Milan Verespej | 8303359 | 2019-05-20 09:44:23 +0200 | [diff] [blame] | 76 | |
| 77 | def run_cli(): |
| 78 | parser = argparse.ArgumentParser(description='Download git repositories from list') |
| 79 | parser.add_argument('git_list', metavar='git-list', |
Milan Verespej | 301b83a | 2019-06-05 15:23:42 +0200 | [diff] [blame] | 80 | help='File with list of git repos to download.') |
Milan Verespej | 8303359 | 2019-05-20 09:44:23 +0200 | [diff] [blame] | 81 | parser.add_argument('--output-dir', '-o', default=os.getcwd(), |
| 82 | help='Download destination') |
| 83 | |
| 84 | args = parser.parse_args() |
| 85 | |
| 86 | logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s') |
| 87 | |
| 88 | progress = base.init_progress('git repositories') |
Milan Verespej | 455be47 | 2019-05-23 14:21:19 +0200 | [diff] [blame] | 89 | try: |
| 90 | download(args.git_list, args.output_dir, progress) |
| 91 | except RuntimeError as err: |
| 92 | log.exception(err) |
| 93 | sys.exit(1) |
Milan Verespej | 8303359 | 2019-05-20 09:44:23 +0200 | [diff] [blame] | 94 | |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | run_cli() |