pmbootstrap/pmb/chroot/root.py

87 lines
2.8 KiB
Python
Raw Normal View History

2017-05-26 20:08:45 +00:00
"""
Copyright 2018 Oliver Smith
2017-05-26 20:08:45 +00:00
This file is part of pmbootstrap.
pmbootstrap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pmbootstrap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import shutil
import shlex
import pmb.config
import pmb.chroot
import pmb.chroot.binfmt
import pmb.helpers.run
def executables_absolute_path():
"""
Get the absolute paths to the sh and chroot executables.
"""
ret = {}
for binary in ["sh", "chroot"]:
path = shutil.which(binary, path=pmb.config.chroot_host_path)
if not path:
raise RuntimeError("Could not find the '" + binary +
"' executable. Make sure, that it is in" " your current user's PATH.")
ret[binary] = path
return ret
2017-05-26 20:08:45 +00:00
def root(args, cmd, suffix="native", working_dir="/", log=True,
auto_init=True, return_stdout=False, check=True):
2017-05-26 20:08:45 +00:00
"""
Run a command inside a chroot as root.
:param log: When set to true, redirect all output to the logfile
:param auto_init: Automatically initialize the chroot
"""
# Get and verify chroot folder
chroot = args.work + "/chroot_" + suffix
if not auto_init and not os.path.islink(chroot + "/bin/sh"):
raise RuntimeError("Chroot does not exist: " + chroot)
Properly rebuild/install packages when something changed (Fix #120, #108, #131) (#129) TLDR: Always rebuild/install packages when something changed when executing "pmbootstrap install/initfs/flash", more speed in dependency resolution. --- pmbootstrap has already gotten some support for "timestamp based rebuilds", which modifies the logic for when packages should be rebuilt. It doesn't only consider packages outdated with old pkgver/pkgrel combinations, but also packages, where a source file has a newer timestamp, than the built package has. I've found out, that this can lead to more rebuilds than expected. For example, when you check out the pmbootstrap git repository again into another folder, although you have already built packages. Then all files have the timestamp of the checkout, and the packages will appear to be outdated. While this is not largely a concern now, this will become a problem once we have a binary package repository, because then the packages from the binary repo will always seem to be outdated, if you just freshly checked out the repository. To combat this, git gets asked if the files from the aport we're looking at are in sync with upstream, or not. Only when the files are not in sync with upstream and the timestamps of the sources are newer, a rebuild gets triggered from now on. In case this logic should fail, I've added an option during "pmbootstrap init" where you can enable or disable the "timestamp based rebuilds" option. In addition to that, this commit also works on fixing #120: packages do not get updated in "pmbootstrap install" after they have been rebuilt. For this to work, we specify all packages explicitly for abuild, instead of letting abuild do the resolving. This feature will also work with the "timestamp based rebuilds". This commit also fixes the working_dir argument in pmb.helpers.run.user, which was simply ignored before. Finally, the performance of the dependency resolution is faster again (when compared to the current version in master), because the parsed apkbuilds and finding the aport by pkgname gets cached during one pmbootstrap call (in args.cache, which also makes it easy to put fake data there in testcases). The new dependency resolution code can output lots of verbose messages for debugging by specifying the `-v` parameter. The meaning of that changed, it used to output the file names where log messages come from, but no one seemed to use that anyway.
2017-07-10 15:23:43 +00:00
if auto_init:
pmb.chroot.init(args, suffix)
2017-05-26 20:08:45 +00:00
# Run the args with sudo chroot, and with cleaned environment
# variables
executables = executables_absolute_path()
2017-05-26 20:08:45 +00:00
for i in range(len(cmd)):
cmd[i] = shlex.quote(cmd[i])
cmd_inner_shell = ("cd " + shlex.quote(working_dir) + ";" +
" ".join(cmd))
cmd_full = ["sudo", executables["sh"], "-c",
"env -i" + # unset all
2017-05-26 20:08:45 +00:00
" CHARSET=UTF-8" +
" PATH=" + pmb.config.chroot_path +
" SHELL=/bin/ash" +
" HISTFILE=~/.ash_history" +
" " + executables["chroot"] +
2017-05-26 20:08:45 +00:00
" " + chroot +
" sh -c " + shlex.quote(cmd_inner_shell)
]
# Generate log message
log_message = "(" + suffix + ") % "
if working_dir != "/":
log_message += "cd " + working_dir + " && "
log_message += " ".join(cmd)
# Run the command
return pmb.helpers.run.core(args, cmd_full, log_message, log,
return_stdout, check)