Skip to content

Commit d8e1e42

Browse files
committed
Add configurable python target to check-python
1 parent 642d724 commit d8e1e42

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

jupyter_releaser/cli.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ def main(force):
184184
)
185185
]
186186

187+
python_target_options = [
188+
click.option(
189+
"--python-target",
190+
envvar="RH_PYTHON_TARGET",
191+
default="",
192+
help="The Python package import to check for; default to the Python package name."
193+
)
194+
]
195+
187196
dry_run_options = [
188197
click.option(
189198
"--dry-run", is_flag=True, envvar="RH_DRY_RUN", help="Run as a dry run"
@@ -403,14 +412,16 @@ def build_python(dist_dir, python_packages):
403412

404413
@main.command()
405414
@add_options(dist_dir_options)
415+
@add_options(python_target_options)
406416
@use_checkout_dir()
407-
def check_python(dist_dir):
417+
def check_python(dist_dir, python_target):
408418
"""Check Python dist files"""
409419
for dist_file in glob(f"{dist_dir}/*"):
410420
if Path(dist_file).suffix not in [".gz", ".whl"]:
411421
util.log(f"Skipping non-python dist file {dist_file}")
412422
continue
413-
python.check_dist(dist_file)
423+
test_cmd = f'python -c "import {python_target}"' if python_target else ""
424+
python.check_dist(dist_file, test_cmd)
414425

415426

416427
@main.command()

jupyter_releaser/python.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import shlex
88
from glob import glob
99
from pathlib import Path
10-
from subprocess import PIPE
10+
from subprocess import PIPE, CalledProcessError
1111
from subprocess import Popen
1212
from tempfile import TemporaryDirectory
1313

@@ -58,7 +58,12 @@ def check_dist(dist_file, test_cmd=""):
5858
util.run(f"python -m venv {env_path}")
5959
util.run(f"{bin_path}/python -m pip install -q -U pip")
6060
util.run(f"{bin_path}/pip install -q {dist_file}")
61-
util.run(f"{bin_path}/{test_cmd}")
61+
try:
62+
util.run(f"{bin_path}/{test_cmd}")
63+
except CalledProcessError as e:
64+
if test_cmd == "":
65+
util.log('You may need to set "python_target" to an appropriate Python package name in the config file.')
66+
raise e
6267

6368

6469
def get_pypi_token(release_url, python_package):

jupyter_releaser/tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ def py_multipackage(git_repo):
8080
return testutil.create_python_package(git_repo, multi=True)
8181

8282

83+
@fixture
84+
def py_package_different_names(git_repo):
85+
return testutil.create_python_package(git_repo, not_matching_name=True)
86+
87+
8388
@fixture
8489
def npm_package(git_repo):
8590
return testutil.create_npm_package(git_repo)

jupyter_releaser/tests/test_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ def test_check_python(py_package, runner, build_mock, git_prep):
435435
assert "after-check-python" in log
436436

437437

438+
def test_check_python_different_names(monkeypatch, py_package_different_names, runner, build_mock, git_prep):
439+
monkeypatch.setenv("RH_PYTHON_TARGET", "foobar")
440+
runner(["build-python"])
441+
runner(["check-python"])
442+
443+
log = get_log()
444+
assert "before-check-python" in log
445+
assert "after-check-python" in log
446+
447+
438448
def test_handle_npm(npm_package, runner, git_prep):
439449
runner(["build-npm"])
440450

jupyter_releaser/tests/util.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
"""
4747

4848

49-
def setup_cfg_template(package_name="foo"):
49+
def setup_cfg_template(package_name="foo", module_name=None):
5050
return f"""
5151
[metadata]
5252
name = {package_name}
53-
version = attr: {package_name}.__version__
53+
version = attr: {module_name or package_name}.__version__
5454
description = My package description
5555
long_description = file: README.md
5656
long_description_content_type = text/markdown
@@ -62,7 +62,7 @@ def setup_cfg_template(package_name="foo"):
6262
[options]
6363
zip_safe = False
6464
include_package_data = True
65-
py_modules = {package_name}
65+
py_modules = {module_name or package_name}
6666
"""
6767

6868

@@ -171,13 +171,16 @@ def get_log():
171171
return log.read_text(encoding="utf-8").splitlines()
172172

173173

174-
def create_python_package(git_repo, multi=False):
175-
def write_files(git_repo, sub_packages=[], package_name="foo"):
174+
def create_python_package(git_repo, multi=False, not_matching_name=False):
175+
def write_files(git_repo, sub_packages=[], package_name="foo", module_name=None):
176+
177+
module_name = module_name or package_name
178+
176179
setuppy = git_repo / "setup.py"
177180
setuppy.write_text(SETUP_PY_TEMPLATE, encoding="utf-8")
178181

179182
setuppy = git_repo / "setup.cfg"
180-
setuppy.write_text(setup_cfg_template(package_name), encoding="utf-8")
183+
setuppy.write_text(setup_cfg_template(package_name, module_name), encoding="utf-8")
181184

182185
tbump = git_repo / "tbump.toml"
183186
tbump.write_text(
@@ -188,7 +191,7 @@ def write_files(git_repo, sub_packages=[], package_name="foo"):
188191
pyproject = git_repo / "pyproject.toml"
189192
pyproject.write_text(pyproject_template(sub_packages), encoding="utf-8")
190193

191-
foopy = git_repo / f"{package_name}.py"
194+
foopy = git_repo / f"{module_name}.py"
192195
foopy.write_text(PY_MODULE_TEMPLATE, encoding="utf-8")
193196

194197
manifest = git_repo / "MANIFEST.in"
@@ -215,11 +218,24 @@ def write_files(git_repo, sub_packages=[], package_name="foo"):
215218
}
216219
)
217220
sub_package.mkdir()
218-
write_files(git_repo / sub_package, package_name=f"foo{i}")
221+
package_name = f"foo{i}"
222+
module_name = f"foo{i}bar" if not_matching_name else None
223+
write_files(
224+
git_repo / sub_package,
225+
package_name=package_name,
226+
module_name=module_name
227+
)
219228
run(f"git add {sub_package}")
220229
run(f'git commit -m "initial python {sub_package}"')
221230

222-
write_files(git_repo, sub_packages=sub_packages)
231+
package_name = "foo"
232+
module_name = "foobar" if not_matching_name else None
233+
write_files(
234+
git_repo,
235+
sub_packages=sub_packages,
236+
package_name=package_name,
237+
module_name=module_name
238+
)
223239
run("git add .")
224240
run('git commit -m "initial python package"')
225241

0 commit comments

Comments
 (0)