| 1 | import os |
|---|
| 2 | import re |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | def get_entity_declarations(ftxt): |
|---|
| 6 | z = re.compile("\s*entity\s+(?P<entities>\w+)\s+is") |
|---|
| 7 | entities = [] |
|---|
| 8 | for m in z.finditer(ftxt): |
|---|
| 9 | entities.extend(m.groupdict().values()) |
|---|
| 10 | return entities |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | def get_entity_instantiations(ftxt): |
|---|
| 14 | z = re.compile("(:\s*entity\s+((?P<library>\w+(?=\.))\.)?(?P<entity>\w+)\s+)|(\s*component\s+(?P<compentity>\w+)\s+)") |
|---|
| 15 | entities = [] |
|---|
| 16 | for m in z.finditer(ftxt): |
|---|
| 17 | d = m.groupdict() |
|---|
| 18 | if d['entity'] != None: |
|---|
| 19 | entities.append(d['entity']) |
|---|
| 20 | if d['compentity'] != None: |
|---|
| 21 | entities.append(d['compentity']) |
|---|
| 22 | |
|---|
| 23 | return entities |
|---|
| 24 | |
|---|
| 25 | def entity_tuple(ftxt): |
|---|
| 26 | return (get_entity_declarations(ftxt),get_entity_instantiations(ftxt)) |
|---|
| 27 | |
|---|
| 28 | #flist is a list of file paths, txtfun is applied to the text in these files. a list of [(file-path, txtfun(file-text)), ...] is returned |
|---|
| 29 | def analyze_files(flist,txtfun): |
|---|
| 30 | return [(filepath,txtfun(file(filepath).read())) for filepath in flist] |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | def get_constants(ftxt): |
|---|
| 34 | |
|---|
| 35 | start_search_at = 0 |
|---|
| 36 | constant_list = [] |
|---|
| 37 | search_results = [res.start() for res in re.finditer("constant\s+\w+\s+:",ftxt)] |
|---|
| 38 | |
|---|
| 39 | for result in search_results: |
|---|
| 40 | colon_after = ftxt.find(":",result) |
|---|
| 41 | constant = ftxt[result+8:colon_after].lstrip("\t ").rstrip("\t ") |
|---|
| 42 | constant_list.append(constant) |
|---|
| 43 | |
|---|
| 44 | return constant_list |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | def uppercase_constants(filepath): |
|---|
| 48 | f = file(filepath) |
|---|
| 49 | ftxt = f.read() |
|---|
| 50 | constant_list = get_constants(ftxt) |
|---|
| 51 | for constant in constant_list: |
|---|
| 52 | ftxt = re.sub("(?i)"+constant,constant.upper(),ftxt) |
|---|
| 53 | file(filepath+".out","wt").write(ftxt) |
|---|
| 54 | |
|---|
| 55 | def get_vhd_files(startpath): |
|---|
| 56 | filelist = [] |
|---|
| 57 | for t in os.walk(startpath): |
|---|
| 58 | for filename in t[2]: |
|---|
| 59 | if filename[-3:].upper() == "VHD": |
|---|
| 60 | filelist.append(t[0]+"\\"+filename) |
|---|
| 61 | return filelist |
|---|
| 62 | |
|---|
| 63 | def main(path): |
|---|
| 64 | files = get_vhd_files(path) |
|---|
| 65 | print files |
|---|
| 66 | for file in files: |
|---|
| 67 | uppercase_constants(file) |
|---|
| 68 | return files |
|---|
| 69 | |
|---|
| 70 | def commit(files): |
|---|
| 71 | for file in files: |
|---|
| 72 | os.remove(file) |
|---|
| 73 | os.rename(file+".out",file) |
|---|
| 74 | |
|---|
| 75 | #use this to delete files appended with '.out' |
|---|
| 76 | def delete_output(files): |
|---|
| 77 | os.remove(file+".out") |
|---|
| 78 | |
|---|
| 79 | import sys |
|---|
| 80 | if __name__ == "__main__": |
|---|
| 81 | if len(sys.argv) > 1: |
|---|
| 82 | files = main(sys.argv[1]) |
|---|
| 83 | else: |
|---|
| 84 | files = main('.') |
|---|