Skip to content

Commit

Permalink
Merge pull request #379 from bundlewrap/openbsd-flavors
Browse files Browse the repository at this point in the history
pkg_openbsd: Support flavors
  • Loading branch information
trehn committed Dec 20, 2017
2 parents 2fd8a57 + 0f8035d commit 9e918c5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
54 changes: 44 additions & 10 deletions bundlewrap/items/pkg_openbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@
from bundlewrap.utils.text import mark_for_translation as _


PKGSPEC_REGEX = re.compile(r"^(.+)-(\d.+)$")


def pkg_install(node, pkgname, version):
full_name = "{}-{}".format(pkgname, version) if version else pkgname
PKGSPEC_REGEX = re.compile(r"^([^-]+)-(\d[^-]+)(-(.+))?$")


def pkg_install(node, pkgname, flavor, version):
# Setting either flavor or version to None means "don't specify this
# component". Setting flavor to the empty string means choosing the
# "normal" flavor.
#
# flavor = "", version = None: "pkgname--"
# flavor = "foo", version = None: "pkgname--foo"
# flavor = None, version = None: "pkgname" (a)
# flavor = "", version = "1.0": "pkgname-1.0" (b)
# flavor = "foo", version = "1.0": "pkgname-1.0-foo"
# flavor = None, version = "1.0": "pkgname-1.0"
# flavor = None, version = "-foo": "pkgname--foo" (backwards compat)
if flavor is None and version is None:
# Case "(a)"
full_name = pkgname
elif flavor == "" and version is not None:
# Case "(b)"
full_name = "{}-{}".format(pkgname, version)
else:
version_part = "-" if version is None else "-{}".format(version)
flavor_part = "" if flavor is None else "-{}".format(flavor)
full_name = "{}{}{}".format(pkgname, version_part, flavor_part)
return node.run("pkg_add -r -I {}".format(full_name), may_fail=True)


Expand All @@ -23,10 +43,15 @@ def pkg_installed(node, pkgname):
may_fail=True,
)
for line in result.stdout.decode('utf-8').strip().splitlines():
installed_package, installed_version = PKGSPEC_REGEX.match(line).groups()
installed_package, installed_version, _, installed_flavor = \
PKGSPEC_REGEX.match(line).groups()
if installed_package == pkgname:
return installed_version
return False
# If our regex didn't match a flavor, then this is
# equivalent to using the "normal" flavor.
if installed_flavor is None:
installed_flavor = ""
return installed_version, installed_flavor
return False, None


def pkg_remove(node, pkgname):
Expand All @@ -40,6 +65,7 @@ class OpenBSDPkg(Item):
BUNDLE_ATTRIBUTE_NAME = "pkg_openbsd"
ITEM_ATTRIBUTES = {
'installed': True,
'flavor': "",
'version': None,
}
ITEM_TYPE_NAME = "pkg_openbsd"
Expand All @@ -52,6 +78,8 @@ def __repr__(self):

def cdict(self):
cdict = self.attributes.copy()
if not cdict['installed']:
del cdict['flavor']
if cdict['version'] is None or not cdict['installed']:
del cdict['version']
return cdict
Expand All @@ -60,12 +88,18 @@ def fix(self, status):
if self.attributes['installed'] is False:
pkg_remove(self.node, self.name)
else:
pkg_install(self.node, self.name, self.attributes['version'])
pkg_install(
self.node,
self.name,
self.attributes['flavor'],
self.attributes['version']
)

def sdict(self):
version = pkg_installed(self.node, self.name)
version, flavor = pkg_installed(self.node, self.name)
return {
'installed': bool(version),
'flavor': flavor if flavor else _("none"),
'version': version if version else _("none"),
}

Expand Down
15 changes: 14 additions & 1 deletion docs/content/items/pkg_openbsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Handles packages installed by `pkg_add` on OpenBSD systems.
"baz": {
"installed": False,
},
"qux": {
"flavor": "no_x11",
},
}

<br><br>
Expand All @@ -29,6 +32,16 @@ See also: [The list of generic builtin item attributes](../repo/items.py.md#buil

<hr>

## flavor

Optional, defaults to the "normal" flavor.

Can be used together with `version`. Ignored when `installed` is `False`.

<hr>

## version

Optional version string. Required for packages that offer multiple variants (like nginx or sudo). Ignored when `installed` is `False`.
Optional version string. Can be used to select one specific version of a package.

Can be used together with `flavor`. Ignored when `installed` is `False`.

0 comments on commit 9e918c5

Please sign in to comment.