code: def build_stan_model(target_dir):
      print("Importing cmdstanpy...")
      import cmdstanpy
      from multiprocessing import cpu_count
  
      target_cmdstan_dir = (
          Path(target_dir) / f"cmdstan-{CMDSTAN_VERSION}"
      ).resolve()
      print("target_cmdstan_dir: {}".format(target_cmdstan_dir))
  
      with tempfile.TemporaryDirectory() as tmp_dir:
          # long paths on windows can cause problems during build
          if IS_WINDOWS:
              print("Windows detected. Use tmp_dir: {}".format(tmp_dir))
              cmdstan_dir = (
                  Path(tmp_dir) / f"cmdstan-{CMDSTAN_VERSION}"
              ).resolve()
          else:
              cmdstan_dir = target_cmdstan_dir
  
          if os.path.isdir(cmdstan_dir):
              rmtree(cmdstan_dir)
  
          if not cmdstanpy.install_cmdstan(
              version=CMDSTAN_VERSION,
              # if we want to do it inside the repo dir, we need to include the folder in
              # MANIFEST.in
              dir=cmdstan_dir,
              overwrite=True,
              verbose=True,
              cores=cpu_count(),
              progress=True,
          ):
              raise RuntimeError(
                  "CmdStan failed to install in repackaged directory"
              )
          else:
              print("Installed cmdstanpy package.")
  
          print("cmdstan_dir: {}".format(cmdstan_dir))
          for model in MODELS:
              model_name = "{}.stan".format(model)
              # note: ensure copy target is a directory not a file.
  
              temp_source_file_path = os.path.join(
                  MODEL_SOURCE_DIR, model_name
              )
              print(
                  "Loading source file: {}".format(
                      temp_source_file_path
                  )
              )
              temp_stan_file = copy(
                  temp_source_file_path, cmdstan_dir.parent.resolve()
              )
  
              # temp_stan_file = os.path.join(MODEL_SOURCE_DIR, model_name)
  
              print("Compiling stan file: {}".format(temp_stan_file))
              sm = cmdstanpy.CmdStanModel(stan_file=temp_stan_file)
              target_name = "{}.bin".format(model)
              target_file_path = os.path.join(target_dir, target_name)
              print("Copying file to {}".format(target_file_path))
              copy(sm.exe_file, target_file_path)
  
imports: import sys
  import os
  import platform
  from pathlib import Path
  import tempfile
  from shutil import copy, copytree, rmtree
  from setuptools import setup, find_packages, Extension
  from setuptools.command.build_py import build_py
  from setuptools.command.test import test
  from setuptools.command.build_ext import build_ext
  from setuptools.command.editable_wheel import editable_wheel
  from wheel.bdist_wheel import bdist_wheel
label_desc: Use image node_id 3 for calling a global function with example usage: build_stan_model(target_dir) without return types