pmbootstrap/pmb/parse/apkbuild.py

141 lines
4.8 KiB
Python
Raw Normal View History

2017-05-26 20:08:45 +00:00
"""
Copyright 2017 Oliver Smith
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 pmb.config
def replace_variables(apkbuild):
"""
Replace a hardcoded list of variables inside the APKBUILD.
"""
ret = apkbuild
# _flavor: ${_device} (lineageos kernel packages)
ret["_flavor"] = ret["_flavor"].replace("${_device}",
ret["_device"])
# pkgname: $_flavor
ret["pkgname"] = ret["pkgname"].replace("${_flavor}", ret["_flavor"])
# subpackages: $pkgname
replaced = []
for subpackage in ret["subpackages"]:
replaced.append(subpackage.replace("$pkgname", ret["pkgname"]))
ret["subpackages"] = replaced
2017-06-05 01:58:45 +00:00
# makedepends: $makedepends_host, $makedepends_build, $_llvmver
2017-05-26 20:08:45 +00:00
replaced = []
for makedepend in ret["makedepends"]:
if makedepend.startswith("$"):
key = makedepend[1:]
if key in ret:
replaced += ret[key]
else:
raise RuntimeError("Could not resolve variable " +
makedepend + " in APKBUILD of " +
apkbuild["pkgname"])
else:
# replace in the middle of the string
for var in ["_llvmver"]:
makedepend = makedepend.replace("$" + var, ret[var])
replaced += [makedepend]
# Python: ${pkgname#py-}
if ret["pkgname"].startswith("py-"):
replacement = ret["pkgname"][3:]
for var in ["depends", "makedepends", "subpackages"]:
for i in range(len(ret[var])):
ret[var][i] = ret[var][i].replace(
"${pkgname#py-}", replacement)
2017-05-26 20:08:45 +00:00
ret["makedepends"] = replaced
return ret
def cut_off_function_names(apkbuild):
"""
For subpackages: only keep the subpackage name, without the internal
function name, that tells how to build the subpackage.
"""
sub = apkbuild["subpackages"]
for i in range(len(sub)):
sub[i] = sub[i].split(":", 1)[0]
apkbuild["subpackages"] = sub
return apkbuild
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
def apkbuild(args, path):
2017-05-26 20:08:45 +00:00
"""
Parse relevant information out of the APKBUILD file. This is not meant
to be perfect and catch every edge case (for that, a full shell parser
would be necessary!). Instead, it should just work with the use-cases
covered by pmbootstrap and not take too long.
:param path: Full path to the APKBUILD
:returns: Relevant variables from the APKBUILD. Arrays get returned as
arrays.
"""
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
# Try to get a cached result first (we assume, that the aports don't change
# in one pmbootstrap call)
if path in args.cache["apkbuild"]:
return args.cache["apkbuild"][path]
2017-05-26 20:08:45 +00:00
with open(path, encoding="utf-8") as handle:
lines = handle.readlines()
# Parse all attributes from the config
ret = {}
for i in range(len(lines)):
for attribute, options in pmb.config.apkbuild_attributes.items():
if not lines[i].startswith(attribute + "="):
continue
# Extend the line value until we reach the ending quote sign
line_value = lines[i][len(attribute + "="):-1]
end_char = None
if line_value.startswith("\""):
end_char = "\""
value = ""
while i < len(lines) - 1:
value += line_value.replace("\"", "").strip()
if not end_char or line_value.endswith(end_char):
break
value += " "
i += 1
line_value = lines[i][:-1]
# Split up arrays, delete empty strings inside the list
if options["array"]:
if value:
value = list(filter(None, value.split(" ")))
else:
value = []
ret[attribute] = value
# Add missing entries
for attribute, options in pmb.config.apkbuild_attributes.items():
if attribute not in ret:
if options["array"]:
ret[attribute] = []
else:
ret[attribute] = ""
ret = replace_variables(ret)
ret = cut_off_function_names(ret)
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
args.cache["apkbuild"][path] = ret
2017-05-26 20:08:45 +00:00
return ret