Skip to content

Commit

Permalink
meta: implement update command for updating packages
Browse files Browse the repository at this point in the history
  • Loading branch information
no92 committed Jun 2, 2024
1 parent 87f6810 commit a20a3ca
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 2 deletions.
100 changes: 100 additions & 0 deletions xbstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,104 @@ def resolve_host_paths(x):
# ----------------------------------------------------------------------------------------


def do_update(args):
cfg = config_for_args(args)
if args.all:
pkgs = cfg.all_pkgs()
else:
pkgs = [cfg.get_target_pkg(pkg) for pkg in args.packages]
to_update = []
checked_packages = 0

if args.verbose:
_util.log_info("Checking packages for updates:")
for pkg in pkgs:
if pkg.check_if_installed(cfg).missing and args.uninstalled:
if args.verbose:
print(f"\t{pkg.name}: not installed")
to_update.append(pkg)
checked_packages += 1
continue

checked_packages += 1

if pkg.version == pkg.installed_version:
if args.verbose:
print(f"\t{pkg.name}: {pkg.version} (up-to-date)")
else:
if not pkg.installed_version:
if args.verbose:
print(f"\t{pkg.name}: available {pkg.version}, installed ?")
else:
if args.verbose:
print(
f"\t{pkg.name}: available {pkg.version}, installed {pkg.installed_version}"
)
to_update.append(pkg)

_util.log_info(f"{len(to_update)} of {checked_packages} checked packages need updating:")
for pkg in to_update:
if pkg.installed_version:
print(f"\t{pkg.name} ({pkg.installed_version} -> {pkg.version})")
else:
print(f"\t{pkg.name} (new install of {pkg.version})")

if not args.dry_run:
plan = xbstrap.base.Plan(cfg)
plan.update = not args.pull
plan.reset = xbstrap.base.ResetMode.RESET if args.reset else None

for pkg in to_update:
if args.pull:
plan.wanted.add((xbstrap.base.Action.PULL_PKG_PACK, pkg))
else:
plan.wanted.add((xbstrap.base.Action.CONFIGURE_PKG, pkg))
plan.wanted.add((xbstrap.base.Action.BUILD_PKG, pkg))
if plan.cfg.use_xbps:
plan.wanted.add((xbstrap.base.Action.PACK_PKG, pkg))
plan.wanted.add((xbstrap.base.Action.INSTALL_PKG, pkg))

plan.run_plan()


do_update.parser = main_subparsers.add_parser(
"update",
description=(
"Update packages to their newest version. "
"Unless told otherwise, packages are built from source. "
"Only listed packages (or all with --all) are checked. "
"Not installed packages are excluded by default, unless --uninstalled is supplied."
),
parents=[],
)
do_update.parser.add_argument("packages", nargs="*")
do_update.parser.add_argument(
"-p", "--pull", action="store_true", help="pull packages instead of building from source"
)
do_update.parser.add_argument(
"-n", "--dry-run", action="store_true", help="only check for updates without actually updating"
)
do_update.parser.add_argument("-a", "--all", action="store_true", help="check all packages")
do_update.parser.add_argument(
"-u",
"--uninstalled",
action="store_true",
help="include packages that are not installed",
)
do_update.parser.add_argument(
"-r",
"--reset",
action="store_true",
help="reset repository state; risks loss of local commits!",
)
do_update.parser.add_argument(
"-v", "--verbose", action="store_true", help="print every version check performed"
)
do_update.parser.set_defaults(_impl=do_update)

# ----------------------------------------------------------------------------------------


def do_execute_manifest(args):
if args.c is not None:
manifest = yaml.load(args.c, Loader=xbstrap.base.global_yaml_loader)
Expand Down Expand Up @@ -993,6 +1091,8 @@ def main():
do_run_task(args)
elif args.command == "lsp":
do_lsp(args)
elif args.command == "update":
do_update(args)
else:
assert not "Unexpected command"
except (
Expand Down
48 changes: 46 additions & 2 deletions xbstrap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def rolling_id(self):
commit_yml = self._cfg._commit_yml.get("commits", dict()).get(self._name, dict())
rolling_id = commit_yml.get("rolling_id")
if rolling_id is None:
raise RollingIdUnavailableError(self._name)
return self.determine_rolling_id()
return rolling_id

def determine_rolling_id(self):
Expand Down Expand Up @@ -1404,6 +1404,45 @@ def compute_version(self, **kwargs):
def version(self):
return self.compute_version()

@property
def installed_version(self):
if self._cfg.use_xbps:
environ = os.environ.copy()
_util.build_environ_paths(
environ, "PATH", prepend=[os.path.join(_util.find_home(), "bin")]
)
environ["XBPS_ARCH"] = self.architecture

try:
out = (
subprocess.check_output(
[
"xbps-query",
"-r",
self._cfg.sysroot_dir,
self.name,
"--property",
"pkgver",
],
env=environ,
)
.decode()
.strip()
)
if out.startswith(self.name):
return out[len(self.name) + 1 :]
except subprocess.CalledProcessError:
pass

path = os.path.join(self._cfg.sysroot_dir, "etc", "xbstrap", self.name + ".installed")
if not os.path.isfile(path):
return None
with open(path, "r") as f:
data = yaml.load(f, Loader=yaml.SafeLoader)
if data and "version" in data:
return data["version"]
return None

def get_task(self, task):
if task in self._tasks:
return self._tasks[task]
Expand Down Expand Up @@ -1489,7 +1528,11 @@ def mark_as_installed(self):
_util.try_mkdir(os.path.join(self._cfg.sysroot_dir, "etc"))
_util.try_mkdir(os.path.join(self._cfg.sysroot_dir, "etc", "xbstrap"))
path = os.path.join(self._cfg.sysroot_dir, "etc", "xbstrap", self.name + ".installed")
touch(path)
with open(path, "w") as f:
data = {
"version": self.version,
}
yaml.safe_dump(data, f, sort_keys=False, default_flow_style=False)


class PackageRunTask(RequirementsMixin):
Expand Down Expand Up @@ -2587,6 +2630,7 @@ def install_pkg(cfg, pkg):
]
_util.log_info("Running {}".format(args))
subprocess.check_call(args, env=environ, stdout=output)
pkg.mark_as_installed()
else:
installtree(pkg.staging_dir, cfg.sysroot_dir)
pkg.mark_as_installed()
Expand Down

0 comments on commit a20a3ca

Please sign in to comment.