blob: aff01b8086e2ac0b4cf6807950fc94174d850b46 [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
24import logging
25import sys
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 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
44def 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)
Milan Verespej455be472019-05-23 14:21:19 +020048 raise RuntimeError('git missing')
Milan Verespej83033592019-05-20 09:44:23 +020049
Milan Verespej455be472019-05-23 14:21:19 +020050 git_set = {tuple(item.split()) for item in base.load_list(git_list)}
Milan Verespej83033592019-05-20 09:44:23 +020051
52 error_count = 0
53
54 base.start_progress(progress, len(git_set), [], log)
55
56 for repo in git_set:
57 dst = '{}/{}'.format(dst_dir, repo[0])
58 if os.path.isdir(dst):
59 log.warning('Directory {} already exists. Repo probably present'.format(dst))
60 progress.update(progress.value + 1)
61 continue
62 try:
63 clone_repo(dst, *repo)
64 progress.update(progress.value + 1)
65 except subprocess.CalledProcessError as err:
Milan Verespej455be472019-05-23 14:21:19 +020066 log.exception(err.output.decode())
Milan Verespej83033592019-05-20 09:44:23 +020067 error_count += 1
68
69 base.finish_progress(progress, error_count, log)
70 if error_count > 0:
71 log.error('{} were not downloaded. Check logs for details'.format(error_count))
Milan Verespej455be472019-05-23 14:21:19 +020072 raise RuntimeError('Download unsuccesfull')
Milan Verespej83033592019-05-20 09:44:23 +020073
74def run_cli():
75 parser = argparse.ArgumentParser(description='Download git repositories from list')
76 parser.add_argument('git_list', metavar='git-list',
77 help='File with list of npm packages to download.')
78 parser.add_argument('--output-dir', '-o', default=os.getcwd(),
79 help='Download destination')
80
81 args = parser.parse_args()
82
83 logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(message)s')
84
85 progress = base.init_progress('git repositories')
Milan Verespej455be472019-05-23 14:21:19 +020086 try:
87 download(args.git_list, args.output_dir, progress)
88 except RuntimeError as err:
89 log.exception(err)
90 sys.exit(1)
Milan Verespej83033592019-05-20 09:44:23 +020091
92
93if __name__ == '__main__':
94 run_cli()