1#!/usr/bin/env python3
2#
3# Conversion script to rename variables to versions with improved terminology.
4# Also highlights potentially problematic language and removed variables.
5#
6# Copyright (C) 2021 Richard Purdie
7# Copyright (C) 2022 Wind River Systems, Inc.
8#
9# SPDX-License-Identifier: GPL-2.0-only
10#
11
12import re
13import os
14import sys
15import tempfile
16import shutil
17import mimetypes
18
19if len(sys.argv) < 2:
20    print("Please specify a directory to run the conversion script against.")
21    sys.exit(1)
22
23renames = {
24"BB_ENV_WHITELIST" : "BB_ENV_PASSTHROUGH",
25"BB_ENV_EXTRAWHITE" : "BB_ENV_PASSTHROUGH_ADDITIONS",
26"BB_HASHCONFIG_WHITELIST" : "BB_HASHCONFIG_IGNORE_VARS",
27"BB_SETSCENE_ENFORCE_WHITELIST" : "BB_SETSCENE_ENFORCE_IGNORE_TASKS",
28"BB_HASHBASE_WHITELIST" : "BB_BASEHASH_IGNORE_VARS",
29"BB_HASHTASK_WHITELIST" : "BB_TASKHASH_IGNORE_TASKS",
30"CVE_CHECK_PN_WHITELIST" : "CVE_CHECK_SKIP_RECIPE",
31"CVE_CHECK_WHITELIST" : "CVE_CHECK_IGNORE",
32"MULTI_PROVIDER_WHITELIST" : "BB_MULTI_PROVIDER_ALLOWED",
33"PNBLACKLIST" : "SKIP_RECIPE",
34"SDK_LOCAL_CONF_BLACKLIST" : "ESDK_LOCALCONF_REMOVE",
35"SDK_LOCAL_CONF_WHITELIST" : "ESDK_LOCALCONF_ALLOW",
36"SDK_INHERIT_BLACKLIST" : "ESDK_CLASS_INHERIT_DISABLE",
37"SSTATE_DUPWHITELIST" : "SSTATE_ALLOW_OVERLAP_FILES",
38"SYSROOT_DIRS_BLACKLIST" : "SYSROOT_DIRS_IGNORE",
39"UNKNOWN_CONFIGURE_WHITELIST" : "UNKNOWN_CONFIGURE_OPT_IGNORE",
40"ICECC_USER_CLASS_BL" : "ICECC_CLASS_DISABLE",
41"ICECC_SYSTEM_CLASS_BL" : "ICECC_CLASS_DISABLE",
42"ICECC_USER_PACKAGE_WL" : "ICECC_RECIPE_ENABLE",
43"ICECC_USER_PACKAGE_BL" : "ICECC_RECIPE_DISABLE",
44"ICECC_SYSTEM_PACKAGE_BL" : "ICECC_RECIPE_DISABLE",
45"LICENSE_FLAGS_WHITELIST" : "LICENSE_FLAGS_ACCEPTED",
46}
47
48removed_list = [
49"BB_STAMP_WHITELIST",
50"BB_STAMP_POLICY",
51"INHERIT_BLACKLIST",
52"TUNEABI_WHITELIST",
53]
54
55context_check_list = [
56"blacklist",
57"whitelist",
58"abort",
59]
60
61def processfile(fn):
62
63    print("processing file '%s'" % fn)
64    try:
65        fh, abs_path = tempfile.mkstemp()
66        modified = False
67        with os.fdopen(fh, 'w') as new_file:
68            with open(fn, "r") as old_file:
69                lineno = 0
70                for line in old_file:
71                    lineno += 1
72                    if not line or "BB_RENAMED_VARIABLE" in line:
73                        continue
74                    # Do the renames
75                    for old_name, new_name in renames.items():
76                        if old_name in line:
77                            line = line.replace(old_name, new_name)
78                            modified = True
79                    # Find removed names
80                    for removed_name in removed_list:
81                        if removed_name in line:
82                            print("%s needs further work at line %s because %s has been deprecated" % (fn, lineno, removed_name))
83                    for check_word in context_check_list:
84                        if re.search(check_word, line, re.IGNORECASE):
85                            print("%s needs further work at line %s since it contains %s"% (fn, lineno, check_word))
86                    new_file.write(line)
87            new_file.close()
88            if modified:
89                print("*** Modified file '%s'" % (fn))
90                shutil.copymode(fn, abs_path)
91                os.remove(fn)
92                shutil.move(abs_path, fn)
93    except UnicodeDecodeError:
94        pass
95
96ourname = os.path.basename(sys.argv[0])
97ourversion = "0.1"
98
99if os.path.isfile(sys.argv[1]):
100    processfile(sys.argv[1])
101    sys.exit(0)
102
103for targetdir in sys.argv[1:]:
104    print("processing directory '%s'" % targetdir)
105    for root, dirs, files in os.walk(targetdir):
106        for name in files:
107            if name == ourname:
108                continue
109            fn = os.path.join(root, name)
110            if os.path.islink(fn):
111                continue
112            if "ChangeLog" in fn or "/.git/" in fn or fn.endswith(".html") or fn.endswith(".patch") or fn.endswith(".m4") or fn.endswith(".diff") or fn.endswith(".orig"):
113                continue
114            processfile(fn)
115
116print("All files processed with version %s" % ourversion)
117