codekingpro/portable-devtools
114k
1"""Print information about the system and langchain packages for debugging purposes."""2 3import pkgutil4import platform5import re6import sys7from collections.abc import Sequence8from importlib import metadata, util9 10 11def _get_sub_deps(packages: Sequence[str]) -> list[str]:12 """Get any specified sub-dependencies."""13 sub_deps = set()14 underscored_packages = {pkg.replace("-", "_") for pkg in packages}15 16 for pkg in packages:17 try:18 required = metadata.requires(pkg)19 except metadata.PackageNotFoundError:20 continue21 22 if not required:23 continue24 25 for req in required:26 # Extract package name (e.g., "httpx<1,>=0.23.0" -> "httpx")27 match = re.match(r"^([a-zA-Z0-9_.-]+)", req)28 if match:29 pkg_name = match.group(1)30 if pkg_name.replace("-", "_") not in underscored_packages:31 sub_deps.add(pkg_name)32 33 return sorted(sub_deps, key=lambda x: x.lower())34 35 36def print_sys_info(*, additional_pkgs: Sequence[str] = ()) -> None:37 """Print information about the environment for debugging purposes.38 39 Args:40 additional_pkgs: Additional packages to include in the output.41 """42 # Packages that do not start with "langchain" prefix.43 other_langchain_packages = [44 "langsmith",45 "deepagents",46 "deepagents-cli",47 ]48 49 langchain_pkgs = [50 name for _, name, _ in pkgutil.iter_modules() if name.startswith("langchain")51 ]52 53 langgraph_pkgs = [54 name for _, name, _ in pkgutil.iter_modules() if name.startswith("langgraph")55 ]56 57 all_packages = sorted(58 set(59 langchain_pkgs60 + langgraph_pkgs61 + other_langchain_packages62 + list(additional_pkgs)63 )64 )65 66 # Always surface these packages to the top67 order_by = ["langchain_core", "langchain", "langchain_community", "langsmith"]68 69 for pkg in reversed(order_by):70 if pkg in all_packages:71 all_packages.remove(pkg)72 all_packages = [pkg, *list(all_packages)]73 74 system_info = {75 "OS": platform.system(),76 "OS Version": platform.version(),77 "Python Version": sys.version,78 }79 print()80 print("System Information")81 print("------------------")82 print("> OS: ", system_info["OS"])83 print("> OS Version: ", system_info["OS Version"])84 print("> Python Version: ", system_info["Python Version"])85 86 # Print out only langchain packages87 print()88 print("Package Information")89 print("-------------------")90 91 not_installed = []92 93 for pkg in all_packages:94 try:95 found_package = util.find_spec(pkg)96 except Exception:97 found_package = None98 if found_package is None:99 not_installed.append(pkg)100 continue101 102 # Package version103 try:104 package_version = metadata.version(pkg)105 except Exception:106 package_version = None107 108 # Print package with version109 if package_version is not None:110 print(f"> {pkg}: {package_version}")111 112 if not_installed:113 print()114 print("Optional packages not installed")115 print("-------------------------------")116 for pkg in not_installed:117 print(f"> {pkg}")118 119 sub_dependencies = _get_sub_deps(all_packages)120 121 if sub_dependencies:122 print()123 print("Other Dependencies")124 print("------------------")125 126 for dep in sub_dependencies:127 try:128 dep_version = metadata.version(dep)129 except Exception:130 dep_version = None131 132 if dep_version is not None:133 print(f"> {dep}: {dep_version}")134 135 136if __name__ == "__main__":137 print_sys_info()138 