-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.py
73 lines (59 loc) · 2.07 KB
/
utils.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
65
66
67
68
69
70
71
72
import logging
import os
import shutil
import yaml
import git
def parse_config(config_file, required_keys):
with open(config_file, 'r') as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
logging.error(exc)
for key in required_keys:
if key not in config:
logging.error("key %s not found in %s" % (key, config_file))
exit(1)
logging.debug("config %s pass" % config_file)
return config
def try_copy_tree(src, dst, force_delete):
logging.info("copying %s to %s" % (src, dst))
try:
shutil.copytree(src, dst)
except FileExistsError as e:
if force_delete:
logging.warning("deleting %s" % dst)
shutil.rmtree(dst)
shutil.copytree(src, dst)
else:
logging.error(e)
logging.info("use --force-delete to remove old directories")
exit(1)
def try_mkdir(dst, force_delete):
try:
os.mkdir(dst)
except FileExistsError as e:
if force_delete:
logging.warning("deleting %s" % dst)
shutil.rmtree(dst)
os.mkdir(dst)
else:
logging.error(e)
logging.info("use --force-delete to remove old directories")
exit(1)
def get_git_sha(repo_dir):
return git.Repo(repo_dir).head.object.hexsha
def clone_repo(repo_url, commit, repo_dir, force_delete):
if os.path.exists(repo_dir):
if force_delete:
logging.warning("deleting %s" % repo_dir)
shutil.rmtree(repo_dir)
else:
logging.warning("directory %s already exists - skipping. Use --force-delete to remove" % repo_dir)
return
logging.info("cloning %s" % repo_url)
repo = git.Repo.clone_from(repo_url, repo_dir)
logging.info("checking out to %s" % commit)
repo.git.checkout(commit)
logging.info("installing submodules")
# the submodule support for gitpython is broken, so use git (via repo) to do the work instead.
repo.git.submodule('update', '--init', '--recursive')