takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/tools/misc/remove-device-h.py@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | import json |
kadonotakashi | 0:8fdf9a60065b | 2 | import os |
kadonotakashi | 0:8fdf9a60065b | 3 | import stat |
kadonotakashi | 0:8fdf9a60065b | 4 | import re |
kadonotakashi | 0:8fdf9a60065b | 5 | from collections import OrderedDict |
kadonotakashi | 0:8fdf9a60065b | 6 | from subprocess import Popen |
kadonotakashi | 0:8fdf9a60065b | 7 | |
kadonotakashi | 0:8fdf9a60065b | 8 | git_processes = [] |
kadonotakashi | 0:8fdf9a60065b | 9 | |
kadonotakashi | 0:8fdf9a60065b | 10 | class MyJSONEncoder(json.JSONEncoder): |
kadonotakashi | 0:8fdf9a60065b | 11 | def __init__(self, *args, **kwargs): |
kadonotakashi | 0:8fdf9a60065b | 12 | super(MyJSONEncoder, self).__init__(*args, **kwargs) |
kadonotakashi | 0:8fdf9a60065b | 13 | self.current_indent = 0 |
kadonotakashi | 0:8fdf9a60065b | 14 | self.current_indent_str = "" |
kadonotakashi | 0:8fdf9a60065b | 15 | |
kadonotakashi | 0:8fdf9a60065b | 16 | |
kadonotakashi | 0:8fdf9a60065b | 17 | def encode(self, o): |
kadonotakashi | 0:8fdf9a60065b | 18 | #Special Processing for lists |
kadonotakashi | 0:8fdf9a60065b | 19 | if isinstance(o, (list, tuple)): |
kadonotakashi | 0:8fdf9a60065b | 20 | primitives_only = True |
kadonotakashi | 0:8fdf9a60065b | 21 | for item in o: |
kadonotakashi | 0:8fdf9a60065b | 22 | if isinstance(item, (list, tuple, dict)): |
kadonotakashi | 0:8fdf9a60065b | 23 | primitives_only = False |
kadonotakashi | 0:8fdf9a60065b | 24 | break |
kadonotakashi | 0:8fdf9a60065b | 25 | output = [] |
kadonotakashi | 0:8fdf9a60065b | 26 | if primitives_only: |
kadonotakashi | 0:8fdf9a60065b | 27 | for item in o: |
kadonotakashi | 0:8fdf9a60065b | 28 | output.append(json.dumps(item)) |
kadonotakashi | 0:8fdf9a60065b | 29 | return "[" + ", ".join(output) + "]" |
kadonotakashi | 0:8fdf9a60065b | 30 | else: |
kadonotakashi | 0:8fdf9a60065b | 31 | self.current_indent += self.indent |
kadonotakashi | 0:8fdf9a60065b | 32 | self.current_indent_str = " " * self.current_indent |
kadonotakashi | 0:8fdf9a60065b | 33 | for item in o: |
kadonotakashi | 0:8fdf9a60065b | 34 | output.append(self.current_indent_str + self.encode(item)) |
kadonotakashi | 0:8fdf9a60065b | 35 | self.current_indent -= self.indent |
kadonotakashi | 0:8fdf9a60065b | 36 | self.current_indent_str = " " * self.current_indent |
kadonotakashi | 0:8fdf9a60065b | 37 | return "[\n" + ",\n".join(output) + "\n" + self.current_indent_str + "]" |
kadonotakashi | 0:8fdf9a60065b | 38 | elif isinstance(o, dict): |
kadonotakashi | 0:8fdf9a60065b | 39 | primitives_only = True |
kadonotakashi | 0:8fdf9a60065b | 40 | for item in o.values(): |
kadonotakashi | 0:8fdf9a60065b | 41 | if isinstance(item, (list, tuple, dict)): |
kadonotakashi | 0:8fdf9a60065b | 42 | primitives_only = False |
kadonotakashi | 0:8fdf9a60065b | 43 | break |
kadonotakashi | 0:8fdf9a60065b | 44 | output = [] |
kadonotakashi | 0:8fdf9a60065b | 45 | if primitives_only and len(o) < 3: |
kadonotakashi | 0:8fdf9a60065b | 46 | for key, value in o.items(): |
kadonotakashi | 0:8fdf9a60065b | 47 | output.append(json.dumps(key) + ": " + self.encode(value)) |
kadonotakashi | 0:8fdf9a60065b | 48 | return "{" + ", ".join(output) + "}" |
kadonotakashi | 0:8fdf9a60065b | 49 | else: |
kadonotakashi | 0:8fdf9a60065b | 50 | self.current_indent += self.indent |
kadonotakashi | 0:8fdf9a60065b | 51 | self.current_indent_str = " " * self.current_indent |
kadonotakashi | 0:8fdf9a60065b | 52 | for key, value in o.items(): |
kadonotakashi | 0:8fdf9a60065b | 53 | output.append(self.current_indent_str + json.dumps(key) + ": " + self.encode(value)) |
kadonotakashi | 0:8fdf9a60065b | 54 | self.current_indent -= self.indent |
kadonotakashi | 0:8fdf9a60065b | 55 | self.current_indent_str = " " * self.current_indent |
kadonotakashi | 0:8fdf9a60065b | 56 | return "{\n" + ",\n".join(output) + "\n" + self.current_indent_str + "}" |
kadonotakashi | 0:8fdf9a60065b | 57 | else: |
kadonotakashi | 0:8fdf9a60065b | 58 | return json.dumps(o) |
kadonotakashi | 0:8fdf9a60065b | 59 | |
kadonotakashi | 0:8fdf9a60065b | 60 | def load(path): |
kadonotakashi | 0:8fdf9a60065b | 61 | with open(path, 'r') as f : |
kadonotakashi | 0:8fdf9a60065b | 62 | return json.load(f, object_pairs_hook=OrderedDict) |
kadonotakashi | 0:8fdf9a60065b | 63 | |
kadonotakashi | 0:8fdf9a60065b | 64 | def dump(path, obj): |
kadonotakashi | 0:8fdf9a60065b | 65 | with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT, stat.S_IRUSR | stat.S_IWUSR), 'w') as f : |
kadonotakashi | 0:8fdf9a60065b | 66 | os.chmod(path, stat.S_IRUSR | stat.S_IWUSR) |
kadonotakashi | 0:8fdf9a60065b | 67 | f.write(MyJSONEncoder(indent=4).encode(obj)) |
kadonotakashi | 0:8fdf9a60065b | 68 | f.write(u'\n') |
kadonotakashi | 0:8fdf9a60065b | 69 | f.truncate() |
kadonotakashi | 0:8fdf9a60065b | 70 | |
kadonotakashi | 0:8fdf9a60065b | 71 | def find(stem, path) : |
kadonotakashi | 0:8fdf9a60065b | 72 | for root, directories, files in os.walk(path, followlinks=True) : |
kadonotakashi | 0:8fdf9a60065b | 73 | [dir for dir in directories if dir[0] != '.'] |
kadonotakashi | 0:8fdf9a60065b | 74 | if (stem_match(stem,os.path.basename(os.path.normpath(root))) and |
kadonotakashi | 0:8fdf9a60065b | 75 | "device.h" in files) : |
kadonotakashi | 0:8fdf9a60065b | 76 | return os.path.join(root, "device.h") |
kadonotakashi | 0:8fdf9a60065b | 77 | |
kadonotakashi | 0:8fdf9a60065b | 78 | def find_all_devices(path, verbose=False) : |
kadonotakashi | 0:8fdf9a60065b | 79 | for root, directories, files in os.walk(path, followlinks=True) : |
kadonotakashi | 0:8fdf9a60065b | 80 | [dir for dir in directories if dir[0] != '.'] |
kadonotakashi | 0:8fdf9a60065b | 81 | if "device.h" in files : |
kadonotakashi | 0:8fdf9a60065b | 82 | if verbose : print("[VERBOSE] found a device.h file in {}".format(root)) |
kadonotakashi | 0:8fdf9a60065b | 83 | yield os.path.join(root, "device.h") |
kadonotakashi | 0:8fdf9a60065b | 84 | |
kadonotakashi | 0:8fdf9a60065b | 85 | mbed_matcher = re.compile('mbed', re.IGNORECASE) |
kadonotakashi | 0:8fdf9a60065b | 86 | def stem_match(stem, thing) : |
kadonotakashi | 0:8fdf9a60065b | 87 | return (stem in thing or |
kadonotakashi | 0:8fdf9a60065b | 88 | re.sub(mbed_matcher, '', stem) in thing) |
kadonotakashi | 0:8fdf9a60065b | 89 | |
kadonotakashi | 0:8fdf9a60065b | 90 | attr_matcher = re.compile('^#define\W+DEVICE_(\w+)\W+1.*$') |
kadonotakashi | 0:8fdf9a60065b | 91 | def parse_attributes(path) : |
kadonotakashi | 0:8fdf9a60065b | 92 | with open(path) as input : |
kadonotakashi | 0:8fdf9a60065b | 93 | for line in input : |
kadonotakashi | 0:8fdf9a60065b | 94 | m = re.match(attr_matcher, line) |
kadonotakashi | 0:8fdf9a60065b | 95 | if m: yield m.group(1) |
kadonotakashi | 0:8fdf9a60065b | 96 | |
kadonotakashi | 0:8fdf9a60065b | 97 | remove_matcher = re.compile('^#define\W+DEVICE_(\w+)\W+[10].*$') |
kadonotakashi | 0:8fdf9a60065b | 98 | def remove_attributes(path) : |
kadonotakashi | 0:8fdf9a60065b | 99 | with open(path) as input : |
kadonotakashi | 0:8fdf9a60065b | 100 | remainder = filter(lambda l: not re.match(remove_matcher, l), input) |
kadonotakashi | 0:8fdf9a60065b | 101 | with open(path,"wb") as output : |
kadonotakashi | 0:8fdf9a60065b | 102 | output.truncate(0) |
kadonotakashi | 0:8fdf9a60065b | 103 | output.write("// The 'provides' section in 'target.json' is now used"+ |
kadonotakashi | 0:8fdf9a60065b | 104 | " to create the device's hardware preprocessor switches.\n") |
kadonotakashi | 0:8fdf9a60065b | 105 | output.write("// Check the 'provides' section of the target description"+ |
kadonotakashi | 0:8fdf9a60065b | 106 | " in 'targets.json' for more details.\n") |
kadonotakashi | 0:8fdf9a60065b | 107 | output.writelines(remainder) |
kadonotakashi | 0:8fdf9a60065b | 108 | |
kadonotakashi | 0:8fdf9a60065b | 109 | def user_select(things, message) : |
kadonotakashi | 0:8fdf9a60065b | 110 | print(message) |
kadonotakashi | 0:8fdf9a60065b | 111 | for thing, number in zip(things, range(len(things))): |
kadonotakashi | 0:8fdf9a60065b | 112 | print("{} : {}".format(number, thing)) |
kadonotakashi | 0:8fdf9a60065b | 113 | selection = None |
kadonotakashi | 0:8fdf9a60065b | 114 | while selection is None : |
kadonotakashi | 0:8fdf9a60065b | 115 | print("please select an integer [0..{}] or specify all".format(len(things) - 1)) |
kadonotakashi | 0:8fdf9a60065b | 116 | try : |
kadonotakashi | 0:8fdf9a60065b | 117 | i = raw_input() |
kadonotakashi | 0:8fdf9a60065b | 118 | if i == "all" : |
kadonotakashi | 0:8fdf9a60065b | 119 | selection = "all" |
kadonotakashi | 0:8fdf9a60065b | 120 | else : |
kadonotakashi | 0:8fdf9a60065b | 121 | selection = int(i) |
kadonotakashi | 0:8fdf9a60065b | 122 | if (selection > len(things) or |
kadonotakashi | 0:8fdf9a60065b | 123 | selection < 0) : |
kadonotakashi | 0:8fdf9a60065b | 124 | print("selection {} out of range".format(selection)) |
kadonotakashi | 0:8fdf9a60065b | 125 | selection = None |
kadonotakashi | 0:8fdf9a60065b | 126 | except (ValueError, SyntaxError) : |
kadonotakashi | 0:8fdf9a60065b | 127 | print("selection not understood") |
kadonotakashi | 0:8fdf9a60065b | 128 | if selection == "all" : |
kadonotakashi | 0:8fdf9a60065b | 129 | return things |
kadonotakashi | 0:8fdf9a60065b | 130 | else : |
kadonotakashi | 0:8fdf9a60065b | 131 | return [things[selection]] |
kadonotakashi | 0:8fdf9a60065b | 132 | |
kadonotakashi | 0:8fdf9a60065b | 133 | target_matcher = re.compile("TARGET_") |
kadonotakashi | 0:8fdf9a60065b | 134 | def strip_target(str) : |
kadonotakashi | 0:8fdf9a60065b | 135 | return re.sub(target_matcher, "", str) |
kadonotakashi | 0:8fdf9a60065b | 136 | |
kadonotakashi | 0:8fdf9a60065b | 137 | def add_to_targets(targets, device_file, verbose=False, remove=False) : |
kadonotakashi | 0:8fdf9a60065b | 138 | if verbose : print("[VERBOSE] trying target {}".format(device_file)) |
kadonotakashi | 0:8fdf9a60065b | 139 | device = strip_target(os.path.basename(os.path.normpath(os.path.dirname(device_file)))) |
kadonotakashi | 0:8fdf9a60065b | 140 | if not device : |
kadonotakashi | 0:8fdf9a60065b | 141 | print("[WARNING] device {} did not have an associated device.h".format(device)) |
kadonotakashi | 0:8fdf9a60065b | 142 | else : |
kadonotakashi | 0:8fdf9a60065b | 143 | possible_matches = set([key for key in targets.keys() if stem_match(device, key)]) |
kadonotakashi | 0:8fdf9a60065b | 144 | for key, value in targets.items() : |
kadonotakashi | 0:8fdf9a60065b | 145 | for alt in value['extra_labels'] if 'extra_labels' in value else [] : |
kadonotakashi | 0:8fdf9a60065b | 146 | if stem_match(device, alt) : possible_matches.add(key) |
kadonotakashi | 0:8fdf9a60065b | 147 | for alt in value['extra_labels_add'] if 'extra_labels_add' in value else [] : |
kadonotakashi | 0:8fdf9a60065b | 148 | if stem_match(device, alt) : possible_matches.add(key) |
kadonotakashi | 0:8fdf9a60065b | 149 | possible_matches = list(possible_matches) |
kadonotakashi | 0:8fdf9a60065b | 150 | for match in possible_matches : |
kadonotakashi | 0:8fdf9a60065b | 151 | if device == match : possible_matches = [match] |
kadonotakashi | 0:8fdf9a60065b | 152 | if not possible_matches : |
kadonotakashi | 0:8fdf9a60065b | 153 | print("[WARNING] device {} did not have an associated entry in targets.json".format(device)) |
kadonotakashi | 0:8fdf9a60065b | 154 | return None |
kadonotakashi | 0:8fdf9a60065b | 155 | elif len(possible_matches) > 1 : |
kadonotakashi | 0:8fdf9a60065b | 156 | message = ("possible matches for file {}".format(device_file)) |
kadonotakashi | 0:8fdf9a60065b | 157 | target = user_select(possible_matches, message) |
kadonotakashi | 0:8fdf9a60065b | 158 | else : |
kadonotakashi | 0:8fdf9a60065b | 159 | target = possible_matches |
kadonotakashi | 0:8fdf9a60065b | 160 | attrs = list(parse_attributes(device_file)) |
kadonotakashi | 0:8fdf9a60065b | 161 | if attrs : |
kadonotakashi | 0:8fdf9a60065b | 162 | for t in target : |
kadonotakashi | 0:8fdf9a60065b | 163 | targets[t]["device_has"] = sorted(list(set(targets[t].setdefault("device_has",[]) + attrs))) |
kadonotakashi | 0:8fdf9a60065b | 164 | if verbose : print("[VERBOSE] target {} now device_has {}".format(t, attrs)) |
kadonotakashi | 0:8fdf9a60065b | 165 | if remove is True: |
kadonotakashi | 0:8fdf9a60065b | 166 | remove_attributes(device_file) |
kadonotakashi | 0:8fdf9a60065b | 167 | |
kadonotakashi | 0:8fdf9a60065b | 168 | if __name__ == '__main__' : |
kadonotakashi | 0:8fdf9a60065b | 169 | import argparse |
kadonotakashi | 0:8fdf9a60065b | 170 | parser = argparse.ArgumentParser(description='A helpful little script for converting' + |
kadonotakashi | 0:8fdf9a60065b | 171 | ' device.h files to parts of the targets.json file') |
kadonotakashi | 0:8fdf9a60065b | 172 | parser.add_argument('-a', '--all', action='store_true', |
kadonotakashi | 0:8fdf9a60065b | 173 | help='find and convert all available device.h files in the'+ |
kadonotakashi | 0:8fdf9a60065b | 174 | ' directory tree starting at the current directory') |
kadonotakashi | 0:8fdf9a60065b | 175 | parser.add_argument('-f', '--file', nargs='+', help='specify an individual file to '+ |
kadonotakashi | 0:8fdf9a60065b | 176 | 'convert from device.h format to a piece of targets.json') |
kadonotakashi | 0:8fdf9a60065b | 177 | parser.add_argument('-t', '--target', nargs='+', help='specify an individual target'+ |
kadonotakashi | 0:8fdf9a60065b | 178 | ' to convert from device.h format to a piece of targets.json') |
kadonotakashi | 0:8fdf9a60065b | 179 | parser.add_argument('-v', '--verbose', action='store_true', |
kadonotakashi | 0:8fdf9a60065b | 180 | help="print out every target that is updated in the targets.json") |
kadonotakashi | 0:8fdf9a60065b | 181 | parser.add_argument('-r', '--rm', action='store_true', |
kadonotakashi | 0:8fdf9a60065b | 182 | help="remove the used attributes from a device.h file") |
kadonotakashi | 0:8fdf9a60065b | 183 | args = parser.parse_args() |
kadonotakashi | 0:8fdf9a60065b | 184 | if not args.target and not args.file and not args.all : |
kadonotakashi | 0:8fdf9a60065b | 185 | print("[WARNING] no action specified; auto-formatting targets.json") |
kadonotakashi | 0:8fdf9a60065b | 186 | |
kadonotakashi | 0:8fdf9a60065b | 187 | targets_file_name = os.path.join(os.curdir, "hal", "targets.json") |
kadonotakashi | 0:8fdf9a60065b | 188 | try : |
kadonotakashi | 0:8fdf9a60065b | 189 | targets = load(targets_file_name) |
kadonotakashi | 0:8fdf9a60065b | 190 | except OSError : |
kadonotakashi | 0:8fdf9a60065b | 191 | print("[ERROR] did not find targets.json where I expected it {}".format(targets_file_name)) |
kadonotakashi | 0:8fdf9a60065b | 192 | exit(1) |
kadonotakashi | 0:8fdf9a60065b | 193 | except ValueError : |
kadonotakashi | 0:8fdf9a60065b | 194 | print("[ERROR] invalid json found in {}".format(targets_file_name)) |
kadonotakashi | 0:8fdf9a60065b | 195 | exit(2) |
kadonotakashi | 0:8fdf9a60065b | 196 | |
kadonotakashi | 0:8fdf9a60065b | 197 | if args.target : |
kadonotakashi | 0:8fdf9a60065b | 198 | for target in args.target : |
kadonotakashi | 0:8fdf9a60065b | 199 | device_file = find(target, os.curdir) |
kadonotakashi | 0:8fdf9a60065b | 200 | if device_file : |
kadonotakashi | 0:8fdf9a60065b | 201 | add_to_targets(targets, device_file, verbose=args.verbose, remove=args.rm) |
kadonotakashi | 0:8fdf9a60065b | 202 | else : |
kadonotakashi | 0:8fdf9a60065b | 203 | print("[WARNING] could not locate a device file for target {}".format(target)) |
kadonotakashi | 0:8fdf9a60065b | 204 | |
kadonotakashi | 0:8fdf9a60065b | 205 | if args.file : |
kadonotakashi | 0:8fdf9a60065b | 206 | for file in args.file : |
kadonotakashi | 0:8fdf9a60065b | 207 | add_to_targets(targets, file, verbose=args.verbose, remove=args.rm) |
kadonotakashi | 0:8fdf9a60065b | 208 | |
kadonotakashi | 0:8fdf9a60065b | 209 | if args.all : |
kadonotakashi | 0:8fdf9a60065b | 210 | for file in find_all_devices(os.curdir, verbose=args.verbose) : |
kadonotakashi | 0:8fdf9a60065b | 211 | add_to_targets(targets, file, verbose=args.verbose, remove=args.rm) |
kadonotakashi | 0:8fdf9a60065b | 212 | |
kadonotakashi | 0:8fdf9a60065b | 213 | dump(targets_file_name, targets) |
kadonotakashi | 0:8fdf9a60065b | 214 | |
kadonotakashi | 0:8fdf9a60065b | 215 | for process in git_processes : |
kadonotakashi | 0:8fdf9a60065b | 216 | process.wait() |