Created
December 4, 2016 16:34
-
-
Save chenz/da61ad8506e9c311810bc0db5af811ef to your computer and use it in GitHub Desktop.
Python script for fixing duplicates in minipro's device database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# This script parses devices.h and prints a copy to stdout in which | |
# all duplicate entries are either removed, or renamed with a number | |
# appended if they have different contents. | |
import re | |
import sys | |
def log(msg): | |
sys.stderr.write("{}\n".format(msg)) | |
def parse_structs(): | |
with open("devices.h") as f: | |
return f.read().split("},")[:-1] | |
name_pattern = re.compile(r'(name\s*=\s*)"([^"]+)"') | |
def main(): | |
name_structs = {} | |
all_structs = parse_structs() | |
log("found {} structs".format(len(all_structs))) | |
for s in all_structs: | |
m = name_pattern.search(s) | |
name = m.group(2) | |
structs = name_structs.setdefault(name, []) | |
occurence = len(structs) + 1 | |
if s in structs: | |
log("redundant: {}".format(name)) | |
elif occurence > 1: | |
log("duplicate name: {} (occurence {})".format(name, occurence)) | |
s_ = name_pattern.sub(r'\1"\2 #{}"'.format(occurence), s) | |
sys.stdout.write(s_ + "},") | |
structs.append(s) | |
else: | |
sys.stdout.write(s + "},") | |
structs.append(s) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment