mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-17 02:03:44 +00:00
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
This commit is contained in:
parent
923e9f9596
commit
ca2eed36b7
@ -4,18 +4,19 @@ from typing import Optional
|
|||||||
|
|
||||||
from typing_extensions import Annotated
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
from langchain.cli.create_repo.base import create, is_poetry_installed
|
# Keep this import here so that we can check if Typer is 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
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import typer
|
import typer
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise ImportError(
|
raise ImportError(
|
||||||
"Typer must be installed to use the CLI. "
|
"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)
|
app = typer.Typer(no_args_is_help=False, add_completion=False)
|
||||||
|
|
||||||
|
@ -98,6 +98,10 @@ def _copy_template_files(
|
|||||||
"""
|
"""
|
||||||
for template_directory_path in template_directories:
|
for template_directory_path in template_directories:
|
||||||
for template_file_path in template_directory_path.glob("**/*"):
|
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(
|
relative_template_file_path = UnderscoreTemplate(
|
||||||
str(template_file_path.relative_to(template_directory_path))
|
str(template_file_path.relative_to(template_directory_path))
|
||||||
).substitute(project_name_identifier=project_name_identifier)
|
).substitute(project_name_identifier=project_name_identifier)
|
||||||
@ -105,8 +109,16 @@ def _copy_template_files(
|
|||||||
if template_file_path.is_dir():
|
if template_file_path.is_dir():
|
||||||
project_file_path.mkdir(parents=True, exist_ok=True)
|
project_file_path.mkdir(parents=True, exist_ok=True)
|
||||||
else:
|
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(
|
project_file_path.write_text(
|
||||||
UnderscoreTemplate(template_file_path.read_text()).substitute(
|
UnderscoreTemplate(content).substitute(
|
||||||
project_name=project_name,
|
project_name=project_name,
|
||||||
project_name_identifier=project_name_identifier,
|
project_name_identifier=project_name_identifier,
|
||||||
author_name=author_name,
|
author_name=author_name,
|
||||||
@ -146,7 +158,11 @@ def _init_git(project_directory_path: Path) -> None:
|
|||||||
typer.echo(
|
typer.echo(
|
||||||
f"\n{typer.style('Initializing git...', bold=True, fg=typer.colors.GREEN)}"
|
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
|
# 7. Create initial commit
|
||||||
subprocess.run(["git", "add", "."], cwd=project_directory_path)
|
subprocess.run(["git", "add", "."], cwd=project_directory_path)
|
||||||
@ -243,4 +259,8 @@ def create(
|
|||||||
|
|
||||||
def is_poetry_installed() -> bool:
|
def is_poetry_installed() -> bool:
|
||||||
"""Check if Poetry is installed."""
|
"""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
|
||||||
|
Loading…
Reference in New Issue
Block a user