Skip to content

Commit 86dc7d7

Browse files
authored
Merge pull request #238 from fcollonval/fix/python-target
2 parents 642d724 + 8cfbd40 commit 86dc7d7

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

jupyter_releaser/cli.py

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

187+
check_imports_options = [
188+
click.option(
189+
"--check-imports",
190+
envvar="RH_CHECK_IMPORTS",
191+
default=[],
192+
multiple=True,
193+
help="The Python packages import to check for; default to the Python package name.",
194+
)
195+
]
196+
187197
dry_run_options = [
188198
click.option(
189199
"--dry-run", is_flag=True, envvar="RH_DRY_RUN", help="Run as a dry run"
@@ -403,14 +413,16 @@ def build_python(dist_dir, python_packages):
403413

404414
@main.command()
405415
@add_options(dist_dir_options)
416+
@add_options(check_imports_options)
406417
@use_checkout_dir()
407-
def check_python(dist_dir):
418+
def check_python(dist_dir, check_imports):
408419
"""Check Python dist files"""
409420
for dist_file in glob(f"{dist_dir}/*"):
410421
if Path(dist_file).suffix not in [".gz", ".whl"]:
411422
util.log(f"Skipping non-python dist file {dist_file}")
412423
continue
413-
python.check_dist(dist_file)
424+
425+
python.check_dist(dist_file, python_imports=check_imports)
414426

415427

416428
@main.command()

jupyter_releaser/python.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shlex
88
from glob import glob
99
from pathlib import Path
10+
from subprocess import CalledProcessError
1011
from subprocess import PIPE
1112
from subprocess import Popen
1213
from tempfile import TemporaryDirectory
@@ -33,16 +34,22 @@ def build_dist(dist_dir, clean=True):
3334
util.run(f"python setup.py bdist_wheel --dist-dir {dest}", quiet=True)
3435

3536

36-
def check_dist(dist_file, test_cmd=""):
37+
def check_dist(dist_file, test_cmd="", python_imports=None):
3738
"""Check a Python package locally (not as a cli)"""
3839
dist_file = util.normalize_path(dist_file)
3940
util.run(f"twine check {dist_file}")
4041

41-
if not test_cmd:
42+
test_commands = []
43+
44+
if test_cmd:
45+
test_commands.append(test_cmd)
46+
elif python_imports is not None:
47+
test_commands.extend([f'python -c "import {name}"' for name in python_imports])
48+
else:
4249
# Get the package name from the dist file name
4350
name = re.match(r"(\S+)-\d", osp.basename(dist_file)).groups()[0]
4451
name = name.replace("-", "_")
45-
test_cmd = f'python -c "import {name}"'
52+
test_commands.append(f'python -c "import {name}"')
4653

4754
# Create venvs to install dist file
4855
# run the test command in the venv
@@ -58,7 +65,15 @@ def check_dist(dist_file, test_cmd=""):
5865
util.run(f"python -m venv {env_path}")
5966
util.run(f"{bin_path}/python -m pip install -q -U pip")
6067
util.run(f"{bin_path}/pip install -q {dist_file}")
61-
util.run(f"{bin_path}/{test_cmd}")
68+
try:
69+
for cmd in test_commands:
70+
util.run(f"{bin_path}/{cmd}")
71+
except CalledProcessError as e:
72+
if test_cmd == "":
73+
util.log(
74+
'You may need to set "check_imports" to appropriate Python package names in the config file.'
75+
)
76+
raise e
6277

6378

6479
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def test_list_envvars(runner):
152152
branch: RH_BRANCH
153153
cache-file: RH_CACHE_FILE
154154
changelog-path: RH_CHANGELOG
155+
check-imports: RH_CHECK_IMPORTS
155156
dist-dir: RH_DIST_DIR
156157
dry-run: RH_DRY_RUN
157158
links-expire: RH_LINKS_EXPIRE
@@ -435,6 +436,18 @@ def test_check_python(py_package, runner, build_mock, git_prep):
435436
assert "after-check-python" in log
436437

437438

439+
def test_check_python_different_names(
440+
monkeypatch, py_package_different_names, runner, build_mock, git_prep
441+
):
442+
monkeypatch.setenv("RH_CHECK_IMPORTS", "foobar")
443+
runner(["build-python"])
444+
runner(["check-python"])
445+
446+
log = get_log()
447+
assert "before-check-python" in log
448+
assert "after-check-python" in log
449+
450+
438451
def test_handle_npm(npm_package, runner, git_prep):
439452
runner(["build-npm"])
440453

jupyter_releaser/tests/util.py

Lines changed: 27 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,18 @@ 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(
184+
setup_cfg_template(package_name, module_name), encoding="utf-8"
185+
)
181186

182187
tbump = git_repo / "tbump.toml"
183188
tbump.write_text(
@@ -188,7 +193,7 @@ def write_files(git_repo, sub_packages=[], package_name="foo"):
188193
pyproject = git_repo / "pyproject.toml"
189194
pyproject.write_text(pyproject_template(sub_packages), encoding="utf-8")
190195

191-
foopy = git_repo / f"{package_name}.py"
196+
foopy = git_repo / f"{module_name}.py"
192197
foopy.write_text(PY_MODULE_TEMPLATE, encoding="utf-8")
193198

194199
manifest = git_repo / "MANIFEST.in"
@@ -215,11 +220,24 @@ def write_files(git_repo, sub_packages=[], package_name="foo"):
215220
}
216221
)
217222
sub_package.mkdir()
218-
write_files(git_repo / sub_package, package_name=f"foo{i}")
223+
package_name = f"foo{i}"
224+
module_name = f"foo{i}bar" if not_matching_name else None
225+
write_files(
226+
git_repo / sub_package,
227+
package_name=package_name,
228+
module_name=module_name,
229+
)
219230
run(f"git add {sub_package}")
220231
run(f'git commit -m "initial python {sub_package}"')
221232

222-
write_files(git_repo, sub_packages=sub_packages)
233+
package_name = "foo"
234+
module_name = "foobar" if not_matching_name else None
235+
write_files(
236+
git_repo,
237+
sub_packages=sub_packages,
238+
package_name=package_name,
239+
module_name=module_name,
240+
)
223241
run("git add .")
224242
run('git commit -m "initial python package"')
225243

0 commit comments

Comments
 (0)