-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemerge-gitclone
executable file
·132 lines (107 loc) · 4.3 KB
/
emerge-gitclone
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python3
# Copyright The Flatcar Authors.
# SPDX-License-Identifier: Apache-2.0
import os
import shutil
import subprocess
import sys
import re
import portage
GIT_URI = "https://github.com/flatcar/{repo}.git"
GIT_LOCATION = "/var/lib/portage/{repo}"
def get_release():
"""Return the version of the currently running release."""
release = False
with open("/usr/share/flatcar/release") as release_file:
for line in release_file:
if line.startswith("FLATCAR_RELEASE_VERSION="):
release = line.split("=", 1)[1].strip().replace("+", "-", 1)
return release
def get_channel():
"""Return the channel for the current deployment"""
channel = None
with open("/usr/share/flatcar/update.conf") as update_conf:
for line in update_conf:
if line.startswith("GROUP"):
channel = line.split("=", 1)[1].strip()
if channel == 'developer':
channel = 'main'
return channel
def git_clone_scripts(repo_url, repo_location, ref):
"""Clone the scripts repo at the given location, and handle submodules (if applicable)"""
print(f">>> Starting git clone in {repo_location}")
os.umask(0o022)
subprocess.check_call(
["git", "clone", repo_url, repo_location]
)
# Check if this ref uses submodules and check these out if it does.
subprocess.check_output(
["git", "-C", repo_location, "checkout", ref]
)
try:
with open(repo_location + '/.gitmodules') as gm:
gmc = gm.read()
if '[submodule ' in gmc:
print(f">>> Submodules detected in {ref}.")
subprocess.check_output(
["git", "-C", repo_location, "submodule", "init"]
)
subprocess.check_output(
["git", "-C", repo_location, "submodule", "update"]
)
else:
print(f">>> Empty or invalid submodule config detected in {ref}")
print(f">>> at {repo_location}/.gitmodules. Ignoring and continuing w/o submodules.")
except (FileNotFoundError, IOError):
print(f">>> No submodules detected in {ref}. Assuming unified scripts repo.")
print(f">>> Git clone in {repo_location} successful")
def sync_repo():
release = get_release()
channel = get_channel()
ref = f"{channel}-{release}"
# Check if release ends with a git ref. If it does we're dealing with
# a dev build which does not necessarily have a tag on 'scripts'.
# These version strings look like e.g. "3552.0.0+nightly-20230323-2100-4-g5d72f4ee",
# with "-g<checksum>" being a git ref.
gitref_pat = r'-g([0-9A-Fa-f]+)$'
ref_match = re.search(gitref_pat, release)
if ref_match:
git_ref = ref_match.group(1)
print(f">>> Gitref detected in version {ref}, will check out {git_ref}.")
ref = git_ref
repo_list = ["scripts", "coreos-overlay", "portage-stable"]
for repo in repo_list:
if os.path.isdir(GIT_LOCATION.format(repo=repo)) and not os.path.islink(
GIT_LOCATION.format(repo=repo)
):
shutil.rmtree(GIT_LOCATION.format(repo=repo))
if os.path.lexists(GIT_LOCATION.format(repo=repo)):
os.unlink(GIT_LOCATION.format(repo=repo))
if repo == "scripts":
git_clone_scripts(
repo_url=GIT_URI.format(repo=repo),
repo_location=GIT_LOCATION.format(repo=repo),
ref=ref
)
else:
print(f">>> Symlink the repository with the appropriate folder - {repo}")
os.symlink(
GIT_LOCATION.format(repo="scripts")
+ f"/sdk_container/src/third_party/{repo}",
GIT_LOCATION.format(repo=repo),
)
def main():
try:
sync_repo()
# Perform normal post-sync tasks
configroot = portage.settings["PORTAGE_CONFIGROOT"]
post_sync = "%s/etc/portage/bin/post_sync" % configroot
if os.path.exists(post_sync):
subprocess.check_call([post_sync])
subprocess.check_call(["emerge", "--check-news", "--quiet"])
except Exception as e:
print(e.output)
sys.stderr.write(">>> No git repositories configured.\n")
sys.exit(1)
if __name__ == "__main__":
main()