Skip to content

Add a CLI subcommand to install Clojure Jar files in the virtualenv #1243

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added the `basilisp.url` namespace for structured URL manipulation (#1239)
* Added support for proxies (#425)
* Added a `:slots` meta flag for `deftype` to disable creation of `__slots__` on created types (#1241)
* Added a CLI subcommand `install-jar` to install a Clojure `*.jar` file into the virtualenv (#668)

### Changed
* Removed implicit support for single-use iterables in sequences, and introduced `iterator-seq` to expliciltly handle them (#1192)
Expand Down
28 changes: 27 additions & 1 deletion src/basilisp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import os
import pathlib
import sys
import sysconfig
import textwrap
import types
from collections.abc import Sequence
from pathlib import Path
from typing import Any, Callable, Optional, Union

from basilisp import main as basilisp
from basilisp.contrib import jarutil
from basilisp.lang import compiler as compiler
from basilisp.lang import keyword as kw
from basilisp.lang import list as llist
Expand Down Expand Up @@ -90,7 +92,7 @@ def prepend_once(path: str) -> None:
sys.path.insert(0, path)

for pth in args.include_path or []:
p = pathlib.Path(pth).resolve()
p = pathlib.Path(pth).expanduser().resolve()
prepend_once(str(p))

if args.include_unsafe_path:
Expand Down Expand Up @@ -478,6 +480,29 @@ def _add_bootstrap_subcommand(parser: argparse.ArgumentParser) -> None:
)


def install_jar(_, args: argparse.Namespace) -> None:
jars = args.jars
site_packages = sysconfig.get_paths()["purelib"]

for jar in jars:
jarutil.install_jar(Path(site_packages), Path(jar))


@_subcommand(
"install-jar",
help="install a JAR file into the virtualenv",
description=textwrap.dedent(
"""Install a JAR file from your local Maven repository into the current
virtualenv."""
),
handler=install_jar,
)
def _add_install_jar_subcommand(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"jars", nargs="*", help="one or more JAR files from a local filesystem location"
)


def nrepl_server(
_,
args: argparse.Namespace,
Expand Down Expand Up @@ -818,6 +843,7 @@ def invoke_cli(args: Optional[Sequence[str]] = None) -> None:

subparsers = parser.add_subparsers(help="sub-commands")
_add_bootstrap_subcommand(subparsers)
_add_install_jar_subcommand(subparsers)
_add_nrepl_server_subcommand(subparsers)
_add_repl_subcommand(subparsers)
_add_run_subcommand(subparsers)
Expand Down
23 changes: 23 additions & 0 deletions src/basilisp/contrib/jarutil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import shutil
from pathlib import Path
from tempfile import TemporaryDirectory
from zipfile import ZipFile


def install_jar(site_packages: Path, source_jar: Path) -> None:
if source_jar.suffix != ".jar":
raise ValueError(f"Expected a *.jar file; got *{source_jar.suffix}")

with TemporaryDirectory() as tmpdir, ZipFile(source_jar, mode="r") as zf:
zf.extractall(
path=tmpdir,
members=[mem for mem in zf.namelist() if not mem.startswith("META-INF")],
)
for p in Path(tmpdir).iterdir():
if p.is_dir():
destination = site_packages / p.name
d = shutil.copytree(p, destination)
print(f"Created {d}")
else:
f = shutil.copy(p, site_packages)
print(f"Created {f}")