forked from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone_repo.py
More file actions
59 lines (51 loc) · 1.78 KB
/
clone_repo.py
File metadata and controls
59 lines (51 loc) · 1.78 KB
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
import subprocess, os, pexpect
def subprocess_run(cmd_string=None, cmd_array=None, **kwargs):
cmd = cmd_string.split(" ") if cmd_string else cmd_array
print(f"Command running: {cmd}")
return subprocess.run(
cmd,
check=True,
stdout=subprocess.PIPE,
**kwargs
).stdout.decode("utf-8")
def check_known_hosts():
set_known_hosts = True
try:
keyscan = subprocess_run(
cmd_string=f"ssh-keygen -H -F {HOST}").stdout.decode("utf-8")
if len(keyscan) > 1:
set_known_hosts = False
except Exception as err: pass
if set_known_hosts:
home = os.path.expanduser("~")
cmds = [
f"mkdir -p {home}/.ssh",
f"touch {home}/.ssh/known_hosts",
f"ssh-keyscan {HOST}"
]
process = None
for c in cmds:
try:
# Replace this with `subprocess_run`
process = subprocess_run(cmd_string=c)
except subprocess.CalledProcessError as err: pass
print(process)
with open("/root/.ssh/known_hosts", 'a') as known_hosts:
known_hosts.write(process.stdout.decode("utf-8"))
known_hosts.write("\n")
HOST = subprocess_run(
cmd_array= [*"docker inspect -f".split(" "), "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'", "git"]
)
REPO = "repo"
PORT = 3333
CLONE_TO = "./mod/"
if not os.path.isdir(os.path.join(os.getcwd(), CLONE_TO)):
raise Exception(f"'{CLONE_TO}' does not exist in {os.getcwd()}")
check_known_hosts()
cmd = f"git clone --branch=master ssh://git@{HOST}:{PORT}/srv/{REPO} {CLONE_TO}"
child = pexpect.spawn(cmd, timeout=10)
child.expect("Cloning(.*)")
child.expect("git(.*)")
child.sendline('12345')
child.expect("Resolving deltas: 100%(.*)")
child.read()