BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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