131 lines · python
1#!/usr/bin/env python2"""3wciia - Whose Code Is It Anyway4 5Determines code owner of the file/folder relative to the llvm source root.6Code owner is determined from the content of the CODE_OWNERS.TXT 7by parsing the D: field8 9usage:10 11utils/wciia.py path12 13limitations:14- must be run from llvm source root15- very simplistic algorithm16- only handles * as a wildcard17- not very user friendly 18- does not handle the proposed F: field19 20"""21 22from __future__ import print_function23import os24 25code_owners = {}26 27 28def process_files_and_folders(owner):29 filesfolders = owner["filesfolders"]30 # paths must be in ( ... ) so strip them31 lpar = filesfolders.find("(")32 rpar = filesfolders.rfind(")")33 if rpar <= lpar:34 # give up35 return36 paths = filesfolders[lpar + 1 : rpar]37 # split paths38 owner["paths"] = []39 for path in paths.split():40 owner["paths"].append(path)41 42 43def process_code_owner(owner):44 if "filesfolders" in owner:45 filesfolders = owner["filesfolders"]46 else:47 # print "F: field missing, using D: field"48 owner["filesfolders"] = owner["description"]49 process_files_and_folders(owner)50 code_owners[owner["name"]] = owner51 52 53# process CODE_OWNERS.TXT first54code_owners_file = open("CODE_OWNERS.TXT", "r").readlines()55code_owner = {}56for line in code_owners_file:57 for word in line.split():58 if word == "N:":59 name = line[2:].strip()60 if code_owner:61 process_code_owner(code_owner)62 code_owner = {}63 # reset the values64 code_owner["name"] = name65 if word == "E:":66 email = line[2:].strip()67 code_owner["email"] = email68 if word == "D:":69 description = line[2:].strip()70 code_owner["description"] = description71 if word == "F:":72 filesfolders = line[2:].strip()73 code_owner["filesfolders"].append(filesfolders)74 75 76def find_owners(fpath):77 onames = []78 lmatch = -179 # very simplistic way of findning the best match80 for name in code_owners:81 owner = code_owners[name]82 if "paths" in owner:83 for path in owner["paths"]:84 # print "searching (" + path + ")"85 # try exact match86 if fpath == path:87 return name88 # see if path ends with a *89 rstar = path.rfind("*")90 if rstar > 0:91 # try the longest match,92 rpos = -193 if len(fpath) < len(path):94 rpos = path.find(fpath)95 if rpos == 0:96 onames.append(name)97 onames.append("Chris Lattner")98 return onames99 100 101# now lest try to find the owner of the file or folder102import sys103 104if len(sys.argv) < 2:105 print("usage " + sys.argv[0] + " file_or_folder")106 exit(-1)107 108# the path we are checking109path = str(sys.argv[1])110 111# check if this is real path112if not os.path.exists(path):113 print("path (" + path + ") does not exist")114 exit(-1)115 116owners_name = find_owners(path)117 118# be grammatically correct119print("The owner(s) of the (" + path + ") is(are) : " + str(owners_name))120 121exit(0)122 123# bottom up walk of the current .124# not yet used125root = "."126for dir, subdirList, fileList in os.walk(root, topdown=False):127 print("dir :", dir)128 for fname in fileList:129 print("-", fname)130 print()131