Skip to content

Commit

Permalink
Add python_packages option
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Oct 8, 2021
1 parent 03defec commit dab7443
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 15 deletions.
34 changes: 27 additions & 7 deletions jupyter_releaser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ def main(force):
)
]

python_packages_options = [
click.option(
"--python-packages",
envvar="RH_PYTHON_PACKAGES",
default=["."],
multiple=True,
help="The list of paths to Python packages",
)
]

dry_run_options = [
click.option(
"--dry-run", is_flag=True, envvar="RH_DRY_RUN", help="Run as a dry run"
Expand Down Expand Up @@ -273,10 +283,15 @@ def prep_git(ref, branch, repo, auth, username, git_url):
@main.command()
@add_options(version_spec_options)
@add_options(version_cmd_options)
@add_options(python_packages_options)
@use_checkout_dir()
def bump_version(version_spec, version_cmd):
def bump_version(version_spec, version_cmd, python_packages):
"""Prep git and env variables and bump version"""
lib.bump_version(version_spec, version_cmd)
prev_dir = os.getcwd()
for python_package in python_packages:
os.chdir(python_package)
lib.bump_version(version_spec, version_cmd)
os.chdir(prev_dir)


@main.command()
Expand Down Expand Up @@ -363,13 +378,18 @@ def check_changelog(

@main.command()
@add_options(dist_dir_options)
@add_options(python_packages_options)
@use_checkout_dir()
def build_python(dist_dir):
def build_python(dist_dir, python_packages):
"""Build Python dist files"""
if not util.PYPROJECT.exists() and not util.SETUP_PY.exists():
util.log("Skipping build-python since there are no python package files")
return
python.build_dist(dist_dir)
prev_dir = os.getcwd()
for python_package in python_packages:
os.chdir(python_package)
if not util.PYPROJECT.exists() and not util.SETUP_PY.exists():
util.log(f"Skipping build-python in {python_package} since there are no python package files")
else:
python.build_dist(Path(os.path.relpath(".", python_package)) / dist_dir)
os.chdir(prev_dir)


@main.command()
Expand Down
54 changes: 54 additions & 0 deletions jupyter_releaser/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def test_list_envvars(runner):
output: RH_CHANGELOG_OUTPUT
post-version-message: RH_POST_VERSION_MESSAGE
post-version-spec: RH_POST_VERSION_SPEC
python-packages: RH_PYTHON_PACKAGES
ref: RH_REF
release-message: RH_RELEASE_MESSAGE
repo: RH_REPOSITORY
Expand Down Expand Up @@ -596,6 +597,59 @@ def helper(path, **kwargs):
assert "after-extract-release" in log


@pytest.mark.skipif(
os.name == "nt" and sys.version_info.major == 3 and sys.version_info.minor < 8,
reason="See https://bugs.python.org/issue26660",
)
def test_extract_dist_multipy(py_multipackage, runner, mocker, open_mock, tmp_path, git_prep):
git_repo = py_multipackage[0]["abs_path"]
changelog_entry = mock_changelog_entry(git_repo, runner, mocker)

# Create the dist files
dist_dir = Path(util.CHECKOUT_NAME).resolve() / "dist"
for package in py_multipackage:
run(f"python -m build . -o {dist_dir}", cwd=Path(util.CHECKOUT_NAME) / package['rel_path'])

# Finalize the release
runner(["tag-release"])

os.makedirs("staging")
shutil.move(f"{util.CHECKOUT_NAME}/dist", "staging")

def helper(path, **kwargs):
return MockRequestResponse(f"{git_repo}/staging/dist/{path}")

get_mock = mocker.patch("requests.get", side_effect=helper)

tag_name = f"v{VERSION_SPEC}"

dist_names = [osp.basename(f) for f in glob("staging/dist/*.*")]
releases = [
dict(
tag_name=tag_name,
target_commitish=util.get_branch(),
assets=[dict(name=dist_name, url=dist_name) for dist_name in dist_names],
)
]
sha = run("git rev-parse HEAD", cwd=util.CHECKOUT_NAME)

tags = [dict(ref=f"refs/tags/{tag_name}", object=dict(sha=sha))]
url = normalize_path(osp.join(os.getcwd(), util.CHECKOUT_NAME))
open_mock.side_effect = [
MockHTTPResponse(releases),
MockHTTPResponse(tags),
MockHTTPResponse(dict(html_url=url)),
]

runner(["extract-release", HTML_URL])
assert len(open_mock.mock_calls) == 2
assert len(get_mock.mock_calls) == len(dist_names) == 2 * len(py_multipackage)

log = get_log()
assert "before-extract-release" not in log
assert "after-extract-release" in log


@pytest.mark.skipif(
os.name == "nt" and sys.version_info.major == 3 and sys.version_info.minor < 8,
reason="See https://bugs.python.org/issue26660",
Expand Down
4 changes: 2 additions & 2 deletions jupyter_releaser/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def test_get_version_python(py_package):
assert util.get_version() == "0.0.2a0"


def test_get_versions_python(py_multipackage):
def test_get_version_multipython(py_multipackage):
for package in py_multipackage:
prev_dir = os.getcwd()
os.chdir(package["path"])
os.chdir(package["rel_path"])
assert util.get_version() == package["version"]
new_version = package["version"].split(".")
new_version[-1] = str(int(new_version[-1]) + 1) + "a0"
Expand Down
21 changes: 15 additions & 6 deletions jupyter_releaser/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ def setup_cfg_template(package_name="foo"):
SETUP_PY_TEMPLATE = """__import__("setuptools").setup()\n"""


PYPROJECT_TEMPLATE = """
def pyproject_template(sub_packages=[]):
res = """
[build-system]
requires = ["setuptools>=40.8.0", "wheel"]
build-backend = "setuptools.build_meta"
"""
if sub_packages:
res += f"""
[tools.jupyter-releaser.options]
python_packages = {sub_packages}
"""
return res


def py_module_template(version="0.0.1"):
Expand Down Expand Up @@ -167,7 +174,7 @@ def get_log():


def create_python_package(git_repo, multi=False):
def write_files(git_repo, package_name="foo", version="0.0.1"):
def write_files(git_repo, sub_packages=[], package_name="foo", version="0.0.1"):
setuppy = git_repo / "setup.py"
setuppy.write_text(SETUP_PY_TEMPLATE, encoding="utf-8")

Expand All @@ -181,7 +188,7 @@ def write_files(git_repo, package_name="foo", version="0.0.1"):
)

pyproject = git_repo / "pyproject.toml"
pyproject.write_text(PYPROJECT_TEMPLATE, encoding="utf-8")
pyproject.write_text(pyproject_template(sub_packages), encoding="utf-8")

foopy = git_repo / f"{package_name}.py"
foopy.write_text(py_module_template(version), encoding="utf-8")
Expand All @@ -197,18 +204,20 @@ def write_files(git_repo, package_name="foo", version="0.0.1"):
pre_commit = git_repo / ".pre-commit-config.yaml"
pre_commit.write_text(text, encoding="utf-8")

sub_packages = []
if multi:
packages = [{"path": ".", "version": "0.0.1"}]
packages = [{"abs_path": git_repo, "rel_path": ".", "version": "0.0.1"}]
for i in range(2):
sub_package = Path(f"sub_package{i}")
sub_packages.append(str(sub_package))
version = f"0.0.{i+2}"
packages.append({"path": sub_package, "version": version})
packages.append({"abs_path": git_repo / sub_package, "rel_path": sub_package, "version": version})
sub_package.mkdir()
write_files(git_repo / sub_package, package_name=f"foo{i}", version=version)
run(f"git add {sub_package}")
run(f'git commit -m "initial python {sub_package}"')

write_files(git_repo)
write_files(git_repo, sub_packages=sub_packages)
run("git add .")
run('git commit -m "initial python package"')

Expand Down

0 comments on commit dab7443

Please sign in to comment.