Skip to content

Commit

Permalink
fix: port name capitalisation
Browse files Browse the repository at this point in the history
Sets the correct capitalisation of a port's name from the user's (potentially incorrect) input
  • Loading branch information
harens committed Feb 24, 2023
1 parent 33fbfe4 commit 09d6c61
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
3 changes: 3 additions & 0 deletions seaport/_clipboard/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def clip(

port = Port(name)

# Sets correct capitalisation
name = port.name

old_checks = port.checksums()

# Determine new version
Expand Down
4 changes: 4 additions & 0 deletions seaport/_pull_request/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from seaport._clipboard.clipboard import clip
from seaport._pull_request.clone import pr_variables, sync_fork
from seaport._pull_request.portfile import new_contents
from seaport.portfile import Port


@click.command()
Expand Down Expand Up @@ -87,6 +88,9 @@ def pr(
# That's the command that determines the new contents
ctx.forward(clip)

# Sets the correct capitalisation of name
name, _ = Port.rightcapitalised(name, user_path(True))

# Retrieve new version number and contents
# Assumes first category is where to put the portfile
contents, bump, category = new_contents()
Expand Down
42 changes: 34 additions & 8 deletions seaport/portfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,14 @@ def __init__(self, name: str) -> None:
# TODO: Refactor this
# TODO: This also kind of defeats the purpose of that bandit error, so find a better way to determine the path
# no forward slash at end for Bandit B607
self.name: Final[str] = name

self._path: Final[str] = format_subprocess(["/usr/bin/which", "port"]).replace(
"/port", ""
)

try:
self._info: Final[str] = format_subprocess(
[f"{self._path}/port", "info", self.name]
)
except subprocess.CalledProcessError:
# TODO: Set a more specific exception
raise Exception(f"{self.name} doesn't exist, run portindex if port is new")
capitalised_name, info = self.rightcapitalised(name, self._path)
self.name: Final[str] = capitalised_name
self._info: Final[str] = info

self._parsedInfo: Final[List[str]] = self._info[
self._info.find("@") + 1 :
Expand Down Expand Up @@ -122,6 +118,36 @@ def __init__(self, name: str) -> None:
else int(versionParse[1])
)

@staticmethod
def rightcapitalised(
input_name: str, port_path: str = "/opt/local/bin"
) -> Tuple[str, str]:
"""Get the correct capitalisation of a port.
Args:
input_name: The potentially wrong-capitalised name of a port
port_path: The path to the port binary (default /opt/local/bin)
Returns:
Tuple[str, str]: A tuple representing the right-capitalised name and the scraped
port info.
"""
try:
portInfo: Final[str] = format_subprocess(
[f"{port_path}/port", "info", input_name]
)
except subprocess.CalledProcessError:
raise RuntimeError(
f"{input_name} doesn't exist, run portindex if port is new"
)

# If @ not in output, then parsing probably failed. Fall back to original name
return (
portInfo.split("@")[0].strip() if "@" in portInfo else input_name,
portInfo,
)

def __str__(self) -> str:
"""Outputs the name and version of the port.
Expand Down

0 comments on commit 09d6c61

Please sign in to comment.