This function takes care of most of what's still handled by _action_dockerfile(), namely populating the dictionary that _dockerfile_format() will later consume. Signed-off-by: Andrea Bolognani <abologna@xxxxxxxxxx> --- guests/lcitool | 132 ++++++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 61 deletions(-) diff --git a/guests/lcitool b/guests/lcitool index f216910..82e98c5 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -642,6 +642,76 @@ class Application: with open(keyfile, "r") as r: return r.read().rstrip() + def _dockerfile_build_varmap(self, facts, mappings, pip_mappings, projects, cross_arch): + package_format = facts["package_format"] + package_manager = facts["package_manager"] + os_name = facts["os_name"] + os_version = facts["os_version"] + os_full = os_name + os_version + + pkgs = {} + cross_pkgs = {} + pip_pkgs = {} + base_keys = ["default", package_format, os_name, os_full] + cross_keys = [] + if cross_arch: + keys = base_keys + [cross_arch + "-" + k for k in base_keys] + cross_keys = ["cross-policy-" + k for k in base_keys] + else: + keys = base_keys + [self._native_arch + "-" + k for k in base_keys] + + # We need to add the base project manually here: the standard + # machinery hides it because it's an implementation detail + for project in projects + ["base"]: + for package in self._projects.get_packages(project): + cross_policy = "native" + for key in cross_keys: + if key in mappings[package]: + cross_policy = mappings[package][key] + if cross_policy not in ["native", "foreign", "skip"]: + raise Exception( + "Unexpected cross arch policy {} for {}".format + (cross_policy, package)) + + for key in keys: + if key in mappings[package]: + pkgs[package] = mappings[package][key] + if package in pip_mappings and key in pip_mappings[package]: + pip_pkgs[package] = pip_mappings[package][key] + + if package not in pkgs: + continue + if package in pip_pkgs and pkgs[package] is not None: + del pip_pkgs[package] + if cross_policy == "foreign" and pkgs[package] is not None: + cross_pkgs[package] = pkgs[package] + if pkgs[package] is None or cross_policy in ["skip", "foreign"]: + del pkgs[package] + + pkg_align = " \\\n" + (" " * len("RUN " + package_manager + " ")) + pip_pkg_align = " \\\n" + (" " * len("RUN pip3 ")) + + varmap = {} + varmap["package_manager"] = package_manager + varmap["pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkgs.values()))) + + if package_format == "deb" and cross_arch: + deb_arch = Util.native_arch_to_deb_arch(cross_arch) + abi = Util.native_arch_to_abi(cross_arch) + lib = Util.native_arch_to_lib(cross_arch) + gcc = "gcc-" + abi + varmap["cross_arch"] = deb_arch + pkg_names = [p + ":" + deb_arch for p in cross_pkgs.values()] + pkg_names.append(gcc) + varmap["cross_pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkg_names))) + varmap["cross_abi"] = abi + varmap["cross_lib"] = lib + + if pip_pkgs: + varmap["pip_pkgs"] = pip_pkg_align[1:] + pip_pkg_align.join(sorted(set(pip_pkgs.values()))) + + return varmap + def _dockerfile_format(self, facts, cross_arch, varmap): package_format = facts["package_format"] package_manager = facts["package_manager"] @@ -787,67 +857,7 @@ class Application: ) ) - pkgs = {} - cross_pkgs = {} - pip_pkgs = {} - base_keys = ["default", package_format, os_name, os_full] - cross_keys = [] - if cross_arch: - keys = base_keys + [cross_arch + "-" + k for k in base_keys] - cross_keys = ["cross-policy-" + k for k in base_keys] - else: - keys = base_keys + [self._native_arch + "-" + k for k in base_keys] - - # We need to add the base project manually here: the standard - # machinery hides it because it's an implementation detail - for project in projects + ["base"]: - for package in self._projects.get_packages(project): - cross_policy = "native" - for key in cross_keys: - if key in mappings[package]: - cross_policy = mappings[package][key] - if cross_policy not in ["native", "foreign", "skip"]: - raise Exception( - "Unexpected cross arch policy {} for {}".format - (cross_policy, package)) - - for key in keys: - if key in mappings[package]: - pkgs[package] = mappings[package][key] - if package in pip_mappings and key in pip_mappings[package]: - pip_pkgs[package] = pip_mappings[package][key] - - if package not in pkgs: - continue - if package in pip_pkgs and pkgs[package] is not None: - del pip_pkgs[package] - if cross_policy == "foreign" and pkgs[package] is not None: - cross_pkgs[package] = pkgs[package] - if pkgs[package] is None or cross_policy in ["skip", "foreign"]: - del pkgs[package] - - pkg_align = " \\\n" + (" " * len("RUN " + package_manager + " ")) - pip_pkg_align = " \\\n" + (" " * len("RUN pip3 ")) - - varmap = {} - varmap["package_manager"] = package_manager - varmap["pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkgs.values()))) - - if package_format == "deb" and cross_arch: - deb_arch = Util.native_arch_to_deb_arch(cross_arch) - abi = Util.native_arch_to_abi(cross_arch) - lib = Util.native_arch_to_lib(cross_arch) - gcc = "gcc-" + abi - varmap["cross_arch"] = deb_arch - pkg_names = [p + ":" + deb_arch for p in cross_pkgs.values()] - pkg_names.append(gcc) - varmap["cross_pkgs"] = pkg_align[1:] + pkg_align.join(sorted(set(pkg_names))) - varmap["cross_abi"] = abi - varmap["cross_lib"] = lib - - if pip_pkgs: - varmap["pip_pkgs"] = pip_pkg_align[1:] + pip_pkg_align.join(sorted(set(pip_pkgs.values()))) - + varmap = self._dockerfile_build_varmap(facts, mappings, pip_mappings, projects, cross_arch) self._dockerfile_format(facts, cross_arch, varmap) def run(self): -- 2.24.1