Skip to content

Commit

Permalink
Merge pull request #5 from cidrblock/fix_formatting
Browse files Browse the repository at this point in the history
Fix some formatting
  • Loading branch information
cidrblock authored Aug 22, 2023
2 parents 98d9804 + 18fb343 commit ccfb2c0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bindep
bindir
bthornto
fileh
levelname
Expand Down
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ repos:
name: Spell check with cspell

- repo: https://github.com/jsh9/pydoclint
rev: 0.1.5
rev: 0.2.1
hooks:
- id: pydoclint
args:
Expand All @@ -88,6 +88,7 @@ repos:
- pytest
- tox
- types-PyYAML
- types-setuptools
args:
- src
- tests
Expand Down
1 change: 1 addition & 0 deletions src/pip4a/_version.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: str
14 changes: 12 additions & 2 deletions src/pip4a/arg_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
"""Parse the command line arguments."""
import argparse

from ._version import __version__

try:
from ._version import version as __version__
except ImportError: # pragma: no cover
try:
import pkg_resources

__version__ = pkg_resources.get_distribution("pip4a").version
except Exception: # pylint: disable=broad-except # noqa: BLE001
# this is the fallback SemVer version picked by setuptools_scm when tag
# information is not available.
__version__ = "0.1.dev1"


def parse() -> argparse.Namespace:
Expand Down Expand Up @@ -45,7 +56,6 @@ def parse() -> argparse.Namespace:
help="Target virtual environment.",
)


install_usage = """Usage:
pip4a install .
pip4a install -e .
Expand Down
30 changes: 26 additions & 4 deletions src/pip4a/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

logger = logging.getLogger(__name__)


class Base:
"""A base class for pip4a."""

Expand All @@ -30,8 +31,12 @@ def __init__(self: Base, app: App) -> None:
self.bindir: Path
self.interpreter: Path
self.site_pkg_path: Path
self.python_path: Path

def _set_interpreter(self: Base, create: bool=False) -> None: # noqa: FBT001,FBT002
def _set_interpreter( # noqa: PLR0912
self: Base,
create: bool = False, # noqa: FBT001, FBT002
) -> None:
"""Set the interpreter."""
venv = None
if self.app.args.venv:
Expand Down Expand Up @@ -79,6 +84,23 @@ def _set_interpreter(self: Base, create: bool=False) -> None: # noqa: FBT001,FBT
msg = f"Using current interpreter: {self.interpreter}"
logger.info(msg)

command = "python -c 'import sys; print(sys.executable)'"
msg = f"Running command: {command}"
logger.debug(msg)
try:
proc = subprocess.run(
command,
check=True,
capture_output=True,
shell=True, # noqa: S602
text=True,
)
except subprocess.CalledProcessError as exc:
err = f"Failed to find interpreter in use: {exc}"
logger.critical(err)

self.python_path = Path(proc.stdout.strip())

def _set_bindir(self: Base) -> None:
"""Set the bindir."""
self.bindir = self.interpreter.parent
Expand All @@ -89,9 +111,9 @@ def _set_bindir(self: Base) -> None:
def _set_site_pkg_path(self: Base) -> None:
"""USe the interpreter to find the site packages path."""
command = (
f"{self.interpreter} -c"
" 'import json,site; print(json.dumps(site.getsitepackages()))'"
)
f"{self.interpreter} -c"
" 'import json,site; print(json.dumps(site.getsitepackages()))'"
)
msg = f"Running command: {command}"
logger.debug(msg)
try:
Expand Down
15 changes: 5 additions & 10 deletions src/pip4a/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import shutil
import subprocess
import sys

from pathlib import Path

Expand Down Expand Up @@ -39,7 +38,7 @@ def run(self: Installer) -> None:
self._swap_editable_collection()
self._check_bindep()

if self.app.args.venv and (Path(sys.executable).parent != self.interpreter.parent):
if self.app.args.venv and (self.python_path != self.interpreter):
msg = "A virtual environment was specified but has not been activated."
logger.warning(msg)
msg = (
Expand Down Expand Up @@ -109,10 +108,11 @@ def _install_collection(self: Installer) -> None:
f"{C.COLLECTION_BUILD_DIR}, found {len(built)}"
)
raise RuntimeError(err)
tarball = built[0]
built[0]

command = (
f"{self.bindir / 'ansible-galaxy'} collection install {tarball} -p {self.site_pkg_path}"
f"{self.bindir / 'ansible-galaxy'} collection"
" install {tarball} -p {self.site_pkg_path}"
)
env = os.environ
if not self.app.args.verbose:
Expand All @@ -133,13 +133,8 @@ def _install_collection(self: Installer) -> None:
err = f"Failed to install collection: {exc} {exc.stderr}"
logger.critical(err)


def _swap_editable_collection(self: Installer) -> None:
"""Swap the installed collection with the current working directory.
Args:
site_pkg_path: The first site package path
"""
"""Swap the installed collection with the current working directory."""
site_pkg_collection_path = (
self.site_pkg_path
/ "ansible_collections"
Expand Down
2 changes: 1 addition & 1 deletion src/pip4a/uninstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

if TYPE_CHECKING:
from pathlib import Path

pass


Expand All @@ -37,7 +38,6 @@ def run(self: UnInstaller) -> None:
)
logger.critical(err)


self._set_interpreter()
self._set_bindir()
self._set_site_pkg_path()
Expand Down

0 comments on commit ccfb2c0

Please sign in to comment.