1#!/usr/bin/env python3 2# 3# Conversion script to add new override syntax to existing bitbake metadata 4# 5# Copyright (C) 2021 Richard Purdie 6# 7# SPDX-License-Identifier: GPL-2.0-only 8# 9 10# 11# To use this script on a new layer you need to list the overrides the 12# layer is known to use in the list below. 13# 14# Known constraint: Matching is 'loose' and in particular will find variable 15# and function names with "_append" and "_remove" in them. Those need to be 16# filtered out manually or in the skip list below. 17# 18 19import re 20import os 21import sys 22import tempfile 23import shutil 24import mimetypes 25import argparse 26 27parser = argparse.ArgumentParser(description="Convert override syntax") 28parser.add_argument("--override", "-o", action="append", default=[], help="Add additional strings to consider as an override (e.g. custom machines/distros") 29parser.add_argument("--skip", "-s", action="append", default=[], help="Add additional string to skip and not consider an override") 30parser.add_argument("--skip-ext", "-e", action="append", default=[], help="Additional file suffixes to skip when processing (e.g. '.foo')") 31parser.add_argument("--package-vars", action="append", default=[], help="Additional variables to treat as package variables") 32parser.add_argument("--image-vars", action="append", default=[], help="Additional variables to treat as image variables") 33parser.add_argument("--short-override", action="append", default=[], help="Additional strings to treat as short overrides") 34parser.add_argument("path", nargs="+", help="Paths to convert") 35 36args = parser.parse_args() 37 38# List of strings to treat as overrides 39vars = args.override 40vars += ["append", "prepend", "remove"] 41vars += ["qemuarm", "qemux86", "qemumips", "qemuppc", "qemuriscv", "qemuall"] 42vars += ["genericx86", "edgerouter", "beaglebone-yocto"] 43vars += ["armeb", "arm", "armv5", "armv6", "armv4", "powerpc64", "aarch64", "riscv32", "riscv64", "x86", "mips64", "powerpc"] 44vars += ["mipsarch", "x86-x32", "mips16e", "microblaze", "e5500-64b", "mipsisa32", "mipsisa64"] 45vars += ["class-native", "class-target", "class-cross-canadian", "class-cross", "class-devupstream"] 46vars += ["tune-", "pn-", "forcevariable"] 47vars += ["libc-musl", "libc-glibc", "libc-newlib","libc-baremetal"] 48vars += ["task-configure", "task-compile", "task-install", "task-clean", "task-image-qa", "task-rm_work", "task-image-complete", "task-populate-sdk"] 49vars += ["toolchain-clang", "mydistro", "nios2", "sdkmingw32", "overrideone", "overridetwo"] 50vars += ["linux-gnux32", "linux-muslx32", "linux-gnun32", "mingw32", "poky", "darwin", "linuxstdbase"] 51vars += ["linux-gnueabi", "eabi"] 52vars += ["virtclass-multilib", "virtclass-mcextend"] 53 54# List of strings to treat as overrides but only with whitespace following or another override (more restricted matching). 55# Handles issues with arc matching arch. 56shortvars = ["arc", "mips", "mipsel", "sh4"] + args.short_override 57 58# Variables which take packagenames as an override 59packagevars = ["FILES", "RDEPENDS", "RRECOMMENDS", "SUMMARY", "DESCRIPTION", "RSUGGESTS", "RPROVIDES", "RCONFLICTS", "PKG", "ALLOW_EMPTY", 60 "pkg_postrm", "pkg_postinst_ontarget", "pkg_postinst", "INITSCRIPT_NAME", "INITSCRIPT_PARAMS", "DEBIAN_NOAUTONAME", "ALTERNATIVE", 61 "PKGE", "PKGV", "PKGR", "USERADD_PARAM", "GROUPADD_PARAM", "CONFFILES", "SYSTEMD_SERVICE", "LICENSE", "SECTION", "pkg_preinst", 62 "pkg_prerm", "RREPLACES", "GROUPMEMS_PARAM", "SYSTEMD_AUTO_ENABLE", "SKIP_FILEDEPS", "PRIVATE_LIBS", "PACKAGE_ADD_METADATA", 63 "INSANE_SKIP", "DEBIANNAME", "SYSTEMD_SERVICE_ESCAPED"] + args.package_vars 64 65# Expressions to skip if encountered, these are not overrides 66skips = args.skip 67skips += ["parser_append", "recipe_to_append", "extra_append", "to_remove", "show_appends", "applied_appends", "file_appends", "handle_remove"] 68skips += ["expanded_removes", "color_remove", "test_remove", "empty_remove", "toaster_prepend", "num_removed", "licfiles_append", "_write_append"] 69skips += ["no_report_remove", "test_prepend", "test_append", "multiple_append", "test_remove", "shallow_remove", "do_remove_layer", "first_append"] 70skips += ["parser_remove", "to_append", "no_remove", "bblayers_add_remove", "bblayers_remove", "apply_append", "is_x86", "base_dep_prepend"] 71skips += ["autotools_dep_prepend", "go_map_arm", "alt_remove_links", "systemd_append_file", "file_append", "process_file_darwin"] 72skips += ["run_loaddata_poky", "determine_if_poky_env", "do_populate_poky_src", "libc_cv_include_x86_isa_level", "test_rpm_remove", "do_install_armmultilib"] 73skips += ["get_appends_for_files", "test_doubleref_remove", "test_bitbakelayers_add_remove", "elf32_x86_64", "colour_remove", "revmap_remove"] 74skips += ["test_rpm_remove", "test_bitbakelayers_add_remove", "recipe_append_file", "log_data_removed", "recipe_append", "systemd_machine_unit_append"] 75skips += ["recipetool_append", "changetype_remove", "try_appendfile_wc", "test_qemux86_directdisk", "test_layer_appends", "tgz_removed"] 76 77imagevars = ["IMAGE_CMD", "EXTRA_IMAGECMD", "IMAGE_TYPEDEP", "CONVERSION_CMD", "COMPRESS_CMD"] + args.image_vars 78packagevars += imagevars 79 80skip_ext = [".html", ".patch", ".m4", ".diff"] + args.skip_ext 81 82vars_re = {} 83for exp in vars: 84 vars_re[exp] = (re.compile(r'((^|[#\'"\s\-\+])[A-Za-z0-9_\-:${}\.]+)_' + exp), r"\1:" + exp) 85 86shortvars_re = {} 87for exp in shortvars: 88 shortvars_re[exp] = (re.compile(r'((^|[#\'"\s\-\+])[A-Za-z0-9_\-:${}\.]+)_' + exp + r'([\(\'"\s:])'), r"\1:" + exp + r"\3") 89 90package_re = {} 91for exp in packagevars: 92 package_re[exp] = (re.compile(r'(^|[#\'"\s\-\+]+)' + exp + r'_' + r'([$a-z"\'\s%\[<{\\\*].)'), r"\1" + exp + r":\2") 93 94# Other substitutions to make 95subs = { 96 'r = re.compile(r"([^:]+):\s*(.*)")' : 'r = re.compile(r"(^.+?):\s+(.*)")', 97 "val = d.getVar('%s_%s' % (var, pkg))" : "val = d.getVar('%s:%s' % (var, pkg))", 98 "f.write('%s_%s: %s\\n' % (var, pkg, encode(val)))" : "f.write('%s:%s: %s\\n' % (var, pkg, encode(val)))", 99 "d.getVar('%s_%s' % (scriptlet_name, pkg))" : "d.getVar('%s:%s' % (scriptlet_name, pkg))", 100 'ret.append(v + "_" + p)' : 'ret.append(v + ":" + p)', 101} 102 103def processfile(fn): 104 print("processing file '%s'" % fn) 105 try: 106 fh, abs_path = tempfile.mkstemp() 107 with os.fdopen(fh, 'w') as new_file: 108 with open(fn, "r") as old_file: 109 for line in old_file: 110 skip = False 111 for s in skips: 112 if s in line: 113 skip = True 114 if "ptest_append" in line or "ptest_remove" in line or "ptest_prepend" in line: 115 skip = False 116 for sub in subs: 117 if sub in line: 118 line = line.replace(sub, subs[sub]) 119 skip = True 120 if not skip: 121 for pvar in packagevars: 122 line = package_re[pvar][0].sub(package_re[pvar][1], line) 123 for var in vars: 124 line = vars_re[var][0].sub(vars_re[var][1], line) 125 for shortvar in shortvars: 126 line = shortvars_re[shortvar][0].sub(shortvars_re[shortvar][1], line) 127 if "pkg_postinst:ontarget" in line: 128 line = line.replace("pkg_postinst:ontarget", "pkg_postinst_ontarget") 129 new_file.write(line) 130 shutil.copymode(fn, abs_path) 131 os.remove(fn) 132 shutil.move(abs_path, fn) 133 except UnicodeDecodeError: 134 pass 135 136ourname = os.path.basename(sys.argv[0]) 137ourversion = "0.9.3" 138 139for p in args.path: 140 if os.path.isfile(p): 141 processfile(p) 142 else: 143 print("processing directory '%s'" % p) 144 for root, dirs, files in os.walk(p): 145 for name in files: 146 if name == ourname: 147 continue 148 fn = os.path.join(root, name) 149 if os.path.islink(fn): 150 continue 151 if "/.git/" in fn or any(fn.endswith(ext) for ext in skip_ext): 152 continue 153 processfile(fn) 154 155print("All files processed with version %s" % ourversion) 156