def main()

in utils/build-dists.py [0:0]


def main():
    run("git", "checkout", "--", "setup.py", "opensearchpy/")
    run("rm", "-rf", "build/", "dist/*", "*.egg-info", ".eggs")

    # Grab the major version to be used as a suffix.
    version_path = os.path.join(base_dir, "opensearchpy/_version.py")
    with open(version_path) as f:
        version = re.search(
            r"^__versionstr__\s+=\s+[\"\']([^\"\']+)[\"\']", f.read(), re.M
        ).group(1)
    major_version = version.split(".")[0]

    # If we're handed a version from the build manager we
    # should check that the version is correct or write
    # a new one.
    if len(sys.argv) >= 2:
        # 'build_version' is what the release manager wants,
        # 'expect_version' is what we're expecting to compare
        # the package version to before building the dists.
        build_version = expect_version = sys.argv[1]

        # Any prefixes in the version specifier mean we're making
        # a pre-release which will modify __versionstr__ locally
        # and not produce a git tag.
        if any(x in build_version for x in ("-SNAPSHOT", "-rc", "-alpha", "-beta")):
            # If a snapshot, then we add '+dev'
            if "-SNAPSHOT" in build_version:
                version = version + "+dev"
            # alpha/beta/rc -> aN/bN/rcN
            else:
                pre_number = re.search(r"-(a|b|rc)(?:lpha|eta|)(\d+)$", expect_version)
                version = version + pre_number.group(1) + pre_number.group(2)

            expect_version = re.sub(
                r"(?:-(?:SNAPSHOT|alpha\d+|beta\d+|rc\d+))+$", "", expect_version
            )
            if expect_version.endswith(".x"):
                expect_version = expect_version[:-1]

            # For snapshots we ensure that the version in the package
            # at least *starts* with the version. This is to support
            # build_version='7.x-SNAPSHOT'.
            if not version.startswith(expect_version):
                print(
                    "Version of package (%s) didn't match the "
                    "expected release version (%s)" % (version, build_version)
                )
                exit(1)

        # A release that will be tagged, we want
        # there to be no '+dev', etc.
        elif expect_version != version:
            print(
                "Version of package (%s) didn't match the "
                "expected release version (%s)" % (version, build_version)
            )
            exit(1)

    for suffix in ("", major_version):
        run("rm", "-rf", "build/", "*.egg-info", ".eggs")

        # Rename the module to fit the suffix.
        shutil.move(
            os.path.join(base_dir, "opensearchpy"),
            os.path.join(base_dir, "opensearchpy%s" % suffix),
        )

        # Ensure that the version within 'opensearchpy/_version.py' is correct.
        version_path = os.path.join(base_dir, f"opensearchpy{suffix}/_version.py")
        with open(version_path) as f:
            version_data = f.read()
        version_data = re.sub(
            r"__versionstr__ = \"[^\"]+\"",
            '__versionstr__ = "%s"' % version,
            version_data,
        )
        with open(version_path, "w") as f:
            f.truncate()
            f.write(version_data)

        # Rewrite setup.py with the new name.
        setup_py_path = os.path.join(base_dir, "setup.py")
        with open(setup_py_path) as f:
            setup_py = f.read()
        with open(setup_py_path, "w") as f:
            f.truncate()
            assert 'package_name = "opensearch-py"' in setup_py
            f.write(
                setup_py.replace(
                    'package_name = "opensearch-py"',
                    'package_name = "opensearch-py%s"' % suffix,
                )
            )

        # Build the sdist/wheels
        run("python", "setup.py", "sdist", "bdist_wheel")

        # Clean up everything.
        run("git", "checkout", "--", "setup.py", "opensearchpy/")
        if suffix:
            run("rm", "-rf", "opensearchpy%s/" % suffix)

    # Test everything that got created
    dists = os.listdir(os.path.join(base_dir, "dist"))
    assert len(dists) == 4
    for dist in dists:
        test_dist(os.path.join(base_dir, "dist", dist))
    os.system("chmod a+w dist/*")

    # After this run 'python -m twine upload dist/*'
    print(
        "\n\n"
        "===============================\n\n"
        "    * Releases are ready! *\n\n"
        "$ python -m twine upload dist/*\n\n"
        "==============================="
    )