Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

tools: run engine sync generators and git subprocesses in UTF-8 mode on Windows

LNPANDY095038e0228d
diff --git a/tools/engine.py b/tools/engine.py
index 3f07c6e8..57fa90f1 100755
--- a/tools/engine.py
+++ b/tools/engine.py
@@ -94,7 +94,7 @@ def engine_dir(root):
def pin(root):
f = root / '.upstream-ref'
if not f.is_file(): raise Fail(f'{f} is missing: this is not an NX2Apple checkout')
- sha = f.read_text().strip()
+ sha = f.read_text(encoding='utf-8').strip()
if not re.fullmatch(r'[0-9a-f]{40}', sha): raise Fail(f'.upstream-ref must hold a full commit id (is: {sha!r})')
return sha
@@ -113,13 +113,13 @@ def stamp_key(root):
def read_stamp(eng):
- try: text = (eng / '.git' / STAMP).read_text()
+ try: text = (eng / '.git' / STAMP).read_text(encoding='utf-8')
except OSError: return {}
return dict(l.split(' ', 1) for l in text.splitlines() if ' ' in l)
def write_stamp(eng, root, head, key=None):
- (eng / '.git' / STAMP).write_text(f'nx2apple-engine 1\npin {pin(root)}\nkey {key or stamp_key(root)}\nhead {head}\n')
+ (eng / '.git' / STAMP).write_text(f'nx2apple-engine 1\npin {pin(root)}\nkey {key or stamp_key(root)}\nhead {head}\n', encoding='utf-8')
def is_repo(eng):
@@ -225,9 +225,9 @@ def build(root, eng, patches, sha):
say('empty on the pin (upstream has them, `tools/engine.py bump` drops them): ' + ', '.join(p.file for p in landed))
for out, tool in P.GENERATED:
if (eng / tool).is_file():
- r = subprocess.run([sys.executable, str(eng / tool)], cwd=eng, capture_output=True, text=True, check=False, env=P.clean_env())
+ r = subprocess.run([sys.executable, '-X', 'utf8', str(eng / tool)], cwd=eng, capture_output=True, text=True, encoding='utf-8', errors='replace', check=False, env=P.clean_env())
if r.returncode: raise Fail(f'{tool} failed on the patched tree: {(r.stderr.strip().splitlines() or [str(r.returncode)])[-1]}')
- (eng / P.FORK_PATCHES).write_text(P.fork_patch_list([p for p, _ in applied]))
+ (eng / P.FORK_PATCHES).write_text(P.fork_patch_list([p for p, _ in applied]), encoding='utf-8')
git('add', '--force', '--', P.FORK_PATCHES, *(o for o, _ in P.GENERATED if (eng / o).exists()), cwd=eng)
git(*IDENT, 'commit', '--quiet', '--allow-empty', '-m', GENERATED_SUBJECT, cwd=eng)
return applied, landed
@@ -382,8 +382,8 @@ def cmd_tree(a):
out.mkdir(parents=True, exist_ok=True)
n = extract_tree(eng, out)
eid = engine_id(root)
- (out / '.upstream-ref').write_text(pin(root) + '\n')
- (out / ENGINE_ID_FILE).write_text(eid + '\n')
+ (out / '.upstream-ref').write_text(pin(root) + '\n', encoding='utf-8')
+ (out / ENGINE_ID_FILE).write_text(eid + '\n', encoding='utf-8')
if not (out / P.FORK_PATCHES).is_file(): raise Fail(f'{P.FORK_PATCHES} is missing from engine/: sync again')
say(f'engine tree: {eid} ({n} files) in {out}')
if a.zip:
diff --git a/tools/patches.py b/tools/patches.py
index 391e086c..f848743f 100644
--- a/tools/patches.py
+++ b/tools/patches.py
@@ -44,11 +44,12 @@ class Fail(Exception):
def clean_env(**extra):
"""The environment without the variables that point git at a repository."""
- return {**{k: v for k, v in os.environ.items() if k not in REPO_ENV}, **extra}
+ return {**{k: v for k, v in os.environ.items() if k not in REPO_ENV}, 'PYTHONUTF8': '1', 'PYTHONIOENCODING': 'utf-8', **extra}
def git(*a, cwd=None, check=True, inp=None, binary=False):
- r = subprocess.run(['git', '-c', 'core.autocrlf=false', *a], cwd=cwd or REPO, input=inp, capture_output=True, text=not binary, env=clean_env())
+ kw = {'text': True, 'encoding': 'utf-8', 'errors': 'replace'} if not binary else {}
+ r = subprocess.run(['git', '-c', 'core.autocrlf=false', *a], cwd=cwd or REPO, input=inp, capture_output=True, env=clean_env(), **kw)
if check and r.returncode:
raise Fail(f'git {" ".join(a)} failed: {(r.stderr if not binary else r.stderr.decode(errors="replace")).strip()[-1500:]}')
return r
@@ -115,7 +116,7 @@ def read_series(root):
s = root / 'patches/series'
if not s.is_file(): raise Fail(f'{s} is missing')
out = []
- for line in s.read_text().splitlines():
+ for line in s.read_text(encoding='utf-8').splitlines():
line = line.split('#', 1)[0].strip()
if line: out.append(line)
return out
@@ -132,7 +133,7 @@ def load(root):
def chain_paths(root):
"""The globs of the shader chain, read from the engine's AGENTS.md (in `root`, a tree of the engine), the chain paragraph (the one naming DMSH_CHAIN)."""
- try: text = (root / 'AGENTS.md').read_text()
+ try: text = (root / 'AGENTS.md').read_text(encoding='utf-8')
except OSError: return list(CHAIN_FALLBACK)
para = next((l for l in text.splitlines() if 'DMSH_CHAIN' in l and 'dm/gfx/tools/dmsh_' in l), None)
found = []