-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_git_clone.py
64 lines (50 loc) · 1.81 KB
/
run_git_clone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'''git clone a repo, or hub clone a repo (if "use_hub": true is set in config),
using remote repo URL defined in config and local folder to check out to
(also defined in config)
Authors:
Carl Anderson ([email protected])
'''
import argparse
import logging
import os
import json
import subprocess
def parse_arguments():
"""Parse command line arguments
Returns:
argument objects with flags as attributes
"""
parser = argparse.ArgumentParser()
parser.add_argument('--config',
help='Location of the configuration file',
required=True)
known_args, pipeline_args = parser.parse_known_args()
return known_args, pipeline_args
def main():
"""
do a git clone based on the url and folder in the config
"""
logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s %(funcName)s: %(message)s', level=logging.INFO)
args, _ = parse_arguments()
if os.path.exists(args.config):
with open(args.config, 'r') as f:
config = json.load(f)
else:
raise Exception('config file at: {} not found'.format(args.config))
url = config['git']['url']
folder = config['git']['folder']
cmd = ["git", "clone", url, folder]
if 'use_hub' in config and config['use_hub']:
logging.info("Using hub instead of git")
cmd = ["hub", "clone", url, folder]
logging.info("About to run %s", " ".join(cmd))
try:
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT, shell=False, timeout=3,
universal_newlines=True)
except subprocess.CalledProcessError as exc:
logging.error("%s %s" % (exc.returncode, exc.output))
else:
logging.info("Output: %s", output)
if __name__ == '__main__':
main()