try o fix ubuntu build

This commit is contained in:
Mateusz Gruszczyński
2025-10-24 10:23:14 +02:00
parent 4a60595122
commit c7ee019d28

View File

@@ -582,6 +582,17 @@ def install_node_and_yarn(node_pkg: str):
if not Path("/usr/bin/yarn").exists() and Path("/usr/bin/yarnpkg").exists(): if not Path("/usr/bin/yarn").exists() and Path("/usr/bin/yarnpkg").exists():
os.symlink("/usr/bin/yarnpkg","/usr/bin/yarn") os.symlink("/usr/bin/yarnpkg","/usr/bin/yarn")
def _is_ubuntu_wo_distutils() -> bool:
try:
dist = (OSREL.get("ID","") or "").lower()
ver = (OSREL.get("VERSION_ID","") or "").strip()
def _vers(t):
parts = (ver.split(".") + ["0","0"])[:2]
return (int(parts[0]), int(parts[1]))
return dist == "ubuntu" and _vers(ver) >= (24, 4)
except Exception:
return False
def _prepare_sass(frontend_dir: Path): def _prepare_sass(frontend_dir: Path):
pj = frontend_dir / "package.json" pj = frontend_dir / "package.json"
if not pj.exists(): if not pj.exists():
@@ -599,7 +610,8 @@ def _prepare_sass(frontend_dir: Path):
if not has_node_sass: if not has_node_sass:
return return
use_dart = (os.environ.get("USE_DART_SASS","").strip() == "1") env_flag = (os.environ.get("USE_DART_SASS","").strip())
use_dart = (env_flag == "1") or (env_flag == "" and _is_ubuntu_wo_distutils())
data.setdefault("dependencies", {}) data.setdefault("dependencies", {})
data.setdefault("devDependencies", {}) data.setdefault("devDependencies", {})
@@ -609,27 +621,32 @@ def _prepare_sass(frontend_dir: Path):
data["devDependencies"].pop("node-sass", None) data["devDependencies"].pop("node-sass", None)
if "sass" not in data["dependencies"] and "sass" not in data["devDependencies"]: if "sass" not in data["dependencies"] and "sass" not in data["devDependencies"]:
data["devDependencies"]["sass"] = "^1.77.0" data["devDependencies"]["sass"] = "^1.77.0"
scripts = (data.get("scripts") or {}) scripts = (data.get("scripts") or {})
new_scripts = {} data["scripts"] = {k: re.sub(r"\bnode-sass\b", "sass", v or "") for k, v in scripts.items()}
for k, v in scripts.items():
new_scripts[k] = re.sub(r"\bnode-sass\b", "sass", v or "") if env_flag == "":
data["scripts"] = new_scripts os.environ["USE_DART_SASS"] = "1"
else: else:
target = "^9.0.0" target = "^9.0.0"
if "node-sass" in data["dependencies"]: if "node-sass" in data["dependencies"]:
data["dependencies"]["node-sass"] = target data["dependencies"]["node-sass"] = target
else: else:
data["devDependencies"]["node-sass"] = target data["devDependencies"]["node-sass"] = target
res = (data.get("resolutions") or {}) res = (data.get("resolutions") or {})
res["node-gyp"] = "^10.0.0" res["node-gyp"] = "^10.0.0"
res["node-sass"] = "^9.0.0" res["node-sass"] = "^9.0.0"
data["resolutions"] = res data["resolutions"] = res
os.environ["npm_config_node_sass_binary_site"] = "https://github.com/sass/node-sass/releases/download" os.environ["npm_config_node_sass_binary_site"] = "https://github.com/sass/node-sass/releases/download"
pj.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8") pj.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
def _build_frontend(src_frontend: Path, dest_frontend: Path): def _build_frontend(src_frontend: Path, dest_frontend: Path):
def _semver(s: str) -> bool: def _semver(s: str) -> bool:
return bool(re.match(r"^\d+(?:\.\d+){1,3}$", (s or "").strip())) return bool(re.match(r"^\d+(?:\.\d+){1,3}$", (s or "").strip()))
@@ -662,17 +679,12 @@ def _build_frontend(src_frontend: Path, dest_frontend: Path):
if not yarn_cmd: if not yarn_cmd:
_ensure_yarn_installed() _ensure_yarn_installed()
yarn_cmd = _pick_yarn_cmd() yarn_cmd = _pick_yarn_cmd()
if not yarn_cmd: if not yarn_cmd:
raise RuntimeError( raise RuntimeError("Unable to detect or install a valid Yarn. Try: apt-get install -y npm && npm i -g yarn.")
"Unable to detect or install a valid Yarn. "
"Try: apt-get install -y npm && npm i -g yarn."
)
with step("Installing frontend dependencies (yarn)"): with step("Installing frontend dependencies (yarn)"):
os.environ["NODE_ENV"] = "development" os.environ["NODE_ENV"] = "development"
os.chdir(src_frontend) os.chdir(src_frontend)
_prepare_sass(src_frontend) _prepare_sass(src_frontend)
cache_dir = (run_out(yarn_cmd + ["cache", "dir"], check=False) or "").strip() cache_dir = (run_out(yarn_cmd + ["cache", "dir"], check=False) or "").strip()
@@ -693,48 +705,6 @@ def _build_frontend(src_frontend: Path, dest_frontend: Path):
shutil.copytree(src_frontend / "app-images", dest_frontend / "images", dirs_exist_ok=True) shutil.copytree(src_frontend / "app-images", dest_frontend / "images", dirs_exist_ok=True)
def _ensure_yarn_installed():
if not shutil.which("npm"):
try:
apt_try_install(["npm"])
except Exception:
run(["apt-get", "update"], check=False)
run(["apt-get", "install", "-y", "npm"])
run(["npm", "install", "-g", "yarn"], check=False)
yarn_cmd = _pick_yarn_cmd()
if not yarn_cmd:
_ensure_yarn_installed()
yarn_cmd = _pick_yarn_cmd()
if not yarn_cmd:
raise RuntimeError(
"Unable to detect or install a valid Yarn. "
"Try: apt-get install -y npm && npm i -g yarn."
)
with step("Installing frontend dependencies (yarn)"):
os.environ["NODE_ENV"] = "development"
os.chdir(src_frontend)
cache_dir = (run_out(yarn_cmd + ["cache", "dir"], check=False) or "").strip()
if cache_dir and not Path(cache_dir).exists():
Path(cache_dir).mkdir(parents=True, exist_ok=True)
run(yarn_cmd + ["cache", "clean"], check=False)
run(yarn_cmd + ["install"])
with step("Building frontend (yarn build)"):
env = os.environ.copy()
env["NODE_OPTIONS"] = "--openssl-legacy-provider"
run(yarn_cmd + ["build"], env=env)
with step("Copying frontend artifacts"):
shutil.copytree(src_frontend / "dist", dest_frontend, dirs_exist_ok=True)
if (src_frontend / "app-images").exists():
shutil.copytree(src_frontend / "app-images", dest_frontend / "images", dirs_exist_ok=True)
def patch_npm_backend_commands(): def patch_npm_backend_commands():
candidates = [ candidates = [
Path("/opt/npm/lib/utils.js"), Path("/opt/npm/lib/utils.js"),