|
| 1 | +from .action import Action |
| 2 | +import os |
| 3 | +import json |
| 4 | +from . import utils |
| 5 | +from .logger import logger |
| 6 | + |
| 7 | +class CacheAction(Action): |
| 8 | + |
| 9 | + def __init__(self, parent=None): |
| 10 | + #super().__init__(parent) |
| 11 | + self.parent = parent |
| 12 | + self.__dict__.update(vars(parent)) |
| 13 | + |
| 14 | + def search(self, i): |
| 15 | + i['target_name'] = "cache" |
| 16 | + #logger.debug(f"Searching for cache with input: {i}") |
| 17 | + return self.parent.search(i) |
| 18 | + |
| 19 | + find = search |
| 20 | + |
| 21 | + def rm(self, i): |
| 22 | + i['target_name'] = "cache" |
| 23 | + #logger.debug(f"Removing cache with input: {i}") |
| 24 | + return self.parent.rm(i) |
| 25 | + |
| 26 | + def show(self, run_args): |
| 27 | + self.action_type = "cache" |
| 28 | + res = self.search(run_args) |
| 29 | + logger.info(f"Showing cache with tags: {run_args.get('tags')}") |
| 30 | + cached_meta_keys_to_show = ["uid", "tags", "dependent_cached_path", "associated_script_item"] |
| 31 | + cached_state_keys_to_show = ["new_env", "new_state", "version"] |
| 32 | + for item in res['list']: |
| 33 | + print(f"""Location: {item.path}: |
| 34 | +Cache Meta:""") |
| 35 | + for key in cached_meta_keys_to_show: |
| 36 | + if key in item.meta: |
| 37 | + print(f""" {key}: {item.meta[key]}""") |
| 38 | + print("""Cached State:""") |
| 39 | + cached_state_meta_file = os.path.join(item.path, "mlc-cached-state.json") |
| 40 | + if not os.path.exists(cached_state_meta_file): |
| 41 | + continue |
| 42 | + try: |
| 43 | + # Load and parse the JSON file containing the cached state |
| 44 | + with open(cached_state_meta_file, 'r') as file: |
| 45 | + meta = json.load(file) |
| 46 | + for key in cached_state_keys_to_show: |
| 47 | + if key in meta: |
| 48 | + print(f""" {key}:""", end="") |
| 49 | + if meta[key] and isinstance(meta[key], dict): |
| 50 | + print("") |
| 51 | + utils.printd(meta[key], yaml=False, sort_keys=True, begin_spaces=8) |
| 52 | + else: |
| 53 | + print(f""" {meta[key]}""") |
| 54 | + except json.JSONDecodeError as e: |
| 55 | + logger.error(f"Error decoding JSON: {e}") |
| 56 | + print("......................................................") |
| 57 | + print("") |
| 58 | + |
| 59 | + return {'return': 0} |
| 60 | + |
| 61 | + def list(self, args): |
| 62 | + self.action_type = "cache" |
| 63 | + run_args = {"fetch_all": True} # to fetch the details of all the caches generated |
| 64 | + |
| 65 | + res = self.search(run_args) |
| 66 | + if res['return'] > 0: |
| 67 | + return res |
| 68 | + |
| 69 | + logger.info(f"Listing all the caches and their paths") |
| 70 | + print("......................................................") |
| 71 | + for item in res['list']: |
| 72 | + print(f"tags: {item.meta['tags'] if item.meta.get('tags') else 'None'}") |
| 73 | + print(f"Location: {item.path}") |
| 74 | + print("......................................................") |
| 75 | + |
| 76 | + return {'return': 0} |
0 commit comments