Repository: apache/airflow
File: scripts/ci/prek/compile_provider_assets.py (Python)

Code before:
            print(f"The '{www_directory}' directory has not changed! Skip regeneration.")
            return
        print(f"The directory has changed, regenerating assets in {www_directory}.")
        print("Old hash: " + old_hash)
        print("New hash: " + new_hash)
    else:
        shutil.rmtree(dist_directory, ignore_errors=True)
    env = os.environ.copy()
    env["FORCE_COLOR"] = "true"
    for try_num in range(3):
        print(
            f"### Trying to install {PROVIDERS_BUILD[provider_name]} dependencies: attempt: {try_num + 1} ###"
        )
        result = subprocess.run(
            [PROVIDERS_BUILD[provider_name], "install", "--frozen-lockfile"],
            cwd=os.fspath(www_directory),
            text=True,
            check=False,
            capture_output=True,
        )
        if result.returncode == 0:
            break
        if try_num == 2 or INTERNAL_SERVER_ERROR not in result.stderr + result.stdout:
            print(result.stdout + "\n" + result.stderr)
            sys.exit(result.returncode)
    build_cmd = ["yarn", "run"] if PROVIDERS_BUILD[provider_name] == "yarn" else ["pnpm"]
    subprocess.check_call([*build_cmd, "build"], cwd=os.fspath(www_directory), env=env)
    new_hash = get_directory_hash(www_directory, skip_path_regexps=SKIP_PATH_REGEXPS)
    provider_paths["hash"].write_text(new_hash + "\n")
    print(f"Assets compiled successfully. New hash: {new_hash}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Compile provider assets for the specified provider.")
    parser.add_argument(
        "provider",
        type=str,
        help="The name of the provider whose assets should be compiled.",
        choices=list(PROVIDERS_PATHS.keys()),
    )
    args = parser.parse_args()
    compile_assets(args.provider)


Review comment (suggestion):
```suggestion
    build_cmd = PROVIDERS_BUILD[provider_name]["build"]
```

Code after:
    dist_directory = provider_paths["dist"]
    provider_paths["hash"].parent.mkdir(exist_ok=True, parents=True)
    if dist_directory.exists():
        old_hash = provider_paths["hash"].read_text().strip() if provider_paths["hash"].exists() else ""
        new_hash = get_directory_hash(www_directory, skip_path_regexps=SKIP_PATH_REGEXPS)
        if new_hash == old_hash:
            print(f"The '{www_directory}' directory has not changed! Skip regeneration.")
            return
        print(f"The directory has changed, regenerating assets in {www_directory}.")
        print("Old hash: " + old_hash)
        print("New hash: " + new_hash)
    else:
        shutil.rmtree(dist_directory, ignore_errors=True)
    env = os.environ.copy()
    env["FORCE_COLOR"] = "true"
    for try_num in range(3):
        print(f"### Trying to install {provider_name} dependencies: attempt: {try_num + 1} ###")
        result = subprocess.run(
            PROVIDERS_BUILD[provider_name]["install"],
            cwd=os.fspath(www_directory),
            text=True,
            check=False,
            capture_output=True,
        )
        if result.returncode == 0:
            break
        if try_num == 2 or INTERNAL_SERVER_ERROR not in result.stderr + result.stdout:
            print(result.stdout + "\n" + result.stderr)
            sys.exit(result.returncode)
    subprocess.check_call(PROVIDERS_BUILD[provider_name]["build"], cwd=os.fspath(www_directory), env=env)
    new_hash = get_directory_hash(www_directory, skip_path_regexps=SKIP_PATH_REGEXPS)
    provider_paths["hash"].write_text(new_hash + "\n")
    print(f"Assets compiled successfully. New hash: {new_hash}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Compile provider assets for the specified provider.")
    parser.add_argument(
        "provider",
        type=str,
        help="The name of the provider whose assets should be compiled.",
        choices=list(PROVIDERS_PATHS.keys()),
    )
    args = parser.parse_args()
    compile_assets(args.provider)