-
Notifications
You must be signed in to change notification settings - Fork 527
NO-SNOW: Add core library import #2657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sfc-gh-turbaszek
wants to merge
5
commits into
main
Choose a base branch
from
turbaszek-add-core-import
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
633e04f
NO-SNOW: Add core library import
sfc-gh-turbaszek cb8ec27
fixup! NO-SNOW: Add core library import
sfc-gh-turbaszek cf0ddc0
Move telemetry from auth request to in-band
sfc-gh-turbaszek 787712b
fixup! Move telemetry from auth request to in-band
sfc-gh-turbaszek 7d412f4
Add option to disable core loading
sfc-gh-turbaszek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,11 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import ctypes | ||
| import importlib | ||
| import os | ||
| import string | ||
| import sys | ||
| import threading | ||
| from enum import Enum | ||
| from inspect import stack | ||
| from random import choice | ||
|
|
@@ -96,3 +101,74 @@ def get_application_path() -> str: | |
| return outermost_frame.filename | ||
| except Exception: | ||
| return "unknown" | ||
|
|
||
|
|
||
| class _CoreLoader: | ||
| def __init__(self): | ||
| self._version: bytes | None = None | ||
| self._error: Exception | None = None | ||
|
|
||
| @staticmethod | ||
| def _get_core_path(): | ||
| # Define the file name for each platform | ||
| if sys.platform.startswith("win"): | ||
| lib_name = "libsf_mini_core.dll" | ||
| elif sys.platform.startswith("darwin"): | ||
| lib_name = "libsf_mini_core.dylib" | ||
| else: | ||
| lib_name = "libsf_mini_core.so" | ||
|
|
||
| files = importlib.resources.files("snowflake.connector.minicore") | ||
| return files.joinpath(lib_name) | ||
|
|
||
| @staticmethod | ||
| def _register_functions(core: ctypes.CDLL): | ||
| core.sf_core_full_version.argtypes = [] | ||
| core.sf_core_full_version.restype = ctypes.c_char_p | ||
|
|
||
| @staticmethod | ||
| def _load_minicore(path: str) -> ctypes.CDLL: | ||
| # This context manager is the safe way to get a | ||
| # file path from importlib.resources. It handles cases | ||
| # where the file is inside a zip and needs to be extracted | ||
| # to a temporary location. | ||
| with importlib.resources.as_file(path) as lib_path: | ||
| core = ctypes.CDLL(str(lib_path)) | ||
| return core | ||
|
|
||
| def _is_core_disabled(self) -> bool: | ||
| value = str(os.getenv("SNOWFLAKE_DISABLE_MINICORE", None)).lower() | ||
| return value in ["1", "true"] | ||
|
|
||
| def _load(self) -> None: | ||
| try: | ||
| path = self._get_core_path() | ||
| core = self._load_minicore(path) | ||
| self._register_functions(core) | ||
| self._version = core.sf_core_full_version() | ||
| self._error = None | ||
| except Exception as err: | ||
| self._error = err | ||
|
|
||
| def load(self): | ||
| """Spawn a separate thread to load the minicore library (non-blocking).""" | ||
| if self._is_core_disabled(): | ||
| self._error = "mini-core-disabled" | ||
| return | ||
| self._error = "still-loading" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we wrap it into an error / change class definition to |
||
| thread = threading.Thread(target=self._load, daemon=True) | ||
| thread.start() | ||
|
|
||
| def get_load_error(self) -> str: | ||
| return str(self._error) | ||
|
|
||
| def get_core_version(self) -> str | None: | ||
| if self._version: | ||
| try: | ||
| return self._version.decode("utf-8") | ||
| except Exception: | ||
| pass | ||
| return None | ||
|
|
||
|
|
||
| _core_loader = _CoreLoader() | ||
This file contains hidden or 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
Empty file.
This file contains hidden or 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,54 @@ | ||
| /** | ||
| * @file sf_mini_core.h | ||
| * @brief C API for sf_mini_core library | ||
| * | ||
| * This header file provides the C API for the sf_mini_core Rust library. | ||
| * This file is auto-generated by cbindgen. Do not edit manually! | ||
| */ | ||
|
|
||
| #ifndef SF_MINI_CORE_H | ||
| #define SF_MINI_CORE_H | ||
|
|
||
| /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. | ||
| */ | ||
|
|
||
| #include <stdarg.h> | ||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif // __cplusplus | ||
|
|
||
| /** | ||
| * Returns the full version string for sf_core. | ||
| * | ||
| * This function returns a pointer to a static null-terminated string | ||
| * containing the version of sf_core. | ||
| * | ||
| * @return A pointer to a static string containing the version. | ||
| * The caller must NOT free this pointer. | ||
| * The returned string is valid for the lifetime of the program. | ||
| * | ||
| * @note Thread-safe: Yes | ||
| * @note This function never returns NULL | ||
| * | ||
| * Example usage: | ||
| * @code | ||
| * const char* version = sf_core_full_version(); | ||
| * printf("Version: %s\n", version); | ||
| * @endcode | ||
| * | ||
| * # Safety | ||
| * | ||
| * The returned pointer points to a static string that is valid for the lifetime | ||
| * of the program. The caller must not free the returned pointer. | ||
| */ | ||
| const char *sf_core_full_version(void); | ||
|
|
||
| #ifdef __cplusplus | ||
| } // extern "C" | ||
| #endif // __cplusplus | ||
|
|
||
| #endif /* SF_MINI_CORE_H */ |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we wrap it into an error / change class definition to Exception | str | None?