2012-11-17 14:35:19 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-17 14:24:37 +08:00
|
|
|
"""
|
|
|
|
wciia - Whose Code Is It Anyway
|
|
|
|
|
|
|
|
Determines code owner of the file/folder relative to the llvm source root.
|
|
|
|
Code owner is determined from the content of the CODE_OWNERS.TXT
|
|
|
|
by parsing the D: field
|
|
|
|
|
|
|
|
usage:
|
|
|
|
|
|
|
|
utils/wciia.py path
|
|
|
|
|
|
|
|
limitations:
|
|
|
|
- must be run from llvm source root
|
|
|
|
- very simplistic algorithm
|
|
|
|
- only handles * as a wildcard
|
|
|
|
- not very user friendly
|
|
|
|
- does not handle the proposed F: field
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-01-03 22:11:33 +08:00
|
|
|
from __future__ import print_function
|
2012-11-17 14:24:37 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
code_owners = {}
|
|
|
|
|
2021-05-16 22:34:09 +08:00
|
|
|
|
2012-11-17 14:24:37 +08:00
|
|
|
def process_files_and_folders(owner):
|
2021-05-16 22:34:09 +08:00
|
|
|
filesfolders = owner['filesfolders']
|
|
|
|
# paths must be in ( ... ) so strip them
|
|
|
|
lpar = filesfolders.find('(')
|
|
|
|
rpar = filesfolders.rfind(')')
|
|
|
|
if rpar <= lpar:
|
|
|
|
# give up
|
|
|
|
return
|
|
|
|
paths = filesfolders[lpar + 1:rpar]
|
|
|
|
# split paths
|
|
|
|
owner['paths'] = []
|
|
|
|
for path in paths.split():
|
|
|
|
owner['paths'].append(path)
|
|
|
|
|
|
|
|
|
2012-11-17 14:24:37 +08:00
|
|
|
def process_code_owner(owner):
|
2021-05-16 22:34:09 +08:00
|
|
|
if 'filesfolders' in owner:
|
|
|
|
filesfolders = owner['filesfolders']
|
|
|
|
else:
|
|
|
|
# print "F: field missing, using D: field"
|
|
|
|
owner['filesfolders'] = owner['description']
|
|
|
|
process_files_and_folders(owner)
|
|
|
|
code_owners[owner['name']] = owner
|
|
|
|
|
|
|
|
|
2012-11-17 14:24:37 +08:00
|
|
|
# process CODE_OWNERS.TXT first
|
|
|
|
code_owners_file = open("CODE_OWNERS.TXT", "r").readlines()
|
|
|
|
code_owner = {}
|
|
|
|
for line in code_owners_file:
|
|
|
|
for word in line.split():
|
2021-05-16 22:34:09 +08:00
|
|
|
if word == "N:":
|
|
|
|
name = line[2:].strip()
|
|
|
|
if code_owner:
|
|
|
|
process_code_owner(code_owner)
|
|
|
|
code_owner = {}
|
|
|
|
# reset the values
|
|
|
|
code_owner['name'] = name
|
|
|
|
if word == "E:":
|
|
|
|
email = line[2:].strip()
|
|
|
|
code_owner['email'] = email
|
|
|
|
if word == "D:":
|
|
|
|
description = line[2:].strip()
|
|
|
|
code_owner['description'] = description
|
|
|
|
if word == "F:":
|
|
|
|
filesfolders = line[2:].strip()
|
|
|
|
code_owner['filesfolders'].append(filesfolders)
|
|
|
|
|
|
|
|
|
2012-11-17 14:24:37 +08:00
|
|
|
def find_owners(fpath):
|
2021-05-16 22:34:09 +08:00
|
|
|
onames = []
|
|
|
|
lmatch = -1
|
|
|
|
# very simplistic way of findning the best match
|
|
|
|
for name in code_owners:
|
|
|
|
owner = code_owners[name]
|
|
|
|
if 'paths' in owner:
|
|
|
|
for path in owner['paths']:
|
|
|
|
# print "searching (" + path + ")"
|
|
|
|
# try exact match
|
|
|
|
if fpath == path:
|
|
|
|
return name
|
|
|
|
# see if path ends with a *
|
|
|
|
rstar = path.rfind('*')
|
|
|
|
if rstar > 0:
|
|
|
|
# try the longest match,
|
|
|
|
rpos = -1
|
|
|
|
if len(fpath) < len(path):
|
|
|
|
rpos = path.find(fpath)
|
|
|
|
if rpos == 0:
|
|
|
|
onames.append(name)
|
|
|
|
onames.append('Chris Lattner')
|
|
|
|
return onames
|
|
|
|
|
|
|
|
|
2012-11-17 14:24:37 +08:00
|
|
|
# now lest try to find the owner of the file or folder
|
|
|
|
import sys
|
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
2021-05-16 22:34:09 +08:00
|
|
|
print("usage " + sys.argv[0] + " file_or_folder")
|
|
|
|
exit(-1)
|
|
|
|
|
2012-11-17 14:24:37 +08:00
|
|
|
# the path we are checking
|
|
|
|
path = str(sys.argv[1])
|
|
|
|
|
|
|
|
# check if this is real path
|
|
|
|
if not os.path.exists(path):
|
2021-05-16 22:34:09 +08:00
|
|
|
print("path (" + path + ") does not exist")
|
|
|
|
exit(-1)
|
|
|
|
|
2012-11-17 14:24:37 +08:00
|
|
|
owners_name = find_owners(path)
|
|
|
|
|
2014-01-25 01:20:08 +08:00
|
|
|
# be grammatically correct
|
2019-01-03 22:11:33 +08:00
|
|
|
print("The owner(s) of the (" + path + ") is(are) : " + str(owners_name))
|
2012-11-17 14:24:37 +08:00
|
|
|
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
# bottom up walk of the current .
|
2021-05-16 22:34:09 +08:00
|
|
|
# not yet used
|
2012-11-17 14:24:37 +08:00
|
|
|
root = "."
|
2021-05-16 22:34:09 +08:00
|
|
|
for dir, subdirList, fileList in os.walk(root, topdown=False):
|
|
|
|
print("dir :", dir)
|
|
|
|
for fname in fileList:
|
|
|
|
print("-", fname)
|
|
|
|
print()
|