code: def pack_rpackage() -> Path:
      """Compose the directory used for creating R package tar ball."""
      dest = Path("xgboost")
  
      def pkgroot(path: str) -> None:
          """Change makefiles according to the package layout."""
          with open(Path("R-package") / "src" / path, "r") as fd:
              makefile = fd.read()
              makefile = makefile.replace(
                  "PKGROOT=../../", "PKGROOT=.", 1
              )
          with open(dest / "src" / path, "w") as fd:
              fd.write(makefile)
  
      output = subprocess.run(
          ["git", "clean", "-xdf", "--dry-run"], capture_output=True
      )
      if output.returncode != 0:
          raise ValueError(
              "Failed to check git repository status.", output
          )
      would_remove = output.stdout.decode("utf-8").strip().split("\n")
  
      if would_remove and not all(
          f.find("tests/ci_build") != -1 for f in would_remove
      ):
          raise ValueError(
              "\n".join(would_remove)
              + "\nPlease cleanup the working git repository."
          )
  
      shutil.copytree("R-package", dest)
      os.remove(dest / "demo" / "runall.R")
      # core
      shutil.copytree("src", dest / "src" / "src")
      shutil.copytree("include", dest / "src" / "include")
      shutil.copytree("amalgamation", dest / "src" / "amalgamation")
      # rabit
      rabit = Path("rabit")
      os.mkdir(dest / "src" / rabit)
      shutil.copytree(rabit / "src", dest / "src" / "rabit" / "src")
      shutil.copytree(
          rabit / "include", dest / "src" / "rabit" / "include"
      )
      # dmlc-core
      dmlc_core = Path("dmlc-core")
      os.mkdir(dest / "src" / dmlc_core)
      shutil.copytree(
          dmlc_core / "include", dest / "src" / "dmlc-core" / "include"
      )
      shutil.copytree(
          dmlc_core / "src", dest / "src" / "dmlc-core" / "src"
      )
      # makefile & license
      shutil.copyfile("LICENSE", dest / "LICENSE")
      osxmakef = dest / "src" / "Makevars.win-e"
      if os.path.exists(osxmakef):
          os.remove(osxmakef)
      pkgroot("Makevars.in")
      pkgroot("Makevars.win")
      # misc
      rwsp = Path("R-package") / "remove_warning_suppression_pragma.sh"
      if system() != "Windows":
          subprocess.check_call(rwsp)
      rwsp = dest / "remove_warning_suppression_pragma.sh"
      if system() != "Windows":
          subprocess.check_call(rwsp)
      os.remove(rwsp)
      os.remove(dest / "CMakeLists.txt")
      shutil.rmtree(dest / "tests" / "helper_scripts")
      return dest
  
file_path: xgboost/tests/ci_build/test_r_package.py
imports: import argparse
  import os
  import shutil
  import subprocess
  from io import StringIO
  from pathlib import Path
  from platform import system
  from test_utils import R_PACKAGE, ROOT, DirectoryExcursion, cd, print_time, record_time
label_desc: Use image node_id 2 for calling a global function with example usage: pack_rpackage() and returns: dest