Skip to content
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

Platform publishing procedure #5701

Merged
merged 9 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build/pypi/openbb_platform/PUBLISH.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ Publishing checklist:
1. Ensure all unit tests pass: `pytest openbb_platform -m "not integration"`
2. Ensure all integration tests pass: `pytest openbb_platform -m integration`
3. Change the Platform version on:
- `openbb_platform/platform/core/openbb_core/app/constants.py`
- `openbb_platform/README.md`
- `openbb_platform/platform/core/openbb_core/app/constants.py`
4. Run the publishing script: `python build/pypi/openbb_platform/publish.py`
5. Update poetry files: `python build/pypi/openbb_platform/poetry_update.py`
6. Open a PR so that changes are reflected on the main branch
7. If applicable, set the version on `constants.py` to use the `dev` tag again

Finally, check if everything works:

Expand Down
18 changes: 11 additions & 7 deletions build/pypi/openbb_platform/publish.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
""" Publish the OpenBB Platform to PyPi"""
import subprocess
import sys
from pathlib import Path

PLATFORM_PATH = Path(__file__).parent.resolve() / "openbb_platform"
PLATFORM_PATH = Path(__file__).parent.parent.parent.parent.resolve() / "openbb_platform"

SUB_PACKAGES = ["platform/provider", "platform/core", "extensions", "providers"]

Expand Down Expand Up @@ -30,10 +31,13 @@ def publish():


if __name__ == "__main__":
raise Exception(
"If you're ar running this script for the first time,"
"ensure you have changed `VERSION` on System Settings "
"before you publish the `openbb-core` package to Pypi."
)
msg = """
You are about to publish a new version of OpenBB Platform to PyPI.
Please ensure you've read the "PUBLISH.md" file.
Also, please double check with `poetry config --list` if you're publishing to PyPI or TestPyPI.
"""

publish()
res = input(f"{msg}\n\nDo you want to continue? [y/N] ")

if res.lower() == "y":
publish()
5 changes: 4 additions & 1 deletion openbb_platform/platform/core/openbb_core/api/rest_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""REST API for the OpenBB Platform."""
import logging

from fastapi import FastAPI, Request
Expand All @@ -7,9 +8,9 @@
from openbb_core.api.router.commands import router as router_commands
from openbb_core.api.router.coverage import router as router_coverage
from openbb_core.api.router.system import router as router_system
from openbb_core.app.constants import VERSION
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.app.service.auth_service import AuthService
from openbb_core.app.version import VERSION
from openbb_core.env import Env

logger = logging.getLogger("uvicorn.error")
Expand Down Expand Up @@ -69,6 +70,7 @@ async def startup():

@app.exception_handler(Exception)
async def api_exception_handler(request: Request, exc: Exception):
"""Exception handler for all other exceptions."""
return JSONResponse(
status_code=404,
content={
Expand All @@ -80,6 +82,7 @@ async def api_exception_handler(request: Request, exc: Exception):

@app.exception_handler(OpenBBError)
async def openbb_exception_handler(request: Request, exc: OpenBBError):
"""Exception handler for OpenBB errors."""
openbb_error = exc.original
status_code = 400 if "No results" in str(openbb_error) else 500
return JSONResponse(
Expand Down
2 changes: 1 addition & 1 deletion openbb_platform/platform/core/openbb_core/app/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Constants for the OpenBB Platform."""
from pathlib import Path

HOME_DIRECTORY = Path.home()
OPENBB_DIRECTORY = Path(HOME_DIRECTORY, ".openbb_platform")
USER_SETTINGS_PATH = Path(OPENBB_DIRECTORY, "user_settings.json")
SYSTEM_SETTINGS_PATH = Path(OPENBB_DIRECTORY, "system_settings.json")
VERSION = "4.0.0dev"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""The OpenBB Platform System Settings."""
import json
import platform as pl # I do this so that the import doesn't conflict with the variable name
from functools import partial
Expand All @@ -11,14 +12,16 @@
OPENBB_DIRECTORY,
SYSTEM_SETTINGS_PATH,
USER_SETTINGS_PATH,
VERSION,
)
from openbb_core.app.model.abstract.tagged import Tagged
from openbb_core.app.version import VERSION

FrozenField = partial(Field, frozen=True)


class SystemSettings(Tagged):
"""System settings model."""

# System section
os: str = FrozenField(default=str(pl.system()))
python_version: str = FrozenField(default=str(pl.python_version()))
Expand Down Expand Up @@ -50,17 +53,20 @@ class SystemSettings(Tagged):
model_config = ConfigDict(validate_assignment=True)

def __repr__(self) -> str:
"""Return a string representation of the model."""
return f"{self.__class__.__name__}\n\n" + "\n".join(
f"{k}: {v}" for k, v in self.model_dump().items()
)

@staticmethod
def create_empty_json(path: Path) -> None:
"""Create an empty JSON file."""
path.write_text(json.dumps({}), encoding="utf-8")

@model_validator(mode="after")
@classmethod
def create_openbb_directory(cls, values: "SystemSettings") -> "SystemSettings":
"""Create the OpenBB directory if it doesn't exist."""
obb_dir = Path(values.openbb_directory).resolve()
user_settings = Path(values.user_settings_path).resolve()
system_settings = Path(values.system_settings_path).resolve()
Expand All @@ -75,6 +81,7 @@ def create_openbb_directory(cls, values: "SystemSettings") -> "SystemSettings":
@model_validator(mode="after")
@classmethod
def validate_posthog_handler(cls, values: "SystemSettings") -> "SystemSettings":
"""If the user has enabled log collection, then we need to add the Posthog."""
if (
not any([values.test_mode, values.logging_suppress])
and values.log_collect
Expand All @@ -87,6 +94,7 @@ def validate_posthog_handler(cls, values: "SystemSettings") -> "SystemSettings":
@field_validator("logging_handlers")
@classmethod
def validate_logging_handlers(cls, v):
"""Validate the logging handlers."""
for value in v:
if value not in ["stdout", "stderr", "noop", "file", "posthog"]:
raise ValueError("Invalid logging handler")
Expand Down
39 changes: 39 additions & 0 deletions openbb_platform/platform/core/openbb_core/app/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Version script for the OpenBB Platform."""
import shutil
import subprocess
from pathlib import Path

import pkg_resources

PACKAGE = "openbb"


def get_package_version(package: str):
"""Retrieve the version of a package from installed pip packages."""
version = pkg_resources.get_distribution(package).version

if is_git_repo(Path(__file__).parent.resolve()):
version += "dev"

return version


def is_git_repo(path: Path):
"""Check if the given directory is a git repository."""
git_executable = shutil.which("git")
if not git_executable:
return False
try:
subprocess.run(
[git_executable, "rev-parse", "--is-inside-work-tree"], # noqa: S603
cwd=path,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
return True
except subprocess.CalledProcessError:
return False


VERSION = get_package_version(PACKAGE)
Loading