| 119 | from polygraphy.logger import G_LOGGER, LogMode |
| 120 | |
| 121 | def install_mod(install_name, install_version, raise_error=True): |
| 122 | modname = install_name.split(".")[0] |
| 123 | pkg = pkg_name if pkg_name is not None else _PKG_NAME_FROM_MODULE.get(modname, modname) |
| 124 | extra_flags = install_flags if install_flags is not None else _EXTRA_FLAGS_FOR_MODULE.get(modname, []) |
| 125 | |
| 126 | def fail(): |
| 127 | log_func = G_LOGGER.critical if raise_error else G_LOGGER.warning |
| 128 | log_func(f"Could not automatically install required module: {pkg}. Please install it manually.") |
| 129 | |
| 130 | if config.ASK_BEFORE_INSTALL: |
| 131 | res = None |
| 132 | while res not in ["y", "n"]: |
| 133 | res = input(f"Automatically install '{pkg}' (version: {install_version or 'any'}) ([Y]/n)? ") |
| 134 | res = res.strip()[:1].lower() or "y" |
| 135 | |
| 136 | if res == "n": |
| 137 | fail() |
| 138 | |
| 139 | if install_version == LATEST_VERSION: |
| 140 | extra_flags.append("--upgrade") |
| 141 | elif install_version is not None: |
| 142 | pkg += install_version |
| 143 | |
| 144 | cmd = config.INSTALL_CMD + [pkg] + extra_flags |
| 145 | G_LOGGER.info(f"Running installation command: {' '.join(cmd)}") |
| 146 | status = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE) |
| 147 | if status.returncode != 0: |
| 148 | G_LOGGER.error(f"Error during installation:\n{constants.TAB}{status.stderr.decode()}") |
| 149 | fail() |
| 150 | |
| 151 | mod = importlib.import_module(install_name) |
| 152 | return mod |
| 153 | |
| 154 | mod = None |
| 155 | try: |