summaryrefslogtreecommitdiffstats
path: root/build/pypi/openbb_platform/publish.py
blob: 6d163af0860be4c8dc113d477a1fefcd646370e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Publish the OpenBB Platform to PyPi."""

import argparse
import subprocess
import sys
from pathlib import Path

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

CORE_PACKAGES = ["core"]
EXTENSION_PACKAGES = ["extensions", "providers", "obbject_extensions"]

CMD = [sys.executable, "-m", "poetry"]
EXTENSION_DEPENDENCIES_UPDATE_CMD = ["add", "openbb-core=latest", "--lock"]
VERSION_BUMP_CMD = ["version", "patch"]
PUBLISH_CMD = ["publish", "--build"]


def parse_args():
    parser = argparse.ArgumentParser(
        description="Publish OpenBB Platform to PyPi with optional core or extensions flag."
    )
    parser.add_argument(
        "-c", "--core", action="store_true", help="Publish core packages.", dest="core"
    )
    parser.add_argument(
        "-e",
        "--extensions",
        action="store_true",
        help="Publish extension packages.",
        dest="extensions",
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="Run the commands without actually publishing.",
        default=False,
        dest="dry_run",
    )
    return parser.parse_args()


def update_extension_dependencies(path: Path):
    """Update the extension dependencies"""
    subprocess.run(
        CMD + EXTENSION_DEPENDENCIES_UPDATE_CMD,  # noqa: S603
        cwd=path.parent,
        check=True,
    )


def bump_version(path: Path):
    """Bump the version of the package"""
    print(f"\n> {path.parent.stem}")  # noqa: T201
    subprocess.run(CMD + VERSION_BUMP_CMD, cwd=path.parent, check=True)  # noqa: S603


def publish(dry_run: bool = False, core: bool = False, extensions: bool = False):
    """Publish the Platform to PyPi with optional core or extensions."""
    package_paths = []
    if core:
        print("Working with core packages...")  # noqa: T201
        package_paths.extend(CORE_PACKAGES)
    if extensions:
        print("Working with extensions...")  # noqa: T201
        package_paths.extend(EXTENSION_PACKAGES)

    for sub_path in package_paths:
        is_extension = sub_path in EXTENSION_PACKAGES

        for path in sorted(PLATFORM_PATH.rglob(f"{sub_path}/**/pyproject.toml")):
            try:
                # Update dependencies
                if is_extension and "devtools" not in str(path):
                    update_extension_dependencies(path)
                # Bump version
                bump_version(path)
                # Publish (if not dry run)
                if not dry_run:
                    subprocess.run(
                        CMD + PUBLISH_CMD,  # noqa: S603
                        cwd=path.parent,
                        check=True,  # noqa: S603
                    )
            except Exception as e:
                print(f"Error publishing {path.parent}:\n{e}")  # noqa: T201


if __name__ == "__main__":
    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.
    """
    args = parse_args()

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

    if res.lower() == "y":
        publish(dry_run=args.dry_run, core=args.core, extensions=args.extensions)

        openbb_package_msg = """
        In order to publish the `openbb` package you need to manually update the
        versions in the `pyproject.toml` file. Follow the steps below:
        1. Bump version: `poetry version prerelease --next-phase`
        2. Re-build the static assets that are bundled with the package
        3. Publish: `poetry publish --build`
        """

        print(openbb_package_msg)  # noqa: T201