-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper to fetch git repo info
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Utilities to get information about the runtime environment.""" | ||
from langsmith.env._runtime_env import ( | ||
get_docker_compose_command, | ||
get_docker_compose_version, | ||
get_docker_environment, | ||
get_docker_version, | ||
get_langchain_env_vars, | ||
get_langchain_environment, | ||
get_release_shas, | ||
get_runtime_and_metrics, | ||
get_runtime_environment, | ||
get_system_metrics, | ||
) | ||
from langsmith.env._git import get_git_info | ||
|
||
__all__ = [ | ||
"get_docker_compose_command", | ||
"get_docker_compose_version", | ||
"get_docker_environment", | ||
"get_docker_version", | ||
"get_langchain_env_vars", | ||
"get_langchain_environment", | ||
"get_release_shas", | ||
"get_runtime_and_metrics", | ||
"get_runtime_environment", | ||
"get_system_metrics", | ||
"get_git_info", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Fetch information about any current git repo.""" | ||
|
||
import functools | ||
import logging | ||
import subprocess | ||
from typing import List, Optional, TypeVar | ||
|
||
from typing_extensions import TypedDict | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def _exec_git(command: List[str]) -> Optional[str]: | ||
try: | ||
return subprocess.check_output( | ||
["git"] + command, encoding="utf-8", stderr=subprocess.DEVNULL | ||
).strip() | ||
except FileNotFoundError: | ||
logger.warning("git is not installed, or cannot be found in PATH") | ||
return None | ||
except subprocess.CalledProcessError as e: | ||
logger.debug(f"Error running git command: {e}") | ||
return None | ||
|
||
|
||
class GitInfo(TypedDict, total=False): | ||
remote_url: Optional[str] | ||
commit: Optional[str] | ||
branch: Optional[str] | ||
author_name: Optional[str] | ||
author_email: Optional[str] | ||
commit_message: Optional[str] | ||
commit_time: Optional[str] | ||
dirty: Optional[bool] | ||
tags: Optional[str] | ||
|
||
|
||
@functools.lru_cache(maxsize=1) | ||
def get_git_info(remote: str = "origin") -> Optional[GitInfo]: | ||
"""Get information about the git repository.""" | ||
|
||
if not _exec_git(["rev-parse", "--is-inside-work-tree"]): | ||
return None | ||
|
||
return { | ||
"remote_url": _exec_git(["remote", "get-url", remote]), | ||
"commit": _exec_git(["rev-parse", "HEAD"]), | ||
"commit_time": _exec_git(["log", "-1", "--format=%ct"]), | ||
"branch": _exec_git(["rev-parse", "--abbrev-ref", "HEAD"]), | ||
"tags": _exec_git( | ||
["describe", "--tags", "--exact-match", "--always", "--dirty"] | ||
), | ||
"dirty": _exec_git(["status", "--porcelain"]) != "", | ||
"author_name": _exec_git(["log", "-1", "--format=%an"]), | ||
"author_email": _exec_git(["log", "-1", "--format=%ae"]), | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "langsmith" | ||
version = "0.0.76" | ||
version = "0.0.77" | ||
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform." | ||
authors = ["LangChain <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from langsmith.env import __all__ as env_all | ||
from langsmith.env import get_git_info | ||
|
||
_EXPECTED = [ | ||
"get_docker_compose_command", | ||
"get_docker_compose_version", | ||
"get_docker_environment", | ||
"get_docker_version", | ||
"get_langchain_env_vars", | ||
"get_langchain_environment", | ||
"get_release_shas", | ||
"get_runtime_and_metrics", | ||
"get_runtime_environment", | ||
"get_system_metrics", | ||
"get_git_info", | ||
] | ||
|
||
|
||
def test_public_api() -> None: | ||
assert env_all == _EXPECTED | ||
|
||
|
||
def test_git_info() -> None: | ||
git_info = get_git_info() | ||
assert git_info is not None | ||
assert git_info["commit"] is not None | ||
assert git_info["remote_url"] is not None | ||
assert "langsmith-sdk" in git_info["remote_url"] |