Skip to content

Commit

Permalink
Merge pull request #87 from koterpillar/manual-name
Browse files Browse the repository at this point in the history
Allow packages to override their names
  • Loading branch information
koterpillar authored Oct 8, 2023
2 parents ee3acd3 + f93a52c commit a93a0c0
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 49 deletions.
2 changes: 1 addition & 1 deletion mybox/package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .yum_repo import YumRepo

TYPES: list[tuple[str, Type[Package]]] = [
("name", SystemPackage),
("system", SystemPackage),
("repo", GitHubPackage),
("url", URLPackage),
("clone", Clone),
Expand Down
8 changes: 6 additions & 2 deletions mybox/package/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __hash__(self) -> int:
"""Allow hashing to pass when gathering async results."""
return hash(id(self))

name_: Optional[str] = Field(default=None, alias="name")

os: Optional[list[str]] = None
os_val = allow_singular("os")

Expand All @@ -34,9 +36,11 @@ def driver(self) -> Driver:
return self.driver_

@property
@abstractmethod
def name(self) -> str:
pass
return self.name_ or self.derive_name()

def derive_name(self) -> str:
raise ValueError("Package name not set.")

@abstractmethod
async def get_remote_version(self) -> str:
Expand Down
3 changes: 1 addition & 2 deletions mybox/package/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
class Clone(Destination, Tracked):
repo: str = Field(..., alias="clone")

@property
def name(self) -> str:
def derive_name(self) -> str:
return self.repo

async def directory_exists(self) -> bool:
Expand Down
3 changes: 1 addition & 2 deletions mybox/package/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def candidate_filter(
async def archive_url(self) -> str:
return (await self.artifact()).url

@property
def name(self) -> str:
def derive_name(self) -> str:
return self.repo

async def get_remote_version(self) -> str:
Expand Down
3 changes: 1 addition & 2 deletions mybox/package/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def source_absolute(cls, value): # pylint:disable=no-self-argument
only: Optional[list[str]] = None
only_val = allow_singular("only")

@property
def name(self) -> str:
def derive_name(self) -> str:
return f"links-{self.source}-{self.destination_}-{self.dot}-{self.root}"

def all_paths(self) -> Iterator[Path]:
Expand Down
3 changes: 1 addition & 2 deletions mybox/package/npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class NpmPackage(Root, ManualVersion, Tracked):
binaries: list[str] = Field(default_factory=list, alias="binary")
binaries_val = allow_singular_none("binaries")

@property
def name(self) -> str:
def derive_name(self) -> str:
return self.package

async def get_remote_version(self) -> str:
Expand Down
3 changes: 1 addition & 2 deletions mybox/package/pipx.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class PipxPackage(Tracked):
def package_to_lower(cls, value: str) -> str: # pylint: disable=no-self-argument
return value.lower()

@property
def name(self) -> str:
def derive_name(self) -> str:
return self.package

async def get_metadata(self) -> Optional[dict[str, Any]]:
Expand Down
3 changes: 1 addition & 2 deletions mybox/package/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
class Shell(Root):
shell: Path

@property
def name(self) -> str:
def derive_name(self) -> str:
return "_shell"

async def get_remote_version(self) -> str:
Expand Down
15 changes: 7 additions & 8 deletions mybox/package/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class SystemPackage(ManualVersion):
name_: str = Field(..., alias="name")
system: str
url_: Optional[Value] = Field(default=None, alias="url")
auto_updates: bool = False
services: list[str] = Field(default_factory=list, alias="service")
Expand All @@ -21,9 +21,8 @@ class SystemPackage(ManualVersion):
async def installer(self):
return await make_installer(self.driver)

@property
def name(self) -> str:
return self.name_
def derive_name(self) -> str:
return self.system

@async_cached
async def url(self) -> Optional[str]:
Expand All @@ -36,13 +35,13 @@ async def get_remote_version(self) -> str:
return url_version(url)
if self.auto_updates:
return "latest"
return await (await self.installer()).latest_version(self.name)
return await (await self.installer()).latest_version(self.system)

async def local_version(self) -> Optional[str]:
if await self.url():
return self.cached_version
installer = await self.installer()
version = await installer.installed_version(self.name)
version = await installer.installed_version(self.system)
if self.auto_updates:
return "latest" if version else None
return version
Expand All @@ -63,9 +62,9 @@ async def install(self) -> None:
await installer.install(url)
await self.cache_version()
elif await self.local_version():
await installer.upgrade(self.name)
await installer.upgrade(self.system)
else:
await installer.install(self.name)
await installer.install(self.system)
await (await self.driver.os()).switch(
linux=self.postinstall_linux, macos=self.postinstall_macos
)()
Expand Down
5 changes: 2 additions & 3 deletions mybox/package/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ class URLPackage(ArchivePackage):
async def archive_url(self) -> str:
return self.url

@property
def name(self):
def derive_name(self) -> str:
url = urlparse(self.url)

name = url.hostname
name = url.hostname or ""

path = url.path.rsplit("/", 1)[-1]
if path:
Expand Down
3 changes: 1 addition & 2 deletions mybox/package/yum_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class YumRepo(ManualVersion):
baseurl: str = Field(alias="yum_url")
gpg_key: Optional[str] = None

@property
def name(self) -> str:
def derive_name(self) -> str:
return f"yum-{self.repo_name}"

async def get_remote_version(self) -> str:
Expand Down
12 changes: 8 additions & 4 deletions tests/package/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,17 @@ async def test_no_root_required_for_is_installed(
assert await package.is_installed() in {True, False}

JAVA: list[PackageArgs] = [
{"name": "java-17-openjdk", "os": "linux", "distribution": "fedora"},
{"name": "openjdk-17-jre", "os": "linux", "distribution": ["debian", "ubuntu"]},
{"system": "java-17-openjdk", "os": "linux", "distribution": "fedora"},
{
"system": "openjdk-17-jre",
"os": "linux",
"distribution": ["debian", "ubuntu"],
},
]

NODE: list[PackageArgs] = [
{"name": "nodejs", "os": "linux"},
{"name": "node", "os": "darwin"},
{"system": "nodejs", "os": "linux"},
{"system": "node", "os": "darwin"},
]

@async_cached
Expand Down
12 changes: 6 additions & 6 deletions tests/package/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,27 @@ async def check_installed(self):

prerequisites = [
{
"name": "libgl1-mesa-glx",
"system": "libgl1-mesa-glx",
"distribution": ["debian", "ubuntu"],
},
{
"name": "mesa-libGL",
"system": "mesa-libGL",
"distribution": "fedora",
},
{
"name": "libegl1",
"system": "libegl1",
"distribution": ["debian", "ubuntu"],
},
{
"name": "mesa-libEGL",
"system": "mesa-libEGL",
"distribution": "fedora",
},
{
"name": "xvfb",
"system": "xvfb",
"distribution": ["debian", "ubuntu"],
},
{
"name": "xorg-x11-server-Xvfb",
"system": "xorg-x11-server-Xvfb",
"distribution": "fedora",
},
]
Expand Down
4 changes: 2 additions & 2 deletions tests/package/test_npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ async def constructor_args(self) -> PackageArgs:

prerequisites = [
*PackageTestBase.NODE,
{"name": "npm", "os": "linux", "distribution": ["debian", "ubuntu"]},
{"name": "nodejs-npm", "os": "linux", "distribution": ["fedora"]},
{"system": "npm", "os": "linux", "distribution": ["debian", "ubuntu"]},
{"system": "nodejs-npm", "os": "linux", "distribution": ["fedora"]},
]

async def check_installed_command(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/package/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestRipGrep(PackageTestBase):
affects_system = True

async def constructor_args(self) -> PackageArgs:
return {"name": "ripgrep"}
return {"system": "ripgrep"}

async def check_installed_command(self):
return ["rg", "--help"]
Expand All @@ -20,7 +20,7 @@ class TestRPMFusion(PackageTestBase):

async def constructor_args(self) -> PackageArgs:
return {
"name": "rpmfusion-free-release",
"system": "rpmfusion-free-release",
"url": "https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-38.noarch.rpm",
}

Expand All @@ -41,7 +41,7 @@ class TestInteractiveDeb(PackageTestBase):
affects_system = True

async def constructor_args(self) -> PackageArgs:
return {"name": "tzdata"}
return {"system": "tzdata"}

async def check_installed_command(self):
return ["cat", "/usr/share/doc/tzdata/copyright"]
Expand All @@ -60,7 +60,7 @@ class TestVirtualPackage(PackageTestBase):
affects_system = True

async def constructor_args(self) -> PackageArgs:
return {"name": "g++"}
return {"system": "g++"}

async def check_installed_command(self):
return ["g++", "--version"]
Expand Down
5 changes: 0 additions & 5 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,8 @@ class DummyPackage(ManualVersion, Tracked):
files_val = allow_singular_none("files")

version: str = "1"
name_: str = Field(..., alias="name")
error: Optional[Exception] = None

@property
def name(self) -> str:
return self.name_

async def get_remote_version(self) -> str:
return self.version

Expand Down

0 comments on commit a93a0c0

Please sign in to comment.