From ca2eed36b784e0816415022d9799c8893c5892aa Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 9 Oct 2023 16:30:16 -0400 Subject: [PATCH] LangChain cli fix a few bugs (#11573) Code was assuming that `git` and `poetry` exist. In addition, it was not ignoring pycache files that get generated during run time --- libs/langchain/langchain/cli/cli.py | 11 ++++---- .../langchain/cli/create_repo/base.py | 26 ++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/libs/langchain/langchain/cli/cli.py b/libs/langchain/langchain/cli/cli.py index 8a89ace5db..6c3c666f15 100644 --- a/libs/langchain/langchain/cli/cli.py +++ b/libs/langchain/langchain/cli/cli.py @@ -4,18 +4,19 @@ from typing import Optional from typing_extensions import Annotated -from langchain.cli.create_repo.base import create, is_poetry_installed -from langchain.cli.create_repo.pypi_name import is_name_taken, lint_name -from langchain.cli.create_repo.user_info import get_git_user_email, get_git_user_name - +# Keep this import here so that we can check if Typer is installed try: import typer except ImportError: raise ImportError( "Typer must be installed to use the CLI. " - "You can install it with `pip install typer`." + "You can install it with `pip install typer` or install LangChain " + 'with the [cli] extra like `pip install "langchain[cli]"`.' ) +from langchain.cli.create_repo.base import create, is_poetry_installed +from langchain.cli.create_repo.pypi_name import is_name_taken, lint_name +from langchain.cli.create_repo.user_info import get_git_user_email, get_git_user_name app = typer.Typer(no_args_is_help=False, add_completion=False) diff --git a/libs/langchain/langchain/cli/create_repo/base.py b/libs/langchain/langchain/cli/create_repo/base.py index 4cfa902eef..c6e09f376a 100644 --- a/libs/langchain/langchain/cli/create_repo/base.py +++ b/libs/langchain/langchain/cli/create_repo/base.py @@ -98,6 +98,10 @@ def _copy_template_files( """ for template_directory_path in template_directories: for template_file_path in template_directory_path.glob("**/*"): + # Ignore __pycache__ directories and their contents + if "__pycache__" in template_file_path.parts: + continue + relative_template_file_path = UnderscoreTemplate( str(template_file_path.relative_to(template_directory_path)) ).substitute(project_name_identifier=project_name_identifier) @@ -105,8 +109,16 @@ def _copy_template_files( if template_file_path.is_dir(): project_file_path.mkdir(parents=True, exist_ok=True) else: + try: + content = template_file_path.read_text(encoding="utf-8") + except UnicodeDecodeError as e: + raise RuntimeError( + "Encountered an error while reading a " + f"template file {template_file_path}" + ) from e + project_file_path.write_text( - UnderscoreTemplate(template_file_path.read_text()).substitute( + UnderscoreTemplate(content).substitute( project_name=project_name, project_name_identifier=project_name_identifier, author_name=author_name, @@ -146,7 +158,11 @@ def _init_git(project_directory_path: Path) -> None: typer.echo( f"\n{typer.style('Initializing git...', bold=True, fg=typer.colors.GREEN)}" ) - subprocess.run(["git", "init"], cwd=project_directory_path) + try: + subprocess.run(["git", "init"], cwd=project_directory_path) + except FileNotFoundError: + typer.echo("Git not found. Skipping git initialization.") + return # 7. Create initial commit subprocess.run(["git", "add", "."], cwd=project_directory_path) @@ -243,4 +259,8 @@ def create( def is_poetry_installed() -> bool: """Check if Poetry is installed.""" - return subprocess.run(["poetry", "--version"], capture_output=True).returncode == 0 + try: + result = subprocess.run(["poetry", "--version"], capture_output=True) + return result.returncode == 0 + except FileNotFoundError: + return False