-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode-oss.py
executable file
·95 lines (69 loc) · 2.09 KB
/
code-oss.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import subprocess
import sys
import os
import weakref
import json
import datetime
from pathlib import Path
if os.fork() != 0:
os._exit(0)
os.setsid()
if os.fork() != 0:
os._exit(0)
class TempPath:
def __init__(self):
prefix = "code-oss"
rand = os.urandom(16).hex()
today = datetime.date.today()
yyyymmdd = today.strftime("%Y%m%d")
p = f"/tmp/{prefix}-{yyyymmdd}-{rand}"
self.__path = Path(p)
self.__path.mkdir()
self.__f = weakref.finalize(self, subprocess.run, ["rm", "-rf", p])
def __enter__(self) -> Path:
return self.__path
def __exit__(self, _exc_type, _exc_value, _tb):
self.__f()
def __fspath__(self) -> str:
return str(self.__path)
def __str__(self) -> str:
return str(self.__path)
def __truediv__(self, other) -> Path:
return self.__path / other
EXECUTABLE = "code"
CODE_USER_DATA_DIR = TempPath()
CODE_EXTENSIONS_DIR = TempPath()
PROFILE = Path.home() / "leo-code-oss"
for p in (PROFILE / "userdata").iterdir():
subprocess.run(["cp", "-R", p.resolve(), CODE_USER_DATA_DIR], check=False)
extension_ids = set()
for p in (PROFILE / "extensions").iterdir():
target = CODE_EXTENSIONS_DIR / os.urandom(16).hex()
target.mkdir()
t = TempPath()
cmd = ["unzip", "-q", "-d", t, p.resolve()]
subprocess.run(cmd, check=False, cwd=t)
package_json = json.loads(
(t / "extension" / "package.json").read_text(encoding="utf-8")
)
extension_ids.add(package_json["publisher"] + "." + package_json["name"])
for x in (t / "extension").iterdir():
cmd = ["cp", "-R", x, target.resolve()]
subprocess.run(cmd, check=False)
cmd = [
EXECUTABLE,
"--wait",
"--user-data-dir",
CODE_USER_DATA_DIR,
"--extensions-dir",
CODE_EXTENSIONS_DIR,
"--locale",
"en-US",
]
for ext in extension_ids:
cmd.append("--enable-proposed-api")
cmd.append(ext)
cmd += sys.argv[1:]
subprocess.run(cmd, check=False)
subprocess.run(["pkill", "-9", "--full", str(CODE_USER_DATA_DIR)], check=False)