|
| 1 | +"""Command-line interface for juliapkg.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +from .deps import STATE, add, resolve, rm, status, update |
| 8 | + |
| 9 | +try: |
| 10 | + import click |
| 11 | +except ImportError: |
| 12 | + click = None |
| 13 | + |
| 14 | +if click is None: |
| 15 | + |
| 16 | + def cli(): |
| 17 | + raise ImportError( |
| 18 | + "`click` is required to use the juliapkg CLI. " |
| 19 | + "Please install it with `pip install click` or " |
| 20 | + '`pip install "pyjuliapkg[cli]".' |
| 21 | + ) |
| 22 | + |
| 23 | +else: |
| 24 | + |
| 25 | + class JuliaPkgGroup(click.Group): |
| 26 | + """Custom group to avoid long stacktraces when Julia exits with an error.""" |
| 27 | + |
| 28 | + @property |
| 29 | + def always_show_python_error(self) -> bool: |
| 30 | + return ( |
| 31 | + os.environ.get("PYTHON_JULIAPKG_CLI_ALWAYS_SHOW_PYTHON_ERROR", "0") |
| 32 | + == "1" |
| 33 | + ) |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def _is_graceful_exit(e: subprocess.CalledProcessError) -> bool: |
| 37 | + """Try to guess if a CalledProcessError was Julia gracefully exiting.""" |
| 38 | + return e.returncode == 1 |
| 39 | + |
| 40 | + def invoke(self, ctx): |
| 41 | + try: |
| 42 | + return super().invoke(ctx) |
| 43 | + except subprocess.CalledProcessError as e: |
| 44 | + # Julia already printed an error message |
| 45 | + if ( |
| 46 | + JuliaPkgGroup._is_graceful_exit(e) |
| 47 | + and not self.always_show_python_error |
| 48 | + ): |
| 49 | + click.get_current_context().exit(1) |
| 50 | + else: |
| 51 | + raise |
| 52 | + |
| 53 | + cli = JuliaPkgGroup(help="JuliaPkg - Manage your Julia dependencies from Python.") |
| 54 | + |
| 55 | + @cli.command(name="add") |
| 56 | + @click.argument("package") |
| 57 | + @click.option("--uuid", required=True, help="UUID of the package") |
| 58 | + @click.option("--version", help="Version constraint") |
| 59 | + @click.option("--dev", is_flag=True, help="Add as development dependency") |
| 60 | + @click.option("--path", help="Local path to package") |
| 61 | + @click.option("--subdir", help="Subdirectory within the package") |
| 62 | + @click.option("--url", help="Git URL for the package") |
| 63 | + @click.option("--rev", help="Git revision/branch/tag") |
| 64 | + @click.option("--target", help="Target environment") |
| 65 | + def add_cli(package, uuid, version, dev, path, subdir, url, rev, target): |
| 66 | + """Add a Julia package to the project.""" |
| 67 | + add( |
| 68 | + package, |
| 69 | + uuid=uuid, |
| 70 | + version=version, |
| 71 | + dev=dev, |
| 72 | + path=path, |
| 73 | + subdir=subdir, |
| 74 | + url=url, |
| 75 | + rev=rev, |
| 76 | + target=target, |
| 77 | + ) |
| 78 | + click.echo(f"Queued addition of {package}. Run `resolve` to apply changes.") |
| 79 | + |
| 80 | + @cli.command(name="resolve") |
| 81 | + @click.option("--force", is_flag=True, help="Force resolution") |
| 82 | + @click.option("--dry-run", is_flag=True, help="Dry run (don't actually install)") |
| 83 | + @click.option("--update", is_flag=True, help="Update dependencies") |
| 84 | + def resolve_cli(force, dry_run, update): |
| 85 | + """Resolve and install Julia dependencies.""" |
| 86 | + resolve(force=force, dry_run=dry_run, update=update) |
| 87 | + click.echo("Resolved dependencies.") |
| 88 | + |
| 89 | + @cli.command(name="remove") |
| 90 | + @click.argument("package") |
| 91 | + @click.option("--target", help="Target environment") |
| 92 | + def remove_cli(package, target): |
| 93 | + """Remove a Julia package from the project.""" |
| 94 | + rm(package, target=target) |
| 95 | + click.echo(f"Queued removal of {package}. Run `resolve` to apply changes.") |
| 96 | + |
| 97 | + @cli.command(name="status") |
| 98 | + @click.option("--target", help="Target environment") |
| 99 | + def status_cli(target): |
| 100 | + """Show the status of Julia packages in the project.""" |
| 101 | + status(target=target) |
| 102 | + |
| 103 | + @cli.command(name="update") |
| 104 | + @click.option("--dry-run", is_flag=True, help="Dry run (don't actually install)") |
| 105 | + def update_cli(dry_run): |
| 106 | + """Update Julia packages in the project.""" |
| 107 | + update(dry_run=dry_run) |
| 108 | + |
| 109 | + @cli.command(name="run", context_settings=dict(ignore_unknown_options=True)) |
| 110 | + @click.argument("args", nargs=-1) |
| 111 | + def run_cli(args): |
| 112 | + """Pass-through to Julia CLI. |
| 113 | +
|
| 114 | + For example, use `run` to launch a REPL or `run script.jl` to run a script. |
| 115 | + """ |
| 116 | + resolve() |
| 117 | + executable = STATE["executable"] |
| 118 | + project = STATE["project"] |
| 119 | + |
| 120 | + env = os.environ.copy() |
| 121 | + if sys.executable: |
| 122 | + # prefer PythonCall to use the current Python executable |
| 123 | + # TODO: this is a hack, it would be better for PythonCall to detect that |
| 124 | + # Julia is being called from Python |
| 125 | + env.setdefault("JULIA_PYTHONCALL_EXE", sys.executable) |
| 126 | + cmd = [ |
| 127 | + executable, |
| 128 | + "--project=" + project, |
| 129 | + ] |
| 130 | + for arg in args: |
| 131 | + if arg.startswith("--project"): |
| 132 | + raise ValueError("Do not specify --project when using pyjuliapkg.") |
| 133 | + cmd.append(arg) |
| 134 | + subprocess.run( |
| 135 | + cmd, |
| 136 | + check=True, |
| 137 | + env=env, |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + cli() |
0 commit comments