Skip to content

Commit

Permalink
Merge pull request #238 from fcollonval/fix/python-target
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Jan 11, 2022
2 parents 642d724 + 8cfbd40 commit 86dc7d7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
16 changes: 14 additions & 2 deletions jupyter_releaser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ def main(force):
)
]

check_imports_options = [
click.option(
"--check-imports",
envvar="RH_CHECK_IMPORTS",
default=[],
multiple=True,
help="The Python packages import to check for; default to the Python package name.",
)
]

dry_run_options = [
click.option(
"--dry-run", is_flag=True, envvar="RH_DRY_RUN", help="Run as a dry run"
Expand Down Expand Up @@ -403,14 +413,16 @@ def build_python(dist_dir, python_packages):

@main.command()
@add_options(dist_dir_options)
@add_options(check_imports_options)
@use_checkout_dir()
def check_python(dist_dir):
def check_python(dist_dir, check_imports):
"""Check Python dist files"""
for dist_file in glob(f"{dist_dir}/*"):
if Path(dist_file).suffix not in [".gz", ".whl"]:
util.log(f"Skipping non-python dist file {dist_file}")
continue
python.check_dist(dist_file)

python.check_dist(dist_file, python_imports=check_imports)


@main.command()
Expand Down
23 changes: 19 additions & 4 deletions jupyter_releaser/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shlex
from glob import glob
from pathlib import Path
from subprocess import CalledProcessError
from subprocess import PIPE
from subprocess import Popen
from tempfile import TemporaryDirectory
Expand All @@ -33,16 +34,22 @@ def build_dist(dist_dir, clean=True):
util.run(f"python setup.py bdist_wheel --dist-dir {dest}", quiet=True)


def check_dist(dist_file, test_cmd=""):
def check_dist(dist_file, test_cmd="", python_imports=None):
"""Check a Python package locally (not as a cli)"""
dist_file = util.normalize_path(dist_file)
util.run(f"twine check {dist_file}")

if not test_cmd:
test_commands = []

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

# Create venvs to install dist file
# run the test command in the venv
Expand All @@ -58,7 +65,15 @@ def check_dist(dist_file, test_cmd=""):
util.run(f"python -m venv {env_path}")
util.run(f"{bin_path}/python -m pip install -q -U pip")
util.run(f"{bin_path}/pip install -q {dist_file}")
util.run(f"{bin_path}/{test_cmd}")
try:
for cmd in test_commands:
util.run(f"{bin_path}/{cmd}")
except CalledProcessError as e:
if test_cmd == "":
util.log(
'You may need to set "check_imports" to appropriate Python package names in the config file.'
)
raise e


def get_pypi_token(release_url, python_package):
Expand Down
5 changes: 5 additions & 0 deletions jupyter_releaser/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def py_multipackage(git_repo):
return testutil.create_python_package(git_repo, multi=True)


@fixture
def py_package_different_names(git_repo):
return testutil.create_python_package(git_repo, not_matching_name=True)


@fixture
def npm_package(git_repo):
return testutil.create_npm_package(git_repo)
Expand Down
13 changes: 13 additions & 0 deletions jupyter_releaser/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def test_list_envvars(runner):
branch: RH_BRANCH
cache-file: RH_CACHE_FILE
changelog-path: RH_CHANGELOG
check-imports: RH_CHECK_IMPORTS
dist-dir: RH_DIST_DIR
dry-run: RH_DRY_RUN
links-expire: RH_LINKS_EXPIRE
Expand Down Expand Up @@ -435,6 +436,18 @@ def test_check_python(py_package, runner, build_mock, git_prep):
assert "after-check-python" in log


def test_check_python_different_names(
monkeypatch, py_package_different_names, runner, build_mock, git_prep
):
monkeypatch.setenv("RH_CHECK_IMPORTS", "foobar")
runner(["build-python"])
runner(["check-python"])

log = get_log()
assert "before-check-python" in log
assert "after-check-python" in log


def test_handle_npm(npm_package, runner, git_prep):
runner(["build-npm"])

Expand Down
36 changes: 27 additions & 9 deletions jupyter_releaser/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
"""


def setup_cfg_template(package_name="foo"):
def setup_cfg_template(package_name="foo", module_name=None):
return f"""
[metadata]
name = {package_name}
version = attr: {package_name}.__version__
version = attr: {module_name or package_name}.__version__
description = My package description
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -62,7 +62,7 @@ def setup_cfg_template(package_name="foo"):
[options]
zip_safe = False
include_package_data = True
py_modules = {package_name}
py_modules = {module_name or package_name}
"""


Expand Down Expand Up @@ -171,13 +171,18 @@ def get_log():
return log.read_text(encoding="utf-8").splitlines()


def create_python_package(git_repo, multi=False):
def write_files(git_repo, sub_packages=[], package_name="foo"):
def create_python_package(git_repo, multi=False, not_matching_name=False):
def write_files(git_repo, sub_packages=[], package_name="foo", module_name=None):

module_name = module_name or package_name

setuppy = git_repo / "setup.py"
setuppy.write_text(SETUP_PY_TEMPLATE, encoding="utf-8")

setuppy = git_repo / "setup.cfg"
setuppy.write_text(setup_cfg_template(package_name), encoding="utf-8")
setuppy.write_text(
setup_cfg_template(package_name, module_name), encoding="utf-8"
)

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

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

manifest = git_repo / "MANIFEST.in"
Expand All @@ -215,11 +220,24 @@ def write_files(git_repo, sub_packages=[], package_name="foo"):
}
)
sub_package.mkdir()
write_files(git_repo / sub_package, package_name=f"foo{i}")
package_name = f"foo{i}"
module_name = f"foo{i}bar" if not_matching_name else None
write_files(
git_repo / sub_package,
package_name=package_name,
module_name=module_name,
)
run(f"git add {sub_package}")
run(f'git commit -m "initial python {sub_package}"')

write_files(git_repo, sub_packages=sub_packages)
package_name = "foo"
module_name = "foobar" if not_matching_name else None
write_files(
git_repo,
sub_packages=sub_packages,
package_name=package_name,
module_name=module_name,
)
run("git add .")
run('git commit -m "initial python package"')

Expand Down

0 comments on commit 86dc7d7

Please sign in to comment.