blob: 1d8c297904d2c4286eb9c3ec4b2502299646869b [file] [log] [blame]
Milan Verespej83033592019-05-20 09:44:23 +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 HEREE
21
22import argparse
23import subprocess
Milan Verespej301b83a2019-06-05 15:23:42 +020024import shutil
Milan Verespej83033592019-05-20 09:44:23 +020025import logging
26import sys
27import os
28from retrying import retry
29
30import base
31
32log = logging.getLogger(name=__name__)
33
34@retry(stop_max_attempt_number=5, wait_fixed=5000)
35def 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
45def 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 Verespej455be472019-05-23 14:21:19 +020049 raise RuntimeError('git missing')
Milan Verespej83033592019-05-20 09:44:23 +020050
Milan Verespej455be472019-05-23 14:21:19 +020051 git_set = {tuple(item.split()) for item in base.load_list(git_list)}
Milan Verespej83033592019-05-20 09:44:23 +020052
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 Verespej301b83a2019-06-05 15:23:42 +020067 if os.path.isdir(dst):
68 shutil.rmtree(dst)
Milan Verespej455be472019-05-23 14:21:19 +020069 log.exception(err.output.decode())
Milan Verespej83033592019-05-20 09:44:23 +020070 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 Verespej301b83a2019-06-05 15:23:42 +020075 raise RuntimeError('Download unsuccessful')
Milan Verespej83033592019-05-20 09:44:23 +020076
77def run_cli():
78 parser = argparse.ArgumentParser(description='Download git repositories from list')
79 parser.add_argument('git_list', metavar='git-list',
Milan Verespej301b83a2019-06-05 15:23:42 +020080 help='File with list of git repos to download.')
Milan Verespej83033592019-05-20 09:44:23 +020081 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 Verespej455be472019-05-23 14:21:19 +020089 try:
90 download(args.git_list, args.output_dir, progress)
91 except RuntimeError as err:
92 log.exception(err)
93 sys.exit(1)
Milan Verespej83033592019-05-20 09:44:23 +020094
95
96if __name__ == '__main__':
97 run_cli()