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