Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 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 | |
| 19 | import re |
| 20 | import os |
| 21 | import sys |
| 22 | import tempfile |
| 23 | import shutil |
| 24 | import mimetypes |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 25 | import argparse |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 26 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 27 | parser = argparse.ArgumentParser(description="Convert override syntax") |
| 28 | parser.add_argument("--override", "-o", action="append", default=[], help="Add additional strings to consider as an override (e.g. custom machines/distros") |
| 29 | parser.add_argument("--skip", "-s", action="append", default=[], help="Add additional string to skip and not consider an override") |
| 30 | parser.add_argument("--skip-ext", "-e", action="append", default=[], help="Additional file suffixes to skip when processing (e.g. '.foo')") |
| 31 | parser.add_argument("--package-vars", action="append", default=[], help="Additional variables to treat as package variables") |
| 32 | parser.add_argument("--image-vars", action="append", default=[], help="Additional variables to treat as image variables") |
| 33 | parser.add_argument("--short-override", action="append", default=[], help="Additional strings to treat as short overrides") |
| 34 | parser.add_argument("path", nargs="+", help="Paths to convert") |
| 35 | |
| 36 | args = parser.parse_args() |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 37 | |
| 38 | # List of strings to treat as overrides |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 39 | vars = args.override |
| 40 | vars += ["append", "prepend", "remove"] |
| 41 | vars += ["qemuarm", "qemux86", "qemumips", "qemuppc", "qemuriscv", "qemuall"] |
| 42 | vars += ["genericx86", "edgerouter", "beaglebone-yocto"] |
| 43 | vars += ["armeb", "arm", "armv5", "armv6", "armv4", "powerpc64", "aarch64", "riscv32", "riscv64", "x86", "mips64", "powerpc"] |
| 44 | vars += ["mipsarch", "x86-x32", "mips16e", "microblaze", "e5500-64b", "mipsisa32", "mipsisa64"] |
| 45 | vars += ["class-native", "class-target", "class-cross-canadian", "class-cross", "class-devupstream"] |
| 46 | vars += ["tune-", "pn-", "forcevariable"] |
| 47 | vars += ["libc-musl", "libc-glibc", "libc-newlib","libc-baremetal"] |
| 48 | vars += ["task-configure", "task-compile", "task-install", "task-clean", "task-image-qa", "task-rm_work", "task-image-complete", "task-populate-sdk"] |
| 49 | vars += ["toolchain-clang", "mydistro", "nios2", "sdkmingw32", "overrideone", "overridetwo"] |
| 50 | vars += ["linux-gnux32", "linux-muslx32", "linux-gnun32", "mingw32", "poky", "darwin", "linuxstdbase"] |
| 51 | vars += ["linux-gnueabi", "eabi"] |
| 52 | vars += ["virtclass-multilib", "virtclass-mcextend"] |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 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. |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 56 | shortvars = ["arc", "mips", "mipsel", "sh4"] + args.short_override |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 57 | |
| 58 | # Variables which take packagenames as an override |
| 59 | packagevars = ["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", |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 63 | "INSANE_SKIP", "DEBIANNAME", "SYSTEMD_SERVICE_ESCAPED"] + args.package_vars |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 64 | |
| 65 | # Expressions to skip if encountered, these are not overrides |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 66 | skips = args.skip |
| 67 | skips += ["parser_append", "recipe_to_append", "extra_append", "to_remove", "show_appends", "applied_appends", "file_appends", "handle_remove"] |
| 68 | skips += ["expanded_removes", "color_remove", "test_remove", "empty_remove", "toaster_prepend", "num_removed", "licfiles_append", "_write_append"] |
| 69 | skips += ["no_report_remove", "test_prepend", "test_append", "multiple_append", "test_remove", "shallow_remove", "do_remove_layer", "first_append"] |
| 70 | skips += ["parser_remove", "to_append", "no_remove", "bblayers_add_remove", "bblayers_remove", "apply_append", "is_x86", "base_dep_prepend"] |
| 71 | skips += ["autotools_dep_prepend", "go_map_arm", "alt_remove_links", "systemd_append_file", "file_append", "process_file_darwin"] |
| 72 | skips += ["run_loaddata_poky", "determine_if_poky_env", "do_populate_poky_src", "libc_cv_include_x86_isa_level", "test_rpm_remove", "do_install_armmultilib"] |
| 73 | skips += ["get_appends_for_files", "test_doubleref_remove", "test_bitbakelayers_add_remove", "elf32_x86_64", "colour_remove", "revmap_remove"] |
| 74 | skips += ["test_rpm_remove", "test_bitbakelayers_add_remove", "recipe_append_file", "log_data_removed", "recipe_append", "systemd_machine_unit_append"] |
| 75 | skips += ["recipetool_append", "changetype_remove", "try_appendfile_wc", "test_qemux86_directdisk", "test_layer_appends", "tgz_removed"] |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 76 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 77 | imagevars = ["IMAGE_CMD", "EXTRA_IMAGECMD", "IMAGE_TYPEDEP", "CONVERSION_CMD", "COMPRESS_CMD"] + args.image_vars |
| 78 | packagevars += imagevars |
| 79 | |
| 80 | skip_ext = [".html", ".patch", ".m4", ".diff"] + args.skip_ext |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 81 | |
| 82 | vars_re = {} |
| 83 | for exp in vars: |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 84 | vars_re[exp] = (re.compile('((^|[#\'"\s\-\+])[A-Za-z0-9_\-:${}\.]+)_' + exp), r"\1:" + exp) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 85 | |
| 86 | shortvars_re = {} |
| 87 | for exp in shortvars: |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 88 | shortvars_re[exp] = (re.compile('((^|[#\'"\s\-\+])[A-Za-z0-9_\-:${}\.]+)_' + exp + '([\(\'"\s:])'), r"\1:" + exp + r"\3") |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 89 | |
| 90 | package_re = {} |
| 91 | for exp in packagevars: |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 92 | package_re[exp] = (re.compile('(^|[#\'"\s\-\+]+)' + exp + '_' + '([$a-z"\'\s%\[<{\\\*].)'), r"\1" + exp + r":\2") |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 93 | |
| 94 | # Other substitutions to make |
| 95 | subs = { |
| 96 | 'r = re.compile("([^:]+):\s*(.*)")' : 'r = re.compile("(^.+?):\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 | |
| 103 | def 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 | |
| 136 | ourname = os.path.basename(sys.argv[0]) |
| 137 | ourversion = "0.9.3" |
| 138 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 139 | for 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) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 154 | |
| 155 | print("All files processed with version %s" % ourversion) |