File: cli.py

package info (click to toggle)
hatch-jupyter-builder 0.8.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: python: 1,506; javascript: 135; makefile: 23
file content (54 lines) | stat: -rw-r--r-- 1,842 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Migrate a Jupyter project from setuptools/jupyter-packaging to hatch and
hatch_jupyter_builder."""
import argparse
import logging
import os
import subprocess
import sys
import venv
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Optional

from hatch_jupyter_builder import __version__ as builder_version


def main(td, target_dir):
    """Main script."""
    logger = logging.getLogger(__name__)
    logging.basicConfig()
    venv.create(td, with_pip=True)
    python = Path(td) / "Scripts/python.exe" if os.name == "nt" else Path(td) / "bin/python"

    logger.info("Installing in temporary virtual environment...")

    # Create a virtual environment and use it to run the migration.
    runner = subprocess.check_call
    runner([python, "-m", "pip", "install", "build"])
    runner([python, "-m", "pip", "install", "packaging"])
    runner([python, "-m", "pip", "install", "tomli_w"])
    runner([python, "-m", "pip", "install", "tomli"])
    runner([python, "-m", "pip", "install", "hatch"])
    runner([python, "-m", "build", target_dir, "--sdist"])

    migrator = Path(__file__).parent / "_migrate.py"
    runner([python, migrator, builder_version], cwd=target_dir)


def make_parser(
    parser: Optional[argparse.ArgumentParser] = None, prog: Optional[str] = None
) -> argparse.ArgumentParser:
    """Make a parser object."""
    if parser is None:
        parser = argparse.ArgumentParser(prog=prog)
    parser.add_argument(dest="target_dir", help="Target Directory")
    return parser


def run(args: Optional[argparse.Namespace] = None) -> None:
    """Run the migration."""
    if args is None:
        parser = make_parser(prog=f"{sys.executable} -m hatch_jupyter_builder.migrate")
        args = parser.parse_args()
    with TemporaryDirectory() as td:
        main(td, args.target_dir)