|
| 1 | +# Standard |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | + |
| 5 | +dependencies_with_patch = { |
| 6 | + "microxcaling": "https://github.com/microsoft/microxcaling.git", |
| 7 | +} |
| 8 | + |
| 9 | + |
| 10 | +def install_with_patch( |
| 11 | + pkg_name: str, |
| 12 | + repo_url: str, |
| 13 | + patch_file: str, |
| 14 | + home_dir: str = None, |
| 15 | +) -> None: |
| 16 | + """ |
| 17 | + Install a dependency with a patch file |
| 18 | +
|
| 19 | + Args: |
| 20 | + pkg_name (str): Name of package being installed |
| 21 | + repo_url (str): Github repo URL |
| 22 | + patch_file (str): Patch file in patches/<patch_file> |
| 23 | + home_dir (str): Home directory with fms-model-optimizer and other packages. |
| 24 | + Defaults to None. |
| 25 | + """ |
| 26 | + # We want to git clone the repo to $HOME/repo_name |
| 27 | + if home_dir is None: |
| 28 | + home_dir = os.path.expanduser("~") |
| 29 | + |
| 30 | + # Get fms_mo directory in home_dir |
| 31 | + cwd = os.getcwd() |
| 32 | + |
| 33 | + # Get patch file location from fms-model-optimizer |
| 34 | + patch_file = os.path.join(cwd, "patches", patch_file) |
| 35 | + if not os.path.exists(patch_file): |
| 36 | + raise FileNotFoundError(f"Can't find {pkg_name} patch file in {cwd}/patches") |
| 37 | + |
| 38 | + # Check to see if package exists in cwd or home_dir |
| 39 | + pkg_path_cwd = os.path.join(cwd, pkg_name) |
| 40 | + pkg_path_home = os.path.join(home_dir, pkg_name) |
| 41 | + pkg_exists_cwd = os.path.exists(pkg_path_cwd) |
| 42 | + pkg_exists_home = os.path.exists(pkg_path_home) |
| 43 | + |
| 44 | + # If pkg already exists in cwd or home_dir, skip clone |
| 45 | + if pkg_exists_cwd: |
| 46 | + pkg_dir = pkg_path_cwd |
| 47 | + print(f"Directory {pkg_dir} already exists. Skipping download.") |
| 48 | + elif pkg_exists_home: |
| 49 | + pkg_dir = pkg_path_home |
| 50 | + print(f"Directory {pkg_dir} already exists. Skipping download.") |
| 51 | + else: |
| 52 | + # Clone repo to home directory |
| 53 | + pkg_dir = pkg_path_home |
| 54 | + subprocess.run(["git", "clone", repo_url], cwd=home_dir, check=True) |
| 55 | + |
| 56 | + # Apply patch and pip install package |
| 57 | + try: |
| 58 | + subprocess.run(["git", "apply", "--check", patch_file], cwd=pkg_dir, check=True) |
| 59 | + subprocess.run(["git", "apply", patch_file], cwd=pkg_dir, check=True) |
| 60 | + print( |
| 61 | + f"FMS Model Optimizer patch for {pkg_name} applied. Installing package now." |
| 62 | + ) |
| 63 | + subprocess.run(["pip", "install", "."], cwd=pkg_dir, check=True) |
| 64 | + |
| 65 | + except subprocess.CalledProcessError as e: |
| 66 | + print( |
| 67 | + f"FMS Model Optimizer patch for {pkg_name} is already installed " |
| 68 | + f"or an error has occured: \n{e}" |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def install_dependencies_with_patch() -> None: |
| 73 | + """ |
| 74 | + Script to install depenencies that requires a patch prior to pip install. |
| 75 | +
|
| 76 | + To execute, use `python install_patches.py`. |
| 77 | +
|
| 78 | + Requirements: |
| 79 | + 1. The patch file is named <package>.patch |
| 80 | + 2. Patch file must be located in fms-model-optimizer/patches |
| 81 | + """ |
| 82 | + for pkg, repo_url in dependencies_with_patch.items(): |
| 83 | + install_with_patch( |
| 84 | + pkg_name=pkg, |
| 85 | + repo_url=repo_url, |
| 86 | + patch_file=pkg + ".patch", |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + install_dependencies_with_patch() |
0 commit comments