aboutsummaryrefslogtreecommitdiff
path: root/pw_ide
diff options
context:
space:
mode:
authorChad Norvell <chadnorvell@google.com>2023-08-24 22:28:04 +0000
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-08-24 22:28:04 +0000
commit21c317d5c139c3f72e701d87ea3a8139428531a3 (patch)
treebda332cc3a6c203ddd715d616c23414a5107e6b3 /pw_ide
parentff32f2b64b7179dd87cfe939cf13d962f035c673 (diff)
downloadpigweed-21c317d5c139c3f72e701d87ea3a8139428531a3.tar.gz
pw_ide: Add cmd to install Py packages as editable
Change-Id: I2d4e950c6456a193a7979d407389ac3daae4bfe2 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/163572 Reviewed-by: Anthony DiGirolamo <tonymd@google.com> Commit-Queue: Chad Norvell <chadnorvell@google.com>
Diffstat (limited to 'pw_ide')
-rw-r--r--pw_ide/py/pw_ide/cli.py5
-rw-r--r--pw_ide/py/pw_ide/commands.py60
2 files changed, 64 insertions, 1 deletions
diff --git a/pw_ide/py/pw_ide/cli.py b/pw_ide/py/pw_ide/cli.py
index ebd5ce64c..999de3056 100644
--- a/pw_ide/py/pw_ide/cli.py
+++ b/pw_ide/py/pw_ide/cli.py
@@ -334,6 +334,11 @@ def _build_argument_parser() -> argparse.ArgumentParser:
action='store_true',
help='print the path to the Pigweed Python virtual environment',
)
+ parser_python.add_argument(
+ '--install-editable',
+ metavar='MODULE',
+ help='install a Pigweed Python module in editable mode',
+ )
parser_vscode = add_parser(cmd_vscode, 'vscode')
parser_vscode.add_argument(
diff --git a/pw_ide/py/pw_ide/commands.py b/pw_ide/py/pw_ide/commands.py
index 0c0e427a4..087728618 100644
--- a/pw_ide/py/pw_ide/commands.py
+++ b/pw_ide/py/pw_ide/commands.py
@@ -16,6 +16,7 @@
import logging
from pathlib import Path
import shlex
+import shutil
import subprocess
import sys
from typing import cast, Dict, List, Optional, Set, Tuple
@@ -714,8 +715,51 @@ def cmd_cpp( # pylint: disable=too-many-arguments, too-many-locals, too-many-br
)
+def install_py_module_as_editable(
+ module_name: str,
+ reporter: StatusReporter,
+) -> None:
+ """Install a Pigweed Python module in editable mode."""
+ reporter.info(f'Installing {module_name} as an editable module')
+ try:
+ site_packages_path = [
+ path for path in sys.path if 'site-packages' in path
+ ][0]
+ except IndexError:
+ reporter.err(f'Could not find {module_name} in the Python path!')
+ sys.exit(1)
+
+ reporter.info(f'Found {module_name} at: {site_packages_path}')
+ shutil.rmtree(Path(site_packages_path) / module_name)
+
+ try:
+ subprocess.run(
+ [
+ 'pip',
+ 'install',
+ '--no-deps',
+ '-e',
+ f'{module_name}/py',
+ ],
+ check=True,
+ stdout=subprocess.PIPE,
+ )
+ except subprocess.CalledProcessError:
+ reporter.err(
+ [
+ f'Failed to install {module_name}!',
+ 'You may need to re-bootstrap',
+ ]
+ )
+
+ reporter.new('Success!')
+ reporter.wrn('Note that running bootstrap or building will reverse this.')
+
+
def cmd_python(
- should_print_venv: bool, reporter: StatusReporter = StatusReporter()
+ should_print_venv: bool,
+ install_editable: Optional[str] = None,
+ reporter: StatusReporter = StatusReporter(),
) -> None:
"""Configure Python code intelligence support.
@@ -725,11 +769,25 @@ def cmd_python(
.. code-block:: bash
pw ide python --venv
+
+ When working on Pigweed's Python modules, it can be convenient to install
+ them in editable mode to instantly realize code changes. You can do this by
+ running:
+
+ .. code-block:: bash
+
+ pw ide python --install-editable pw_{module name}
+
+ Just note that running bootstrap or building will override this.
"""
# If true, no arguments were provided and we should do the default
# behavior.
default = True
+ if install_editable is not None:
+ default = False
+ install_py_module_as_editable(install_editable, reporter)
+
if should_print_venv or default:
reporter.info(
[