1#!/usr/bin/env python 2""" 3wciia - Whose Code Is It Anyway 4 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: field 8 9usage: 10 11utils/wciia.py path 12 13limitations: 14- must be run from llvm source root 15- very simplistic algorithm 16- only handles * as a wildcard 17- not very user friendly 18- does not handle the proposed F: field 19 20""" 21 22from __future__ import print_function 23import os 24 25code_owners = {} 26 27 28def process_files_and_folders(owner): 29 filesfolders = owner["filesfolders"] 30 # paths must be in ( ... ) so strip them 31 lpar = filesfolders.find("(") 32 rpar = filesfolders.rfind(")") 33 if rpar <= lpar: 34 # give up 35 return 36 paths = filesfolders[lpar + 1 : rpar] 37 # split paths 38 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"]] = owner 51 52 53# process CODE_OWNERS.TXT first 54code_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 values 64 code_owner["name"] = name 65 if word == "E:": 66 email = line[2:].strip() 67 code_owner["email"] = email 68 if word == "D:": 69 description = line[2:].strip() 70 code_owner["description"] = description 71 if word == "F:": 72 filesfolders = line[2:].strip() 73 code_owner["filesfolders"].append(filesfolders) 74 75 76def find_owners(fpath): 77 onames = [] 78 lmatch = -1 79 # very simplistic way of findning the best match 80 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 match 86 if fpath == path: 87 return name 88 # see if path ends with a * 89 rstar = path.rfind("*") 90 if rstar > 0: 91 # try the longest match, 92 rpos = -1 93 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 onames 99 100 101# now lest try to find the owner of the file or folder 102import sys 103 104if len(sys.argv) < 2: 105 print("usage " + sys.argv[0] + " file_or_folder") 106 exit(-1) 107 108# the path we are checking 109path = str(sys.argv[1]) 110 111# check if this is real path 112if 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 correct 119print("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 used 125root = "." 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