forked from xmos/lib_tflite_micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_dependencies.py
74 lines (62 loc) · 1.95 KB
/
fetch_dependencies.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
73
74
#!/usr/bin/env python
# Copyright 2021 XMOS LIMITED.
# This Software is subject to the terms of the XMOS Public Licence: Version 1.
import os
import shutil
import subprocess as sp
def remove_read_only(func, path, exc_info):
"""Sometimes, Windows complains when removing .git folders"""
import stat
if not os.access(path, os.W_OK):
# Is the error an access error ?
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise exc_info
def read_repo_list():
"""Return a list of lists: [dir, url, ref]"""
repos = []
with open("repos.list") as f:
lines = f.readlines()
for line in lines:
repos.append(line.split())
return repos
base_dir = os.path.join(os.getcwd(), "..")
repos = read_repo_list()
for name, url, ref in repos:
print("\nUpdating " + name + "...")
repo_dir = os.path.join(base_dir, name)
if os.path.isdir(repo_dir):
# check whether it has the correct URL
old_url = (
sp.check_output("git config --get remote.origin.url".split(), cwd=repo_dir)
.decode("utf-8")
.strip()
)
if url == old_url:
print("URL hasn't changed")
else:
print("URL for " + name + " has changed.")
print(" Old: " + old_url)
print(" New: " + url)
print("Deleting repository.")
shutil.rmtree(repo_dir, onerror=remove_read_only)
# Clone
if not os.path.isdir(repo_dir):
sp.check_call(
"git clone {} {}".format(url, name).split(),
cwd=base_dir,
stdout=sp.PIPE,
stderr=sp.PIPE,
)
# Fetch
print("Fetching...")
sp.check_call("git fetch".split(), cwd=repo_dir, stdout=sp.PIPE, stderr=sp.PIPE)
# Checkout
print("Checking out " + ref + "...")
sp.check_call(
"git checkout {}".format(ref).split(),
cwd=repo_dir,
stdout=sp.PIPE,
stderr=sp.PIPE,
)