Skip to content

Commit

Permalink
base: Add --installed flag to package selection
Browse files Browse the repository at this point in the history
  • Loading branch information
avdgrinten committed Nov 8, 2024
1 parent a82eb46 commit d0bab40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions xbstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ def select_pkgs(cfg, args):
else:
sel = [cfg.get_target_pkg(name) for name in args.packages]

if args.installed:
sel.extend(cfg.get_installed_pkgs())

if args.deps_of is not None:
for pkg_name in args.deps_of:
pkg = cfg.get_target_pkg(pkg_name)
Expand All @@ -485,6 +488,7 @@ def select_pkgs(cfg, args):

select_pkgs.parser = argparse.ArgumentParser(add_help=False)
select_pkgs.parser.add_argument("--all", action="store_true")
select_pkgs.parser.add_argument("--installed", action="store_true")
select_pkgs.parser.add_argument("--deps-of", type=str, action="append")
select_pkgs.parser.add_argument("packages", nargs="*", type=str)

Expand Down
34 changes: 34 additions & 0 deletions xbstrap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ def get_target_pkg(self, name):
return pkg
else:
raise GenericError(f"Unknown package {name}")
raise GenericError(f"Unknown package {name}")

def get_xbps_url(self, arch):
xbps_yml = self._root_yml["repositories"]["xbps"]
Expand Down Expand Up @@ -796,6 +797,39 @@ def access_local_xbps_repodata(self, arch):
index = _xbps_utils.read_repodata(rd_path)
return index

def get_installed_pkgs(self):
if not self.use_xbps:
raise GenericError("Package management configuration cannot query installed packages")
environ = os.environ.copy()
_util.build_environ_paths(
environ, "PATH", prepend=[os.path.join(_util.find_home(), "bin")]
)

out = subprocess.check_output(
["xbps-query", "-r", self.sysroot_dir, "-l"],
env=environ,
stderr=subprocess.DEVNULL,
encoding="utf-8",
)

# Lines have a format such as:
# "ii linux-headers-6.9.3_1 Linux kernel headers"
# "ii libexpat-2.5.0_6 Stream-oriented XML parser library"
pattern = re.compile(r"^[\w?]+ ([^ ]+) ")
for line in out.splitlines():
match = pattern.match(line)
if not match:
raise GenericError(f"Unexpected line {repr(line)} from xbps-query")
pkgver = match.group(1)
name = pkgver.rsplit("-", maxsplit=1)[0]

pkg = self._target_pkgs.get(name)
if pkg is None:
continue
if not self.check_labels(pkg.label_set):
continue
yield pkg


class ScriptStep:
def __init__(self, step_yml, containerless=False):
Expand Down

0 comments on commit d0bab40

Please sign in to comment.