Fix missing removeprefix in pseudo_builder.py

This commit is contained in:
IamTheFij 2022-06-30 19:48:35 -07:00
parent d876639c3e
commit 8585380eae
3 changed files with 32 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import toml
from wheel.wheelfile import WheelFile from wheel.wheelfile import WheelFile
import release_gitter as rg import release_gitter as rg
from release_gitter import removeprefix
PACKAGE_NAME = "pseudo" PACKAGE_NAME = "pseudo"
@ -71,7 +72,7 @@ class _PseudoBuildBackend:
print("Prepare meta", metadata_directory, config_settings) print("Prepare meta", metadata_directory, config_settings)
metadata = read_metadata() metadata = read_metadata()
version = metadata.version.removeprefix("v") version = removeprefix(metadata.version, "v")
# Returns distinfo dir? # Returns distinfo dir?
dist_info = Path(metadata_directory) / f"{PACKAGE_NAME}-{version}.dist-info" dist_info = Path(metadata_directory) / f"{PACKAGE_NAME}-{version}.dist-info"
@ -118,7 +119,7 @@ class _PseudoBuildBackend:
metadata_directory = Path(metadata_directory) metadata_directory = Path(metadata_directory)
metadata = read_metadata() metadata = read_metadata()
version = metadata.version.removeprefix("v") version = removeprefix(metadata.version, "v")
wheel_directory = Path(wheel_directory) wheel_directory = Path(wheel_directory)
wheel_directory.mkdir(exist_ok=True) wheel_directory.mkdir(exist_ok=True)

View File

@ -28,6 +28,24 @@ class InvalidRemoteError(ValueError):
pass pass
def removeprefix(s: str, pre: str) -> str:
# Duplicate str.removeprefix for py<3.9
try:
return s.removeprefix(pre) # type: ignore
except AttributeError:
# Py < 3.9
return s[len(pre) :] if s and s.startswith(pre) else s
def removesuffix(s: str, suf: str) -> str:
# Duplicate str.removesuffix for py<3.9
try:
return s.removesuffix(suf) # type: ignore
except AttributeError:
# Py < 3.9
return s[: -len(suf)] if s and s.endswith(suf) else s
@dataclass @dataclass
class GitRemoteInfo: class GitRemoteInfo:
hostname: str hostname: str
@ -93,14 +111,7 @@ def parse_git_remote(git_url: Optional[str] = None) -> GitRemoteInfo:
f"{path[1:3]} Could not parse owner and repo from URL {git_url}" f"{path[1:3]} Could not parse owner and repo from URL {git_url}"
) )
repo = path[2] return GitRemoteInfo(u.hostname, path[1], removesuffix(path[2], ".git"))
try:
repo = repo.removesuffix(".git") # type: ignore
except AttributeError:
# Py < 3.9
repo = repo[: -len(".git")] if repo and repo.endswith(".git") else repo
return GitRemoteInfo(u.hostname, path[1], repo)
def parse_cargo_version(p: Path) -> str: def parse_cargo_version(p: Path) -> str:

View File

@ -20,7 +20,7 @@ class TestExpression(NamedTuple):
args: list[Any] args: list[Any]
kwargs: dict[str, Any] kwargs: dict[str, Any]
expected: Any expected: Any
exception: Optional[type[Exception]] exception: Optional[type[Exception]] = None
def run(self, f: Callable): def run(self, f: Callable):
with self.t.subTest(f=f, args=self.args, kwargs=self.kwargs): with self.t.subTest(f=f, args=self.args, kwargs=self.kwargs):
@ -38,6 +38,15 @@ class TestExpression(NamedTuple):
raise raise
class TestGeneral(unittest.TestCase):
def test_removesuffix(self):
for test_case in (
TestExpression(self, ["repo.git", ".git"], {}, "repo"),
TestExpression(self, ["repo", ".git"], {}, "repo"),
):
test_case.run(release_gitter.removesuffix)
class TestRemoteInfo(unittest.TestCase): class TestRemoteInfo(unittest.TestCase):
def test_parse_remote_info(self): def test_parse_remote_info(self):
for test_case in ( for test_case in (