Skip to content

Updated build-dists.py to work with pyproject.toml #83

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

Merged
merged 3 commits into from
Oct 11, 2024
Merged
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
51 changes: 28 additions & 23 deletions utils/build-dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

"""A command line tool for building and verifying releases
Can be used for building both 'elasticsearch' and 'elasticsearchX' dists.
Only requires 'name' in 'setup.py' and the directory to be changed.
Only requires 'name' in 'pyproject.toml' and the directory to be changed.
"""

import contextlib
Expand Down Expand Up @@ -50,7 +50,7 @@ def run(*argv, expect_exit_code=0):
else:
os.chdir(tmp_dir)

cmd = " ".join(shlex.quote(x) for x in argv)
cmd = shlex.join(argv)
print("$ " + cmd)
exit_code = os.system(cmd)
if exit_code != expect_exit_code:
Expand All @@ -65,7 +65,9 @@ def run(*argv, expect_exit_code=0):

def test_dist(dist):
with set_tmp_dir() as tmp_dir:
dist_name = re.match(r"^(elasticsearch\d*)-", os.path.basename(dist)).group(1)
dist_name = re.match(
r"^(elasticsearch_serverless\d*)-", os.path.basename(dist)
).group(1)

# Build the venv and install the dist
run("python", "-m", "venv", os.path.join(tmp_dir, "venv"))
Expand All @@ -80,6 +82,7 @@ def test_dist(dist):
"mypy",
"numpy",
"pandas-stubs",
"opentelemetry-api",
)
run(venv_python, "-m", "pip", "install", dist)

Expand Down Expand Up @@ -120,14 +123,15 @@ def test_dist(dist):
"--strict",
"--install-types",
"--non-interactive",
"--ignore-missing-imports",
os.path.join(
base_dir, "test_elasticsearch_serverless/test_types/async_types.py"
),
)

# Ensure that the namespaces are correct for the dist
for suffix in ("", "1", "2", "5", "6", "7", "8", "9", "10"):
distx_name = f"elasticsearch{suffix}"
distx_name = f"elasticsearch_serverless{suffix}"
run(
venv_python,
"-c",
Expand All @@ -136,7 +140,7 @@ def test_dist(dist):
)

# Check that sync types work for 'elasticsearch_serverless' and
# that aliased types work for 'elasticsearchX'
# that aliased types work for 'elasticsearch_serverlessX'
if dist_name == "elasticsearch_serverless":
run(
venv_python,
Expand All @@ -145,6 +149,7 @@ def test_dist(dist):
"--strict",
"--install-types",
"--non-interactive",
"--ignore-missing-imports",
os.path.join(
base_dir, "test_elasticsearch_serverless/test_types/sync_types.py"
),
Expand Down Expand Up @@ -174,16 +179,16 @@ def test_dist(dist):


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

# Grab the major version to be used as a suffix.
version_path = os.path.join(base_dir, "elasticsearch_serverless/_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]
# 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
Expand Down Expand Up @@ -231,17 +236,19 @@ def main():
)
exit(1)

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

# Rename the module to fit the suffix.
shutil.move(
os.path.join(base_dir, "elasticsearch_serverless"),
os.path.join(base_dir, f"elasticsearch{suffix}"),
os.path.join(base_dir, f"elasticsearch_serverless{suffix}"),
)

# Ensure that the version within 'elasticsearch_serverless/_version.py' is correct.
version_path = os.path.join(base_dir, f"elasticsearch{suffix}/_version.py")
version_path = os.path.join(
base_dir, f"elasticsearch_serverless{suffix}/_version.py"
)
with open(version_path) as f:
version_data = f.read()
version_data = re.sub(
Expand All @@ -253,31 +260,29 @@ def main():
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:
# Rewrite pyproject.toml with the new name.
pyproject_toml_path = os.path.join(base_dir, "pyproject.toml")
with open(pyproject_toml_path) as f:
pyproject_toml = f.read()
with open(pyproject_toml_path, "w") as f:
f.truncate()
assert 'package_name = "elasticsearch_serverless"' in setup_py
f.write(
setup_py.replace(
'package_name = "elasticsearch_serverless"',
f'package_name = "elasticsearch{suffix}"',
pyproject_toml.replace(
"elasticsearch_serverless", f"elasticsearch_serverless{suffix}"
)
)

# Build the sdist/wheels
run("python", "-m", "build")

# Clean up everything.
run("git", "checkout", "--", "setup.py", "elasticsearch_serverless/")
run("git", "checkout", "--", "pyproject.toml", "elasticsearch_serverless/")
if suffix:
run("rm", "-rf", f"elasticsearch{suffix}/")
run("rm", "-rf", f"elasticsearch_serverless{suffix}/")

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